In this section, we are discussing the entire functionality of java.util.Calender class. In the following code segment we are performing various operations on Date object using its methods. The method getTime() of the calendar class returns a Date object that is then passed to the println() method just to print today's date. The arithmetic function of the date class adds specific amount of time to the given time field following the calendar's rule. This example subtracts two years from the current date of the calendar and also add 3 days to the current date of the calendar.
Description of program:
In the following code segment, first we are getting the current date and time into the date object by using the getInstance() and getTime() methods of the calendar class in a method CalendarTimemethod() and printing this current date and time on the console.
Then we are taking another method SimpleDateFormatmethod(), this method is getting an instance of the calendar class into the date object. Now we are taking an object of SimpleDateFormat class. Now we are getting the current date and time from the date object by calling getTime() method on this object. Now we are passing this current date and time into the format() method of the dateformatter object (SimpleDateFormat class) just to get the current date and time into the simple date format, after that we are printing current date and time on the console.
Now we are taking another method Adddates() that adds two different dates. In this method we are taking an instance date of the calendar class into a reference variable cldr and an object dateformatter of the SimpleDateFormat class. Now we are taking the clone of the calendar class and passing the reference of this clone into cldr. Now we are getting the dates of two years ago and five years after, by using the add() method and printing these values on the console.
Now we are taking another method that is DateDifference(). In this method we are taking date and time from an object of the GregorianCalendar class and passing it into an object startDate1 of the Date class and also taking an another object endDate1 of the date class. Now we are taking the difference of times of these two objects and printing it on the console.
Now in the last Getcalendermethods() we are displaying values to demonstrate the various method of the calendar class.
Here is the code of program:
Here is the output:
Current date and time is: Mon Dec 10 18:37:06 GMT+05:30 2007
Current date and time in simple date format: Mon 2007.12.10 at 06:37:07 PM GMT+05:30
Performing operations on calendar dates.
Before two years it was: Sat 2005.12.10 at 06:37:07 PM GMT+05:30
After five years it will be: Sat 2007.12.15 at 06:37:07 PM GMT+05:30
Difference between two dates
Difference between Mon Dec 10 18:37:07 GMT+05:30 2007
and Fri Mar 25 14:00:00 GMT+05:30 2005 is 990 days.
Various get methods of the calendar class:
Year : 2007
Month : 11
Day of Month : 10
Day of Week : 2
Day of Year : 344
Week of Year : 50
Week of Month : 3
Day of the Week in Month : 2
Hour : 6
AM PM : 1
Hour of the Day : 18
Minute : 37
Liked it! Share this Tutorial