Java Programming Tutorials

Java programming tutorials with many code examples!

Java Calendar

Problem:

How to create Java Calendar instances and display its details? In the following example we’ll show how to create Calendar in an old way and using the new Java 8 Builder.

Solution:

Basic class in Java for calculating time is java.util.Calendar. Traditionally its instances were created using getInstance factory methods, but Java 8 came with a new Calendar.Builder class that allows to set also calendar type for the new calendar.

The following program demonstrates how to create instances of Calendar class and prints some of its details:

package com.farenda.java.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class CalendarGetInstance {

    public static void main(String[] args) {
        calendarWithDefaults();

        calendarWithTimeZoneAndLocale();

        createCalendarUsingJava8Builder();

        availableCalendars();
    }

    private static void calendarWithDefaults() {
        System.out.println("Calendar with default TimeZone and Locale");
        printDetails(Calendar.getInstance());
    }

    private static void calendarWithTimeZoneAndLocale() {
        System.out.println("Calendar with EST TimeZone and Italian Locale");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("EST"), Locale.ITALIAN);
        printDetails(calendar);
    }

    private static void createCalendarUsingJava8Builder() {
        System.out.println("Create using Java 8 Calendar.Builder");
        printDetails(new Calendar.Builder().setCalendarType("gregory").build());
    }

    private static void availableCalendars() {
        System.out.println("Available calendars: " + Calendar.getAvailableCalendarTypes());
        for (String type : Calendar.getAvailableCalendarTypes()) {
            Calendar calendar = new Calendar.Builder()
                    .setCalendarType(type)
                    .setInstant(new Date())
                    .build();
            printDetails(calendar);
        }
    }

    private static void printDetails(Calendar calendar) {
        System.out.println("Calendar type: " + calendar.getCalendarType());
        System.out.println("First day of the week: " + calendar.getFirstDayOfWeek());
        System.out.println("Time since the Epoch: " + calendar.getTime());
        System.out.println("Current TimeZone: " + calendar.getTimeZone().getDisplayName());
        try {
            System.out.println("Weeks in year: " + calendar.getWeeksInWeekYear());
        } catch (UnsupportedOperationException e) {
            System.out.println("Weeks in year: UnsupportedOperationException");
        }
        try {
            System.out.println("The week year: " + calendar.getWeekYear());
        } catch (UnsupportedOperationException e) {
            System.out.println("The week year: UnsupportedOperationException");
        }
        System.out.println("As instant: " + calendar.toInstant());
        System.out.println();
    }
}

And here’s the result of running the above program:

Calendar with default TimeZone and Locale
Calendar type: gregory
First day of the week: 2
Time since the Epoch: Sun Sep 27 10:14:59 CEST 2015
Current TimeZone: Central European Time
Weeks in year: 53
The week year: 2015
As instant: 2015-09-27T08:14:59.195Z

Calendar with EST TimeZone and Italian Locale
Calendar type: gregory
First day of the week: 2
Time since the Epoch: Sun Sep 27 10:14:59 CEST 2015
Current TimeZone: Eastern Standard Time
Weeks in year: 53
The week year: 2015
As instant: 2015-09-27T08:14:59.701Z

Create using Java 8 Calendar.Builder
Calendar type: gregory
First day of the week: 2
Time since the Epoch: Thu Jan 01 00:00:00 CET 1970
Current TimeZone: Central European Time
Weeks in year: 53
The week year: 1970
As instant: 1969-12-31T23:00:00Z

Available calendars: [gregory, buddhist, japanese]
Calendar type: gregory
First day of the week: 2
Time since the Epoch: Sun Sep 27 10:14:59 CEST 2015
Current TimeZone: Central European Time
Weeks in year: 53
The week year: 2015
As instant: 2015-09-27T08:14:59.706Z

Calendar type: buddhist
First day of the week: 2
Time since the Epoch: Sun Sep 27 10:14:59 CEST 2015
Current TimeZone: Central European Time
Weeks in year: 53
The week year: 2558
As instant: 2015-09-27T08:14:59.711Z

Calendar type: japanese
First day of the week: 2
Time since the Epoch: Sun Sep 27 10:14:59 CEST 2015
Current TimeZone: Central European Time
Weeks in year: UnsupportedOperationException
The week year: UnsupportedOperationException
As instant: 2015-09-27T08:14:59.724Z

As you can see there are 3 types of Calendar on my system: gregory, buddhist, japanese. Each of them calculates time differently.

Another thing to note is that Calendar created with Calendar.getInstance() Factory Method is initialized with current time, but the one created with Calendar.Builder has to have time set explicitly.

Share with the World!