Java Calendar Example

The following Java Calendar Example will discuss java.util.Calender class and its methods performing various operations on Date object.

Java Calendar Example

The following Java Calendar Example will discuss java.util.Calender class and its methods performing various operations on Date object.

Java Calendar Example

The following Java Calendar Example will discuss java.util.Calender class and its methods performing various operations on Date object.

getTime() method of the calendar class returns a Date object. It is then passed to the println() method that print today's date.

In this example we will subtract 2 years from the current date of the calendar and few other operations.

For this, we have to get the current date and time into the ‘date’ object. It is done by using the getInstance() and getTime() methods of the Calendar class in a method CalendarTimemethod().

Then using SimpleDateFormatmethod() method we save the instance of the Calendar class into the ‘date’ object.

Now create and object of SimpleDateFormat class, which is used to format the date and time. Then we invoke getTime() method to get the current date and time from the ‘date’ object of Calendar class. Now format() method is used to get the current date and time into the simple date format.

Adddates() method adds two different dates. An instance date of the Calendar class is taken into a reference variable cldr.

Now reference of the clone of the Calendar class is passed into cldr. By using the add() method we get the dates of two years ago.

Now DateDifference() method is used to take date and time from an object of the GregorianCalendar class and is passed into an object startDate1 of the Date class. We also take another object endDate1 of the Date class. Difference of these two objects is printed on the console.

Now in Getcalendermethods(), methods of the Calendar class is used to display various values.

Code of Java Calendar program:

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;

public class CalendarExample {
  
  private static void CalendarTimemethod() {
  Date date = Calendar.getInstance().getTime();
  System.out.println("Current date and time is: " + date);
  System.out.println();
  }

  private static void SimpleDateFormatmethod() {
  Calendar date = Calendar.getInstance();
  SimpleDateFormat dateformatter = new SimpleDateFormat
  ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
  System.out.println("Current date and 

time in simple date format: " 
  + dateformatter.format(date.getTime()));
  System.out.println();
  }

  private static void Adddates() {

  System.out.println("Performing operations on calendar dates.");

  // Get today's date
  Calendar date = Calendar.getInstance();
  Calendar cldr;
  SimpleDateFormat dateformatter = new SimpleDateFormat
  ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

  cldr = (Calendar) date.clone();
  cldr.add(Calendar.DAY_OF_YEAR, - (365 * 2));
  System.out.println("Before two years it was: "
  + dateformatter.format(cldr.getTime()));

  cldr = (Calendar) date.clone();
  cldr.add(Calendar.DAY_OF_YEAR, + 5);
  System.out.println("After five years it will be: " 
 + dateformatter.format(cldr.getTime()));

  System.out.println();
  }

  private static void DateDifference() {
  
  System.out.println("Difference between two dates");
  Date startDate1 = new GregorianCalendar(2005, 02, 

25, 14, 00).getTime();
  Date endDate1 = new Date();;

  long diff = endDate1.getTime() - startDate1.getTime();

  System.out.println("  Difference between " + endDate1);
  System.out.println("  and " + startDate1 + " is " 
   + (diff / 

(1000L*60L*60L*24L)) + " days.");
  System.out.println();
  }

  private static void Getcalendermethods() {

  System.out.println("Various get methods 

of the calendar class:");
  Calendar calender = Calendar.getInstance();

  System.out.println("Year : " 

+ calender.get(Calendar.YEAR));
  System.out.println("Month  : " 

+ calender.get(Calendar.MONTH));
  System.out.println("Day of Month  : " 

+ calender.get(Calendar.DAY_OF_MONTH));
  System.out.println("Day of Week  : " 

+ calender.get(Calendar.DAY_OF_WEEK));
  System.out.println("Day of Year  : " 

+ calender.get(Calendar.DAY_OF_YEAR));
  System.out.println("Week of Year  : " 

+ calender.get(Calendar.WEEK_OF_YEAR));
  System.out.println("Week of Month  : " 

+ calender.get(Calendar.WEEK_OF_MONTH));
  System.out.println
   ("Day of the Week in Month : " 

+ calender.get(Calendar.DAY_OF_WEEK_IN_MONTH));
  System.out.println("Hour  

 : " + calender.get(Calendar.HOUR));
  System.out.println("AM PM 

 : " + calender.get(Calendar.AM_PM));
  System.out.println("Hour of the Day 

 : " + calender.get(Calendar.HOUR_OF_DAY));
  System.out.println("Minute 

  : " + calender.get(Calendar.MINUTE));
  System.out.println("Second  

 : " + calender.get(Calendar.SECOND));
  System.out.println();
  }

  public static void main(String[] args) {
  System.out.println();
  CalendarTimemethod();
  SimpleDateFormatmethod();
  Adddates();
  DateDifference();
  Getcalendermethods();
  }
}

Output:

C:\Examples>java CalendarExample

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