Saturday, March 14, 2009

Last Day of the month - Java

Playing with dates is always fun.

Here is one of those....

//Given the month, will return the date of the last day of the month
//parameter is month number (starting from 0)
public static Date getLastDayOfMonth (int month) {

Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat( format );

Date d = new Date();
d.setMonth( month );

calendar.set(Calendar.MONTH, month);
//System.out.println("Value "+ calendar.get(calendar.MONTH));
int lastDateofMonth = calendar.getActualMaximum(Calendar.DATE);
d.setDate( lastDateofMonth );

return d;

No comments:

Post a Comment