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:

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.