Java Date - java.util.Calendar Class

Calendar Class

The Calendar class (java.util package) is used when working with dates and times in Java.

Date/Time

To create an instance of the Calendar class, use the getInstance method rather than the new operator.

Calendar cal = Calendar.getInstance();

getTime Method

The getTime method gets the calendar date/time.

public final Date getTime()

By using the getTime method, you can get the current time from a calendar as a Date object (java.util package).

package com.devkuma.basic.datetime;

import java.util.Calendar;

public class CalTime {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println(cal.getTime());  // Result: Mon Sep 05 11:34:21 KST 2022
    }
}

Result:

Mon Sep 05 11:35:30 KST 2022

get Method

The get method gets date/time elements.

public int get(int field)
  field: date/time field to get

To get a specific date/time element from a calendar, use the get method. In the get method, specify an integer corresponding to the date/time element you want to retrieve.

Main constants available in the Calendar class (date/time elements)

Constant Overview
YEAR Year
MONTH Month (0-11)
DATE Day
DAY_OF_YEAR Day of the year
WEEK_OF_MONTH Week of the month
WEEK_OF_YEAR Week of the year
HOUR_OF_DAY Hour (24-hour)
HOUR Hour (12-hour)
AM_PM AM (0), PM (1)
MINUTE Minute
SECOND Second
MILLISECOND Millisecond
DAY_OF_WEEK Day of week

DAY_OF_WEEK returns constants such as SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.

package com.devkuma.basic.datetime;

import java.util.Calendar;

public class CalGet {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println(cal.get(Calendar.YEAR)); // Result: 2022
        System.out.println(cal.get(Calendar.MONTH)); // Result: 8
        System.out.println(cal.get(Calendar.DATE)); // Result: 5
    }
}

Result:

2022
8
5

set Method

The set method sets date/time elements.

public final void set(int year, int month, int date [,int hour, int minute])
public void set(int field, int value)
  year: year
  month: month
  date: day
  hour: hour
  minute: minute
  field: date/time field to set
  value: value to set for the field argument

To set calendar date/time elements, use the set method. To specify year, month, day, hour, and minute together, use the first syntax. To specify a particular date/time element, such as year, use the second syntax with a date/time field and value pair. For values that can be specified for the field argument, refer to the table in the get method section above.

package com.devkuma.basic.datetime;

import java.util.Calendar;

public class CalSet {

    public static void main(String[] args) {
        Calendar cal1 = Calendar.getInstance();
        cal1.set(2022, 8, 1, 15, 20, 10);
        System.out.println(cal1.getTime()); // Result: Thu Sep 01 15:20:10 KST 2022

        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.YEAR, 2022);
        System.out.println(cal2.getTime()); // Result: Mon Sep 05 11:41:55 KST 2022
    }
}

Result:

Thu Sep 01 15:20:10 KST 2022
Mon Sep 05 11:41:55 KST 2022

add Method

The add method adds or subtracts date/time elements.

public abstract void add(int field, int amount)
  field: date/time field to calculate
  amount: increment or decrement

To add or subtract values based on the current date/time element, use the add method. In the add method, only the value of the amount argument is added to the specified date/time element. If amount is negative, subtraction is performed.

CalAdd.java

package com.devkuma.basic.datetime;

import java.util.Calendar;

public class CalAdd {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println(cal.getTime());  // Result: Mon Sep 05 11:43:44 KST 2022

        cal.add(Calendar.YEAR, 3);
        System.out.println(cal.getTime());  // Result: Fri Sep 05 11:43:44 KST 2025

        cal.add(Calendar.YEAR, -5);
        System.out.println(cal.getTime());  // Result: Sat Sep 05 11:43:44 KST 2020
    }
}

Result:

Mon Sep 05 11:43:44 KST 2022
Fri Sep 05 11:43:44 KST 2025
Sat Sep 05 11:43:44 KST 2020

roll Method

The roll method adds or subtracts date/time elements by rolling them.

public void roll(int field, int amount)
public abstract void roll(int field, boolean up)
  field: date/time field to add or subtract
  amount: increment or decrement
  up: whether to increase

The roll method also adds or subtracts date/time elements like the add method. However, it differs from add in the following ways.

  1. It does not affect other fields.
    If 1 minute is added to 17:59, the add method carries over and becomes 18:00. The roll method does not carry over, so it becomes 17:00.

  2. A boolean value can be specified as the second argument.
    If true is specified, the date/time field increases; if false is specified, it decreases.

package com.devkuma.basic.datetime;

import java.util.Calendar;

public class CalRoll {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println(cal.getTime());  // Result: Mon Sep 05 11:46:43 KST 2022

        cal.roll(Calendar.MINUTE, true);
        System.out.println(cal.getTime());  // Result: Mon Sep 05 11:47:43 KST 2022
    }
}

Result:

Mon Sep 05 11:46:43 KST 2022
Mon Sep 05 11:47:43 KST 2022

clear Method

The clear method clears date/time elements.

public final void clear([int field])
  field: date/time field to clear

When you use the clear method, the value of the specified date/time element is cleared. The cleared field is set to an initial value determined by the field.

If the field argument is omitted, all fields are cleared. In that case, the value becomes “1970/01/01 00:00:00”.

package com.devkuma.basic.datetime;

import java.util.Calendar;

public class CalClear {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println(cal.getTime());  // Result: Mon Sep 05 11:52:06 KST 2022

        cal.clear(Calendar.SECOND);
        System.out.println(cal.getTime());  // Result: Mon Sep 05 11:52:00 KST 2022

        cal.clear();
        System.out.println(cal.getTime());  // Result: Thu Jan 01 00:00:00 KST 1970
    }
}

Result:

Mon Sep 05 11:52:06 KST 2022
Mon Sep 05 11:52:00 KST 2022
Thu Jan 01 00:00:00 KST 1970

before/equals/after Methods

The before/equals/after methods compare dates.

public boolean after(Object when)
public boolean before(Object when)
public boolean equals(Object when)
  when: calendar to compare

To compare calendar values, use the before, equals, and after methods. The before method returns true when the compared calendar is before the current calendar, equals returns true when they are the same, and after returns true when it is later.

CalEquals.java

package com.devkuma.basic.datetime;

import java.util.Calendar;

public class CalEquals {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal.set(Calendar.YEAR, 2020);
        cal2.set(Calendar.YEAR, 2015);
        System.out.println(cal.before(cal2));   // Result: false
        System.out.println(cal.equals(cal2));   // Result: false
        System.out.println(cal.after(cal2));    // Result: true
    }
}

Result:

false
false
true

Calendars being equal means not only that their date/time values are the same, but also that calendar settings such as timezone and locale are the same. The equals method returns false for different calendars.