Example usage for java.util Calendar set

List of usage examples for java.util Calendar set

Introduction

In this page you can find the example usage for java.util Calendar set.

Prototype

public final void set(int year, int month, int date, int hourOfDay, int minute) 

Source Link

Document

Sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.

Usage

From source file:Main.java

/**
 * Determines what year a transaction belongs to.
 * /*ww  w  . j  ava  2  s  .co  m*/
 * If the given <code>day</code> of the given <code>month</code> for the current year
 * is in the future the transaction is probably from last year.
 * 
 * @param month     The month, where January is 1.
 * @param day       The day of the month, starting from 1.
 * @return          An ISO 8601 formatted date.
 */
public static String getTransactionDate(int month, int day) {
    month--; // Java-months start at 0
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    int currentYear = cal.get(Calendar.YEAR);
    cal.set(currentYear, month, day, 0, 0);
    if (cal.getTime().after(Calendar.getInstance().getTime())) {
        //If the transaction is in the future the year is probably of by +1.
        cal.add(Calendar.YEAR, -1);
    }
    return sdf.format(cal.getTime());
}