I want to get the last month End date, I am getting the current month and recuding it by one and setting that month value. But Calender.set(Calender.Month, lastMonth) doesnt work for only a specific date.
public static String getLastMonthEndDate(Date nowDate, String dateFormat)
{
final String METHOD_NAME = "getLastMonthEndDate" ;
SimpleDateFormat formatedDate = new SimpleDateFormat(dateFormat);
Calendar calendar = new GregorianCalendar(2014, Calendar.OCTOBER, 31); // Hard coded as 2014, oct 31st for the sake of example.
//calendar.setTime(nowDate);
System.out.println(calendar.getTime());
if(calendar.get(Calendar.MONTH) == Calendar.JANUARY)
{
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
}
//System.out.println("calendar.get(Calendar.MONTH) -1 : " + (calendar.get(Calendar.MONTH) -1));
int lastMonth = (calendar.get(Calendar.MONTH) -1);
System.out.println("Last month : "+lastMonth);
calendar.set(Calendar.MONTH, lastMonth);
//System.out.println(calendar.getTime());
//System.out.println("(calendar.get(Calendar.MONTH) : " + calendar.get(Calendar.MONTH));
System.out.println("calendar.getActualMaximum(Calendar.DAY_OF_MONTH) : " + calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
System.out.println("calendar.getTime() : " + calendar.getTime());
return formatedDate.format(calendar.getTime());
}
public static void main(String[] args)
{
Date dCurrentDate = getCurrentDate();
String strLastMonthEndDateInYYYY = getLastMonthEndDate(dCurrentDate, "MM/dd/yyyy");
System.out.println("strLastMonthEndDateInYYYY : "+strLastMonthEndDateInYYYY);
}
The Below is the output which i got
output :
Fri Oct 31 00:00:00 IST 2014
Last month : 8
calendar.getActualMaximum(Calendar.DAY_OF_MONTH) : 31
calendar.getTime() : Wed Oct 01 00:00:00 IST 2014
strLastMonthEndDateInYYYY : 10/01/2014
I don't know what where the mistake is. Please help.
It doesn't work because you are setting the date to "09/31/2014" after your operations on calendar, and there is not day 31 in September 2014. By default, java Calendar
is lenient, and will interpret that date as "10/01/2014" (1st October 2014).
Here is the relevant part of the Javadoc:
When a Calendar is in lenient mode, it accepts a wider range of calendar field values than it produces. When a Calendar recomputes calendar field values for return by get(), all of the calendar fields are normalized. For example, a lenient GregorianCalendar interprets MONTH == JANUARY, DAY_OF_MONTH == 32 as February 1.
And here is a commented version of the relevant parts of your code to explain what is going on:
Calendar calendar = new GregorianCalendar(2014, Calendar.OCTOBER, 31); // 10/31/2014
// [...]
int lastMonth = (calendar.get(Calendar.MONTH) -1); // lastMonth will be Calendar.SEPTEMBER
calendar.set(Calendar.MONTH, lastMonth); // internal value of the calendar is now 09/31/2014
// [..]
System.out.println("calendar.getActualMaximum(Calendar.DAY_OF_MONTH) : " + calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // returns 31 because this is a get() method, so 09/31/2014 will be converted to 10/01/2014, and there are 31 days in October (but the internal representation of the calendar will still be 09/31/2014)
// [...]
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // internal representation is still 09/31/2014, so this will not change the value
// [...]
return formatedDate.format(calendar.getTime()); // this is a get() method, so the value will be converted to 10/01/2014
A better solution would be to set your Calendar
to the first day of the month, and subtract one day:
Calendar calendar = new GregorianCalendar(2014, 9, 31);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
System.out.println((new SimpleDateFormat("MM/dd/yyyy")).format(calendar.getTime())); // 09/30/2014