Calendar and its Subclasses with Examples in Java

Creating Calendar objects

Calendar objects can be created by using getInstance() or by using the constructor GregorianCalendar.

It’s important to notice that months in Calendar are zero based, which means that JANUARY is represented by an int value 0. In order to provide a better code, always use Calendar constants, such as Calendar.JANUARY to avoid misunderstandings.

Related Article: How to Create a Specific Date in Java

Calendar calendar = Calendar.getInstance();
Calendar gregorianCalendar = new GregorianCalendar();
Calendar gregorianCalendarAtSpecificDay = new GregorianCalendar(2016, Calendar.JANUARY, 1);
Calendar gregorianCalendarAtSpecificDayAndTime = new GregorianCalendar(2016, Calendar.JANUARY, 1,
6, 55, 10);

Note: Always use the month constants: The numeric representation is misleading, e.g. Calendar.JANUARY has the value 0

Increasing / Decreasing calendar fields

add() and roll() can be used to increase/decrease Calendar fields.

Calendar calendar = new GregorianCalendar(2016, Calendar.MARCH, 31); // 31 March 2016

The add() method affects all fields, and behaves effectively if one were to add or subtract actual dates from the calendar

calendar.add(Calendar.MONTH, -6);

The above operation removes six months from the calendar, taking us back to 30 September 2015.

To change a particular field without affecting the other fields, use roll().

calendar.roll(Calendar.MONTH, -6);

The above operation removes six months from the current month, so the month is identified as September. No other fields have been adjusted; the year has not changed with this operation.

Subtracting calendars

To get a difference between two Calendars, use getTimeInMillis() method:

Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c2.set(Calendar.DATE, c2.get(Calendar.DATE) + 1);

System.out.println(c2.getTimeInMillis() - c1.getTimeInMillis()); //outputs 86400000 (24 * 60 * 60 *
1000)

Finding AM/PM

With Calendar class it is easy to find AM or PM.

Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
if (cal.get(Calendar.AM_PM) == Calendar.PM)
     System.out.println("It is PM");

Leave a Comment