Monday, June 2, 2008

How to get last day of each month

import java.util.Calendar;

public class CalProblem
{
  public static void main(String[] args)
  {
     Calendar myCal     = Calendar.getInstance();

     myCal.set(Calendar.MONTH, 1);
     myCal.set(Calendar.YEAR, 2007);
     System.out.println("");
     System.out.println("Year               => "+myCal.get(Calendar.YEAR));
     System.out.println("Month              => "+myCal.get(Calendar.MONTH));
     System.out.println("Last Day of Month  => "+myCal.getActualMaximum(Calendar.DAY_OF_MONTH));

     myCal.set(Calendar.MONTH, 1);
     myCal.set(Calendar.YEAR, 2008);
     System.out.println("");
     System.out.println("Year               => "+myCal.get(Calendar.YEAR));
     System.out.println("Month              => "+myCal.get(Calendar.MONTH));
     System.out.println("Last Day of Month  => "+myCal.getActualMaximum(Calendar.DAY_OF_MONTH));

  }
}

No comments:

Google+