Ad

Sunday 29 September 2013

Working with Calendar in Java - Using set and add methods

Calendar is an very important API for working and manipulating with dates.we may have the scenario where we need to schedule the job at the specified date/time.So first we may need to construct the specified date object and then schedule using Timer or any other Scheduler Service at the given date.Calendar makes the date construction very easy.It provides the below methods to achieve this.
getInstance()
This static method of the Calendar class return the calendar object with the current date and time.
add(int field,int value)
Will add the given value to the Calendar field for the date in Calendar.There are may Calendar field like Calendar.YEAR,Calendar.MONTH,Calendar.Date,Calendar.HOUR etc.For instance
  • add(Calendar.YEAR,1) - will add 1 year to the date in Calendar
  • add(Calendar.MONTH,1) - will add 1 month to the date in Calendar
  • add(Calendar.DATE,1) - will add 1 day to the date in Calendar
set(int field,int value)
Will set the give value to the Calendar Field for the date in Calendar.For instance
  • set( Calendar.MONTH,Calendar.JANUARY) - will set the month of the calendar to January.
  • set( Calendar.HOUR_OF_DAY,1) - will set the hour part of the time in Calendar as 1.
geTime()
Will return the Java.util.Date object set in the Calendar

we can more understand about this by seeing the sample program below.Lets imagine we need :
1)To construct the date which falls in Next New Year
2)To construct the date which falls in immediate Thursday
3)To construct the date which falls in First Monday of March Month
The program for this is shown below:

import java.util.Calendar;                                                                                                                  

public class CalendarExample {

public static void main(String[] args) {
CalendarExample calendarExample= new CalendarExample();
calendarExample.nextNewYear();
calendarExample.immediateThursday();
calendarExample.firstMondayOfMarch();
}

/*To construct the date which falls in Next New Year*/
public void nextNewYear()
{
Calendar calendar= Calendar.getInstance();
System.out.println("Present Date/Time:"+calendar.getTime());
calendar.add(Calendar.YEAR,1);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
System.out.println("Next Newyear is at:"+calendar.getTime());
}

/*To construct the date which falls in immediate Thursday*/
        public void immediateThursday() {
           Calendar calendar= Calendar.getInstance();
System.out.println("Present Date/Time:"+calendar.getTime());
if(calendar.get(Calendar.DAY_OF_WEEK)>=Calendar.THURSDAY)
{
calendar.add(Calendar.DATE,7);
}
calendar.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY);
System.out.println("Immediate Thursday is at:"+calendar.getTime());
}
 
        /*To construct the date which falls in First Monday of March Month*/
        public void firstMondayOfMarch() {
           Calendar calendar= Calendar.getInstance();
                System.out.println("Present Date/Time:"+calendar.getTime());
calendar.set(Calendar.MONTH, Calendar.MARCH);
calendar.set(Calendar.DAY_OF_MONTH,1 );
if(calendar.get(Calendar.DAY_OF_WEEK)>Calendar.MONDAY)
{
calendar.set(Calendar.WEEK_OF_MONTH,2);
}
else
{
calendar.set(Calendar.WEEK_OF_MONTH,1);
}
calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
System.out.println("First Monday of March is at:"+calendar.getTime());
}
}

To construct the date which falls in Next New Year
As we can see in the method nextNewYear(),we got the calendar Instance which will have the current
date and Time.Then we
   1) added 1 year to the calendar using calendar Field Calendar.Year in add method
   2)set the Month as January
   3)Set the Day_OF_THE_MONTH as 1
   4)Set the HOUR_OF_DAY,MINUTE and SECONDS as 0.
If the current year is 2013, Constructed date object will contain value 01/01/2014 00:00:00

To construct the date which falls in immediate Thursday
As we can see in the method immediateThursday() ,we got the calendar Instance which will have the
current date and Time.Then we
   1)First checked if the current day of the week is passed Thursday i.e current day is after Thursday.
   2)If the current day is equal to or after thursday,we added 7 days to the current date.Since thursday is
   passed in current week,we moved to next week.
   3)Then we set the day of the week as Thursday.
If the current date is 29/09/2013 17:52:54, Constructed date object will contain value 03/10/2013 17:52:54

To construct the date which falls in First Monday of March Month
As we can see in the method firstMondayOfMarch() ,we got the calendar Instance which will have the
current date and Time.Then we
   1)Set the Month as March
   2)Set the Day of the Month as 1.
   3)Then we checked if the the First day of the Month has passed Monday i.e to find if the first
   occurrence of  Monday falls is in first Week of March.If the first occurrence of Monday falls in first
   week of March we set the Week_of_month as 1 else set it as 2.
   4)Set the Day_Of_The_week as Monday
   5)Set the HOUR_OF_DAY,MINUTE and SECONDS as 0.
If the current year is 2013, Constructed date object will contain value 04/03/2013 00:00:00

If you run the above program,you will get the output as shown below:

Present Date/Time:Sun Sep 29 17:52:54 IST 2013
Next Newyear is at:Wed Jan 01 00:00:00 IST 2014
Present Date/Time:Sun Sep 29 17:52:54 IST 2013
Immediate Thursday is at:Thu Oct 03 17:52:54 IST 2013
Present Date/Time:Sun Sep 29 17:52:54 IST 2013
First Monday of March is at:Mon Mar 04 00:00:00 IST 2013

No comments:

Post a Comment