Java : Calendar with Examples

Calendar (Java SE 20 & JDK 20) API Examples.
You will find code examples on most Calendar methods.

Warning :


Summary

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).

Class diagram

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.DECEMBER, 31)
        .setTimeOfDay(14, 30, 59)
        .build();

System.out.printf("%tF %tT%n", calendar, calendar); // 2100-12-31 14:30:59

System.out.println(calendar.get(Calendar.YEAR)); // 2100
System.out.println(calendar.get(Calendar.MONTH) == Calendar.DECEMBER); // true
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 14
System.out.println(calendar.get(Calendar.MINUTE)); //30
System.out.println(calendar.get(Calendar.SECOND)); // 59

Fields

static final int ALL_STYLES

A style specifier for getDisplayNames indicating names in all styles, such as "January" and "Jan".

final var calendar = new Calendar.Builder().build();
final var names = calendar.getDisplayNames(
        Calendar.MONTH, Calendar.ALL_STYLES, Locale.US);

// {September=8, December=11, February=1, November=10, January=0, October=9,
//  August=7, April=3, March=2, July=6, June=5, Apr=3, Aug=7, Dec=11, Feb=1,
//  Jan=0, Jul=6, Jun=5, Mar=2, May=4, Nov=10, Oct=9, Sep=8}
System.out.println(names);

static final int AM

Value of the AM_PM field indicating the period of the day from midnight to just before noon.

final var calendar = new Calendar.Builder()
        .setTimeOfDay(10, 0, 0)
        .build();

System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 10

System.out.println(calendar.get(Calendar.AM_PM) == Calendar.AM); // true
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.PM); // false
final var calendar = new Calendar.Builder()
        .setTimeOfDay(17, 0, 0)
        .build();

System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 17

System.out.println(calendar.get(Calendar.AM_PM) == Calendar.AM); // false
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.PM); // true

static final int AM_PM

Field number for get and set indicating whether the HOUR is before or after noon.

Please see AM.

static final int APRIL

Value of the MONTH field indicating the fourth month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.APRIL, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.APRIL); // true

protected boolean areFieldsSet

True if fields[] are in sync with the currently set time.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

static final int AUGUST

Value of the MONTH field indicating the eighth month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.AUGUST, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.AUGUST); // true

static final int DATE

Field number for get and set indicating the day of the month.

This is a synonym for DAY_OF_MONTH.

static final int DAY_OF_MONTH

Field number for get and set indicating the day of the month.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 15)
        .build();

System.out.printf("%tF%n", calendar); // 2100-01-15

System.out.println(calendar.get(Calendar.YEAR)); // 2100
System.out.println(calendar.get(Calendar.MONTH) == Calendar.JANUARY); // true
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 15

static final int DAY_OF_WEEK

Field number for get and set indicating the day of the week.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 3)
        .build();

{
    final var name = calendar.getDisplayName(
            Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
    System.out.println(name); // Sunday

    System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // 1
}

calendar.add(Calendar.DATE, 1);

{
    final var name = calendar.getDisplayName(
            Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
    System.out.println(name); // Monday

    System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // 2
}

calendar.add(Calendar.DATE, 5);

{
    final var name = calendar.getDisplayName(
            Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
    System.out.println(name); // Saturday

    System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // 7
}

static final int DAY_OF_WEEK_IN_MONTH

Field number for get and set indicating the ordinal number of the day of the week within the current month.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

final var name = calendar.getDisplayName(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Friday

System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // 1

calendar.set(Calendar.DAY_OF_MONTH, 7);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // 1

calendar.set(Calendar.DAY_OF_MONTH, 8);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // 2

calendar.set(Calendar.DAY_OF_MONTH, 14);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // 2

calendar.set(Calendar.DAY_OF_MONTH, 15);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // 3

static final int DAY_OF_YEAR

Field number for get and set indicating the day number within the current year.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 1

calendar.set(2100, Calendar.JANUARY, 31);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 31

calendar.set(2100, Calendar.FEBRUARY, 1);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 32

calendar.set(2100, Calendar.DECEMBER, 31);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 365

static final int DECEMBER

Value of the MONTH field indicating the twelfth month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.DECEMBER, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.DECEMBER); // true

static final int DST_OFFSET

Field number for get and set indicating the daylight saving offset in milliseconds.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.AUGUST, 1)
        .setTimeZone(zone)
        .build();

System.out.println(calendar.get(Calendar.DST_OFFSET)); // 3600000

static final int ERA

Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

System.out.println(calendar.get(Calendar.ERA)); // 1

calendar.set(Calendar.YEAR, -100);
System.out.println(calendar.get(Calendar.ERA)); // 0

static final int FEBRUARY

Value of the MONTH field indicating the second month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.FEBRUARY, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.FEBRUARY); // true

static final int FIELD_COUNT

The number of distinct fields recognized by get and set.

System.out.println(Calendar.FIELD_COUNT); // 17

protected int[] fields

The calendar field values for the currently set time for this calendar.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

static final int FRIDAY

Value of the DAY_OF_WEEK field indicating Friday.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

final var name = calendar.getDisplayName(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Friday

final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.FRIDAY); // true

static final int HOUR

Field number for get and set indicating the hour of the morning or afternoon.

final var calendar = new Calendar.Builder()
        .setTimeOfDay(14, 30, 59, 999)
        .build();

System.out.printf("%tT%n", calendar); // 14:30:59

System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 14
System.out.println(calendar.get(Calendar.HOUR)); // 2

System.out.println(calendar.get(Calendar.MINUTE)); // 30
System.out.println(calendar.get(Calendar.SECOND)); // 59
System.out.println(calendar.get(Calendar.MILLISECOND)); // 999

static final int HOUR_OF_DAY

Field number for get and set indicating the hour of the day.

Please see HOUR.

protected boolean[] isSet

The flags which tell if a specified calendar field for the calendar is set.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected boolean isTimeSet

True if then the value of time is valid.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

static final int JANUARY

Value of the MONTH field indicating the first month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.JANUARY); // true

static final int JULY

Value of the MONTH field indicating the seventh month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JULY, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.JULY); // true

static final int JUNE

Value of the MONTH field indicating the sixth month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JUNE, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.JUNE); // true

static final int LONG

A style specifier for getDisplayName and getDisplayNames equivalent to LONG_FORMAT.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

final var name1 = calendar.getDisplayName(
        Calendar.MONTH, Calendar.LONG, Locale.US);
System.out.println(name1); // January

final var name2 = calendar.getDisplayName(
        Calendar.MONTH, Calendar.SHORT, Locale.US);
System.out.println(name2); // Jan

final var name3 = calendar.getDisplayName(
        Calendar.MONTH, Calendar.NARROW_FORMAT, Locale.US);
System.out.println(name3); // J

static final int LONG_FORMAT

A style specifier for getDisplayName and getDisplayNames indicating a long name used for format.

This field is equivalent to LONG.

static final int LONG_STANDALONE

A style specifier for getDisplayName and getDisplayNames indicating a long name used independently, such as a month name as calendar headers.

final var calendar = new Calendar.Builder().build();

final var name1 = calendar.getDisplayNames(
        Calendar.ERA, Calendar.LONG, Locale.US);
System.out.println(name1); // {BC=0, AD=1}

final var name2 = calendar.getDisplayNames(
        Calendar.ERA, Calendar.LONG_STANDALONE, Locale.US);
System.out.println(name2); // {Before Christ=0, Anno Domini=1}

static final int MARCH

Value of the MONTH field indicating the third month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.MARCH, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.MARCH); // true

static final int MAY

Value of the MONTH field indicating the fifth month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.MAY, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.MAY); // true

static final int MILLISECOND

Field number for get and set indicating the millisecond within the second.

Please see HOUR.

static final int MINUTE

Field number for get and set indicating the minute within the hour.

Please see HOUR.

static final int MONDAY

Value of the DAY_OF_WEEK field indicating Monday.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 4)
        .build();

final var name = calendar.getDisplayName(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Monday

final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.MONDAY); // true

static final int MONTH

Field number for get and set indicating the month.

Please see DAY_OF_MONTH.

static final int NARROW_FORMAT

A style specifier for getDisplayName and getDisplayNames indicating a narrow name used for format.

Please see LONG.

static final int NARROW_STANDALONE

A style specifier for getDisplayName and getDisplayNames indicating a narrow name independently.

final var calendar = new Calendar.Builder().build();

final var name1 = calendar.getDisplayNames(
        Calendar.ERA, Calendar.NARROW_FORMAT, Locale.US);
System.out.println(name1); // {A=1, B=0}

final var name2 = calendar.getDisplayNames(
        Calendar.ERA, Calendar.NARROW_STANDALONE, Locale.US);
System.out.println(name2); // {A=1, B=0}

static final int NOVEMBER

Value of the MONTH field indicating the eleventh month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.NOVEMBER, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.NOVEMBER); // true

static final int OCTOBER

Value of the MONTH field indicating the tenth month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.OCTOBER, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.OCTOBER); // true

static final int PM

Value of the AM_PM field indicating the period of the day from noon to just before midnight.

Please see AM.

static final int SATURDAY

Value of the DAY_OF_WEEK field indicating Saturday.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 2)
        .build();

final var name = calendar.getDisplayName(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Saturday

final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.SATURDAY); // true

static final int SECOND

Field number for get and set indicating the second within the minute.

Please see HOUR.

static final int SEPTEMBER

Value of the MONTH field indicating the ninth month of the year in the Gregorian and Julian calendars.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.SEPTEMBER, 1)
        .build();

System.out.println(calendar.get(Calendar.MONTH) == Calendar.SEPTEMBER); // true

static final int SHORT

A style specifier for getDisplayName and getDisplayNames equivalent to SHORT_FORMAT.

Please see LONG.

static final int SHORT_FORMAT

A style specifier for getDisplayName and getDisplayNames indicating a short name used for format.

This field is equivalent to SHORT.

static final int SHORT_STANDALONE

A style specifier for getDisplayName and getDisplayNames indicating a short name used independently, such as a month abbreviation as calendar headers.

final var calendar = new Calendar.Builder().build();

final var name1 = calendar.getDisplayNames(
        Calendar.ERA, Calendar.SHORT, Locale.US);
System.out.println(name1); // {AD=1, BC=0}

final var name2 = calendar.getDisplayNames(
        Calendar.ERA, Calendar.SHORT_STANDALONE, Locale.US);
System.out.println(name2); // {AD=1, BC=0}

static final int SUNDAY

Value of the DAY_OF_WEEK field indicating Sunday.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 3)
        .build();

final var name = calendar.getDisplayName(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Sunday

final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.SUNDAY); // true

static final int THURSDAY

Value of the DAY_OF_WEEK field indicating Thursday.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 7)
        .build();

final var name = calendar.getDisplayName(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Thursday

final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.THURSDAY); // true

protected long time

The currently set time for this calendar, expressed in milliseconds after January 1, 1970, 0:00:00 GMT.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

static final int TUESDAY

Value of the DAY_OF_WEEK field indicating Tuesday.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 5)
        .build();

final var name = calendar.getDisplayName(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Tuesday

final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.TUESDAY); // true

static final int UNDECIMBER

Value of the MONTH field indicating the thirteenth month of the year.

System.out.println(Calendar.UNDECIMBER); // 12

static final int WEDNESDAY

Value of the DAY_OF_WEEK field indicating Wednesday.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 6)
        .build();

final var name = calendar.getDisplayName(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Wednesday

final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.WEDNESDAY); // true

static final int WEEK_OF_MONTH

Field number for get and set indicating the week number within the current month.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

System.out.println(calendar.getFirstDayOfWeek() == Calendar.SUNDAY); // true
System.out.println(calendar.getMinimalDaysInFirstWeek()); // 1

final Supplier<String> dayOfWeekName = () -> {
    return calendar.getDisplayName(
            Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
};

System.out.println(dayOfWeekName.get()); // Friday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 1

calendar.set(Calendar.DAY_OF_MONTH, 2);

System.out.println(dayOfWeekName.get()); // Saturday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 1

calendar.set(Calendar.DAY_OF_MONTH, 3);

System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 2

calendar.set(Calendar.DAY_OF_MONTH, 9);

System.out.println(dayOfWeekName.get()); // Saturday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 2

calendar.set(Calendar.DAY_OF_MONTH, 10);

System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 3
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 3

calendar.set(Calendar.DAY_OF_MONTH, 31);

System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 6
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 6

calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
calendar.set(Calendar.DAY_OF_MONTH, 1);

System.out.println(dayOfWeekName.get()); // Monday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 6

calendar.set(Calendar.DAY_OF_MONTH, 7);

System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 7

static final int WEEK_OF_YEAR

Field number for get and set indicating the week number within the current year.

Please see WEEK_OF_MONTH.

static final int YEAR

Field number for get and set indicating the year.

Please see DAY_OF_MONTH.

static final int ZONE_OFFSET

Field number for get and set indicating the raw offset from GMT in milliseconds.

final var zone = TimeZone.getTimeZone("America/Los_Angeles");

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .setTimeZone(zone)
        .build();

System.out.println(calendar.get(Calendar.ZONE_OFFSET)); // -28800000

Constructors

Calendar ()

Constructs a Calendar with the default time zone and the default FORMAT locale.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

Calendar (TimeZone zone, Locale aLocale)

Constructs a calendar with the specified time zone and locale.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

Methods

abstract void add (int field, int amount)

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

System.out.printf("%tF%n", calendar); // 2100-01-01

calendar.add(Calendar.DATE, 15);
System.out.printf("%tF%n", calendar); // 2100-01-16

calendar.add(Calendar.DATE, 30);
System.out.printf("%tF%n", calendar); // 2100-02-15

calendar.add(Calendar.MONTH, 100);
System.out.printf("%tF%n", calendar); // 2108-06-15

boolean after (Object when)

Returns whether this Calendar represents a time after the time represented by the specified Object.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

System.out.printf("%tF%n", calendar); // 2100-01-01

final var calendar2 = new Calendar.Builder()
        .setDate(2099, Calendar.JANUARY, 1)
        .build();

final var calendar3 = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

final var calendar4 = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 2)
        .build();

System.out.printf("%tF%n", calendar2); // 2099-01-01
System.out.printf("%tF%n", calendar3); // 2100-01-01
System.out.printf("%tF%n", calendar4); // 2100-01-02

System.out.println(calendar.after(calendar2)); // true
System.out.println(calendar.after(calendar3)); // false
System.out.println(calendar.after(calendar4)); // false

boolean before (Object when)

Returns whether this Calendar represents a time before the time represented by the specified Object.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

System.out.printf("%tF%n", calendar); // 2100-01-01

final var calendar2 = new Calendar.Builder()
        .setDate(2099, Calendar.JANUARY, 1)
        .build();

final var calendar3 = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

final var calendar4 = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 2)
        .build();

System.out.printf("%tF%n", calendar2); // 2099-01-01
System.out.printf("%tF%n", calendar3); // 2100-01-01
System.out.printf("%tF%n", calendar4); // 2100-01-02

System.out.println(calendar.before(calendar2)); // false
System.out.println(calendar.before(calendar3)); // false
System.out.println(calendar.before(calendar4)); // true

final void clear ()

Sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.AUGUST, 10)
        .setTimeOfDay(14, 30, 59)
        .build();

System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-10 14:30:59

calendar.clear();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00

final void clear (int field)

Sets the given calendar field value and the time value (millisecond offset from the Epoch) of this Calendar undefined.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.AUGUST, 5)
        .build();

System.out.printf("%tF%n", calendar); // 2100-08-05

calendar.clear(Calendar.YEAR);
System.out.printf("%tF%n", calendar); // 1970-08-05

calendar.clear(Calendar.MONTH);
System.out.printf("%tF%n", calendar); // 1970-01-05

calendar.clear(Calendar.DATE);
System.out.printf("%tF%n", calendar); // 1970-01-01

Object clone ()

Creates and returns a copy of this object.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.AUGUST, 5)
        .setTimeOfDay(14, 30, 59)
        .build();

System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-05 14:30:59

final var cloned = calendar.clone();
System.out.printf("%tF %tT%n", cloned, cloned); // 2100-08-05 14:30:59

int compareTo (Calendar anotherCalendar)

Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

System.out.printf("%tF%n", calendar); // 2100-01-01

final var calendar2 = new Calendar.Builder()
        .setDate(2099, Calendar.JANUARY, 1)
        .build();

final var calendar3 = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

final var calendar4 = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 2)
        .build();

System.out.printf("%tF%n", calendar2); // 2099-01-01
System.out.printf("%tF%n", calendar3); // 2100-01-01
System.out.printf("%tF%n", calendar4); // 2100-01-02

System.out.println(calendar.compareTo(calendar2)); // 1
System.out.println(calendar.compareTo(calendar3)); // 0
System.out.println(calendar.compareTo(calendar4)); // -1

protected void complete ()

Fills in any unset fields in the calendar fields.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected abstract void computeFields ()

Converts the current millisecond time value time to calendar field values in fields[].

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected abstract void computeTime ()

Converts the current calendar field values in fields[] to the millisecond time value time.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

boolean equals (Object obj)

Compares this Calendar to the specified Object.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

System.out.printf("%tF%n", calendar); // 2100-01-01

final var calendar2 = new Calendar.Builder()
        .setDate(2099, Calendar.JANUARY, 1)
        .build();

final var calendar3 = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

final var calendar4 = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 2)
        .build();

System.out.printf("%tF%n", calendar2); // 2099-01-01
System.out.printf("%tF%n", calendar3); // 2100-01-01
System.out.printf("%tF%n", calendar4); // 2100-01-02

System.out.println(calendar.equals(calendar2)); // false
System.out.println(calendar.equals(calendar3)); // true
System.out.println(calendar.equals(calendar4)); // false

int get (int field)

Returns the value of the given calendar field.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.DECEMBER, 31)
        .setTimeOfDay(14, 30, 59)
        .build();

System.out.printf("%tF %tT%n", calendar, calendar); // 2100-12-31 14:30:59

System.out.println(calendar.get(Calendar.YEAR)); // 2100
System.out.println(calendar.get(Calendar.MONTH) == Calendar.DECEMBER); // true
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 14
System.out.println(calendar.get(Calendar.MINUTE)); //30
System.out.println(calendar.get(Calendar.SECOND)); // 59

int getActualMaximum (int field)

Returns the maximum value that the specified calendar field could have, given the time value of this Calendar.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

System.out.println(calendar.getCalendarType()); // gregory
System.out.printf("%tF%n", calendar); // 2100-01-01

{
    System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
    System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 31

    System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
    System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31

    System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
    System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}

calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
System.out.printf("%tF%n", calendar); // 2100-02-01

{
    System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
    System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 28

    System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
    System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31

    System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
    System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}

int getActualMinimum (int field)

Returns the minimum value that the specified calendar field could have, given the time value of this Calendar.

Please see getActualMaximum(int field).

static Set<String> getAvailableCalendarTypes ()

Returns an unmodifiable Set containing all calendar types supported by Calendar in the runtime environment.

final var types = Calendar.getAvailableCalendarTypes();
for (final var type : types) {
    System.out.println(type);
}

// Result
// ↓
//gregory
//buddhist
//japanese

static Locale[] getAvailableLocales ()

Returns an array of all locales for which the getInstance methods of this class can return localized instances.

final var locales = Calendar.getAvailableLocales();
Arrays.sort(locales, Comparator.comparing(Locale::toLanguageTag));

for (final var locale : locales) {
    System.out.println(locale);
}

// Result
// ↓
//af
//af_ZA_#Latn
//af_NA
//af_ZA
//agq
//agq_CM
//agq_CM_#Latn
//ak
//ak_GH
//ak_GH_#Latn
//am
//...

String getCalendarType ()

Returns the calendar type of this Calendar.

final var calendar = new Calendar.Builder().build();

final var type = calendar.getCalendarType();
System.out.println(type); // gregory

String getDisplayName (int field, int style, Locale locale)

Returns the string representation of the calendar field value in the given style and locale.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

final var longName = calendar.getDisplayName(
        Calendar.MONTH, Calendar.LONG, Locale.US);
System.out.println(longName); // January

final var shortName = calendar.getDisplayName(
        Calendar.MONTH, Calendar.SHORT, Locale.US);
System.out.println(shortName); // Jan

final var jpName = calendar.getDisplayName(
        Calendar.MONTH, Calendar.LONG, Locale.JAPAN);
System.out.println(jpName); // 1月

Map<String,Integer> getDisplayNames (int field, int style, Locale locale)

Returns a Map containing all names of the calendar field in the given style and locale and their corresponding field values.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

final var longNames = calendar.getDisplayNames(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);

// {Monday=2, Thursday=5, Friday=6, Sunday=1, Wednesday=4, Tuesday=3, Saturday=7}
System.out.println(longNames);

final var shortNames = calendar.getDisplayNames(
        Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);

// {Thu=5, Tue=3, Wed=4, Sat=7, Fri=6, Sun=1, Mon=2}
System.out.println(shortNames);

final var jpNames = calendar.getDisplayNames(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.JAPAN);

// {木曜日=5, 日曜日=1, 火曜日=3, 金曜日=6, 土曜日=7, 月曜日=2, 水曜日=4}
System.out.println(jpNames);

int getFirstDayOfWeek ()

Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .setLocale(Locale.US)
        .build();

final var ret = calendar.getFirstDayOfWeek();
System.out.println(ret == Calendar.SUNDAY); // true
final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .setLocale(Locale.FRANCE)
        .build();

final var ret = calendar.getFirstDayOfWeek();
System.out.println(ret == Calendar.MONDAY); // true

abstract int getGreatestMinimum (int field)

Returns the highest minimum value for the given calendar field of this Calendar instance.

Please see getActualMaximum(int field).

static Calendar getInstance ()

Gets a calendar using the default time zone and locale.

final var calendar = Calendar.getInstance();

System.out.printf("%tF %tT%n", calendar, calendar); // 2023-09-17 16:18:34

static Calendar getInstance (Locale aLocale)

Gets a calendar using the default time zone and specified locale.

Please see also : getInstance()

final var calendar = Calendar.getInstance(Locale.US);

final var ret = calendar.getFirstDayOfWeek();
System.out.println(ret == Calendar.SUNDAY); // true
final var calendar = Calendar.getInstance(Locale.FRANCE);

final var ret = calendar.getFirstDayOfWeek();
System.out.println(ret == Calendar.MONDAY); // true

static Calendar getInstance (TimeZone zone)

Gets a calendar using the specified time zone and default locale.

Please see also : getInstance()

final var zone = TimeZone.getTimeZone("America/Los_Angeles");
final var calendar = Calendar.getInstance(zone);

System.out.printf("%tz%n", calendar); // -0700
System.out.println(calendar.get(Calendar.ZONE_OFFSET)); // -28800000

static Calendar getInstance (TimeZone zone, Locale aLocale)

Gets a calendar with the specified time zone and locale.

Please see :

abstract int getLeastMaximum (int field)

Returns the lowest maximum value for the given calendar field of this Calendar instance.

Please see getActualMaximum(int field).

abstract int getMaximum (int field)

Returns the maximum value for the given calendar field of this Calendar instance.

Please see getActualMaximum(int field).

int getMinimalDaysInFirstWeek ()

Gets what the minimal days required in the first week of the year are; e.g., if the first week is defined as one that contains the first day of the first month of a year, this method returns 1.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();

{
    final var name = calendar.getDisplayName(
            Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
    System.out.println(name); // Friday

    System.out.println(calendar.getMinimalDaysInFirstWeek()); // 1
    System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 1

    calendar.setMinimalDaysInFirstWeek(7);

    System.out.println(calendar.getMinimalDaysInFirstWeek()); // 7
    System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 52
}

abstract int getMinimum (int field)

Returns the minimum value for the given calendar field of this Calendar instance.

Please see getActualMaximum(int field).

final Date getTime ()

Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch").

final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .setTimeZone(zone)
        .build();

System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.getTimeInMillis()); // 4102444800000

final var date = calendar.getTime();
System.out.println(date.getTime()); // 4102444800000

long getTimeInMillis ()

Returns this Calendar's time value in milliseconds.

Please see getTime().

TimeZone getTimeZone ()

Gets the time zone.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.AUGUST, 1)
        .setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
        .build();

final var zone1 = calendar.getTimeZone();
System.out.println(zone1.getID()); // America/Los_Angeles

calendar.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));

final var zone2 = calendar.getTimeZone();
System.out.println(zone2.getID()); // Asia/Tokyo

int getWeeksInWeekYear ()

Returns the number of weeks in the week year represented by this Calendar.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .build();
System.out.println(calendar.getCalendarType()); // gregory

if (calendar.isWeekDateSupported()) {
    System.out.println(calendar.getWeeksInWeekYear()); // 52
    System.out.println(calendar.getWeekYear()); // 2100
}

int getWeekYear ()

Returns the week year represented by this Calendar.

Please see getWeeksInWeekYear().

int hashCode ()

Returns a hash code for this calendar.

final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
        .setTimeZone(zone)
        .build();

System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00
System.out.println(calendar.hashCode()); // 577755

calendar.set(2100, Calendar.JANUARY, 1);

System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.hashCode()); // 751504224

protected final int internalGet (int field)

Returns the value of the given calendar field.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

boolean isLenient ()

Tells whether date/time interpretation is to be lenient.

final var calendar = new Calendar.Builder().build();
System.out.println(calendar.isLenient()); // true

{
    calendar.set(2100, Calendar.JANUARY, 100);
    final var date = calendar.getTime();
    System.out.printf("%tF%n", date); // 2100-04-10
}

calendar.setLenient(false);
System.out.println(calendar.isLenient()); // false

try {
    calendar.set(2100, Calendar.JANUARY, 100);
    final var date = calendar.getTime();
} catch (IllegalArgumentException e) {
    System.out.println("IllegalArgumentException! : " + e.getMessage());
}

// Result
// ↓
//IllegalArgumentException! : DAY_OF_MONTH

final boolean isSet (int field)

Determines if the given calendar field has a value set, including cases that the value has been set by internal fields calculations triggered by a get method call.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.AUGUST, 15)
        .build();

System.out.printf("%tF%n", calendar); // 2100-08-15

System.out.println(calendar.isSet(Calendar.YEAR)); // true
System.out.println(calendar.isSet(Calendar.MONTH)); // true
System.out.println(calendar.isSet(Calendar.DAY_OF_MONTH)); // true

calendar.clear(Calendar.YEAR);
System.out.printf("%tF%n", calendar); // 1970-08-15

System.out.println(calendar.isSet(Calendar.YEAR)); // false
System.out.println(calendar.isSet(Calendar.MONTH)); // true
System.out.println(calendar.isSet(Calendar.DAY_OF_MONTH)); // true

boolean isWeekDateSupported ()

Returns whether this Calendar supports week dates.

Please see getWeeksInWeekYear().

abstract void roll (int field, boolean up)

Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.OCTOBER, 1)
        .build();

System.out.printf("%tF%n", calendar); // 2100-10-01

calendar.roll(Calendar.MONTH, true);
System.out.printf("%tF%n", calendar); // 2100-11-01

calendar.roll(Calendar.MONTH, true);
System.out.printf("%tF%n", calendar); // 2100-12-01

calendar.roll(Calendar.MONTH, true);
System.out.printf("%tF%n", calendar); // 2100-01-01
final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 2)
        .build();

System.out.printf("%tF%n", calendar); // 2100-01-02

calendar.roll(Calendar.DAY_OF_MONTH, false);
System.out.printf("%tF%n", calendar); // 2100-01-01

calendar.roll(Calendar.DAY_OF_MONTH, false);
System.out.printf("%tF%n", calendar); // 2100-01-31

void roll (int field, int amount)

Adds the specified (signed) amount to the specified calendar field without changing larger fields.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JULY, 1)
        .build();

System.out.printf("%tF%n", calendar); // 2100-07-01

calendar.roll(Calendar.MONTH, 3);
System.out.printf("%tF%n", calendar); // 2100-10-01

calendar.roll(Calendar.MONTH, 2);
System.out.printf("%tF%n", calendar); // 2100-12-01

calendar.roll(Calendar.MONTH, 1);
System.out.printf("%tF%n", calendar); // 2100-01-01
final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 3)
        .build();

System.out.printf("%tF%n", calendar); // 2100-01-03

calendar.roll(Calendar.DAY_OF_MONTH, -2);
System.out.printf("%tF%n", calendar); // 2100-01-01

calendar.roll(Calendar.DAY_OF_MONTH, -1);
System.out.printf("%tF%n", calendar); // 2100-01-31

void set (int field, int value)

Sets the given calendar field to the given value.

final var calendar = new Calendar.Builder().build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00

calendar.set(Calendar.YEAR, 2100);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00

calendar.set(Calendar.MONTH, Calendar.AUGUST);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-01 00:00:00

calendar.set(Calendar.DAY_OF_MONTH, 15);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 00:00:00

calendar.set(Calendar.HOUR_OF_DAY, 14);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 14:00:00

calendar.set(Calendar.MINUTE, 30);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 14:30:00

calendar.set(Calendar.SECOND, 59);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 14:30:59

final void set (int year, int month, int date)

Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

final var calendar = new Calendar.Builder().build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00

calendar.set(2100, Calendar.AUGUST, 15);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 00:00:00

final void set (int year, int month, int date, int hourOfDay, int minute)

Sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.

final var calendar = new Calendar.Builder().build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00

calendar.set(2100, Calendar.AUGUST, 15, 14, 30);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 14:30:00

final void set (int year, int month, int date, int hourOfDay, int minute, int second)

Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, and SECOND.

final var calendar = new Calendar.Builder().build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00

calendar.set(2100, Calendar.AUGUST, 15, 14, 30, 59);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 14:30:59

void setFirstDayOfWeek (int value)

Sets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 3)
        .setLocale(Locale.US)
        .build();

final var name = calendar.getDisplayName(
        Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Sunday

System.out.println(calendar.getFirstDayOfWeek() == Calendar.SUNDAY); // true
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2

calendar.setFirstDayOfWeek(Calendar.MONDAY);

System.out.println(calendar.getFirstDayOfWeek() == Calendar.MONDAY); // true
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1

void setLenient (boolean lenient)

Specifies whether or not date/time interpretation is to be lenient.

Please see isLenient().

void setMinimalDaysInFirstWeek (int value)

Sets what the minimal days required in the first week of the year are; For example, if the first week is defined as one that contains the first day of the first month of a year, call this method with value 1.

Please see getMinimalDaysInFirstWeek().

final void setTime (Date date)

Sets this Calendar's time with the given Date.

final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
        .setTimeZone(zone)
        .build();

System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00

final var date = new Date(4102444800000L);
calendar.setTime(date);

System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.getTimeInMillis()); // 4102444800000

void setTimeInMillis (long millis)

Sets this Calendar's current time from the given long value.

final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
        .setTimeZone(zone)
        .build();

System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00

calendar.setTimeInMillis(4102444800000L);

System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.getTimeInMillis()); // 4102444800000

void setTimeZone (TimeZone value)

Sets the time zone with the given time zone value.

Please see getTimeZone().

void setWeekDate (int weekYear, int weekOfYear, int dayOfWeek)

Sets the date of this Calendar with the given date specifiers - week year, week of year, and day of week.

final var calendar = new Calendar.Builder().build();
System.out.printf("%tF%n", calendar); // 1970-01-01

calendar.setWeekDate(2100, 1, Calendar.FRIDAY);
System.out.printf("%tF%n", calendar); // 2100-01-01

calendar.setWeekDate(2100, 1, Calendar.SATURDAY);
System.out.printf("%tF%n", calendar); // 2100-01-02

calendar.setWeekDate(2100, 2, Calendar.SATURDAY);
System.out.printf("%tF%n", calendar); // 2100-01-09

calendar.setWeekDate(2100, 10, Calendar.MONDAY);
System.out.printf("%tF%n", calendar); // 2100-03-01

final Instant toInstant ()

Converts this object to an Instant.

final var zone = TimeZone.getTimeZone("UTC");

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .setTimeZone(zone)
        .build();

System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.get(Calendar.ZONE_OFFSET)); // 0

final var instant = calendar.toInstant();
System.out.println(instant); // 2100-01-01T00:00:00Z

String toString ()

Return a string representation of this calendar.

final var zone = TimeZone.getTimeZone("UTC");

final var calendar = new Calendar.Builder()
        .setDate(2100, Calendar.JANUARY, 1)
        .setTimeZone(zone)
        .build();

final var str = calendar.toString();

// java.util.GregorianCalendar[time=4102444800000,areFieldsSet=true,
// areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo
// [id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],
// firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2100,MONTH=0,
// WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=1,DAY_OF_WEEK=6,
// DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,
// MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
System.out.println(str);

Related posts

To top of page