Java Calendar using Calendar.Builder by Examples

Tags: Calendar Java Calendar

In this Java core tutorial we learn how to create Calendar objects using the java.util.Calendar.Builder class via different Java example programs.

Table of contents

  1. Create Calendar object from milliseconds
  2. Create Calendar object from Date object
  3. Create Calendar object from day month year hour minute and second
  4. Create Calendar object using setFields() method
  5. Create Calendar object using setDate() method
  6. Create Calendar object with date and time
  7. Create Calendar object with time zone
  8. Create Calendar object with calendar type

Create Calendar object from milliseconds

In the following Java program we show how to use the Builder.setInstant() method to build a Calendar object with a given milliseconds value.

CalendarBuilderExample1.java

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarBuilderExample1 {
    public static void main(String... args) {
        long milliseconds = System.currentTimeMillis();

        Calendar calendar = new Calendar.Builder()
                .setInstant(milliseconds)
                .build();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(calendar.getTime()));
    }
}
The output as below.
2022/04/14 23:46:30

Create Calendar object from Date object

The Calendar.Builder class also provides method to build Calendar object from a Date object as following Java program.

CalendarBuilderExample2.java

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarBuilderExample2 {
    public static void main(String... args) {
        Date date = new Date();

        Calendar calendar = new Calendar.Builder()
                .setInstant(date)
                .build();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(calendar.getTime()));
    }
}
The output as below.
2022/04/14 23:49:12

Create Calendar object from day month year hour minute and second

In the following Java program we learn how to use the Builder.set() method to build Calendar object from specified date and time values.

CalendarBuilderExample3.java

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarBuilderExample3 {
    public static void main(String... args) {
        Calendar calendar = new Calendar.Builder()
                .set(Calendar.DAY_OF_MONTH, 14)
                .set(Calendar.MONTH, Calendar.APRIL)
                .set(Calendar.YEAR, 2022)
                .set(Calendar.HOUR, 8)
                .set(Calendar.MINUTE, 30)
                .set(Calendar.SECOND, 40)
                .build();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(calendar.getTime()));
    }
}
The output as below.
2022/04/14 08:30:40

Create Calendar object using setFields() method

We also can use Builder.setFields() method to set date and time with pairs of field key and its value as following Java code.

CalendarBuilderExample4.java

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarBuilderExample4 {
    public static void main(String... args) {
        Calendar calendar = new Calendar.Builder()
                .setFields(
                        Calendar.DAY_OF_MONTH, 14,
                        Calendar.MONTH, Calendar.APRIL,
                        Calendar.YEAR, 2022,
                        Calendar.HOUR, 8,
                        Calendar.MINUTE, 30,
                        Calendar.SECOND, 40)
                .build();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(calendar.getTime()));
    }
}
The output as below.
2022/04/14 08:30:40

Create Calendar object using setDate() method

The Calendar.Builder class also provides setDate() to build Calendar with day month year value as below.

CalendarBuilderExample5.java

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarBuilderExample5 {
    public static void main(String... args) {
        int year = 2022;
        int month = Calendar.MAY;
        int dayOfMonth = 14;
        Calendar calendar = new Calendar.Builder()
                .setDate(year, month, dayOfMonth)
                .build();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(calendar.getTime()));
    }
}
The output as below.
2022/05/14 00:00:00

Create Calendar object with date and time

The following Java program to show how to use Builder.setDate() and Builder.setTimeOfDay() method to build Calendar with given date and time values.

CalendarBuilderExample6.java

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarBuilderExample6 {
    public static void main(String... args) {
        int year = 2022;
        int month = Calendar.APRIL;
        int dayOfMonth = 14;
        int hourOfDay = 10;
        int minute = 20;
        int second = 30;

        Calendar calendar = new Calendar.Builder()
                .setDate(year, month, dayOfMonth)
                .setTimeOfDay(hourOfDay, minute, second)
                .build();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(calendar.getTime()));
    }
}
The output as below.
2022/04/14 10:20:30

Create Calendar object with time zone

We also can build Calendar object with given time zone using Builder.setTimeZone() method as the following Java program.

CalendarBuilderExample7.java

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class CalendarBuilderExample7 {
    public static void main(String... args) {
        int year = 2022;
        int month = Calendar.APRIL;
        int dayOfMonth = 14;
        int hourOfDay = 10;
        int minute = 20;
        int second = 30;
        TimeZone timeZone = TimeZone.getTimeZone("America/New_York");

        Calendar calendar = new Calendar.Builder()
                .setDate(year, month, dayOfMonth)
                .setTimeOfDay(hourOfDay, minute, second)
                .setTimeZone(timeZone)
                .build();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(calendar.getTime()));
        System.out.println(calendar.getTimeZone());
    }
}
The output as below.
2022/04/14 21:20:30
sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

Create Calendar object with calendar type

In the following Java program we use Builder.setCalendarType() method to build Calendar object with given calendar type.

CalendarBuilderExample8.java

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarBuilderExample8 {
    public static void main(String... args) {
        int year = 2022;
        int month = Calendar.JULY;
        int dayOfMonth = 11;
        int hourOfDay = 10;
        int minute = 20;
        int second = 30;

        Calendar calendar = new Calendar.Builder()
                .setDate(year, month, dayOfMonth)
                .setTimeOfDay(hourOfDay, minute, second)
                .setCalendarType("buddhist")
                .build();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(calendar.getTime()));
        System.out.println(calendar.getCalendarType());
    }
}
The output as below.
1479/07/11 10:20:30
buddhist

Happy Coding 😊

Java LocalDate by Examples

Java LocalTime by Examples

Java LocalDateTime by Examples

Java Date by Examples

Java Calendar by Examples

Java Convert Calendar to Date

Java Convert Calendar to Milliseconds

Java Convert Calendar to Instant

Java Convert Calendar to String

Java Get Current Date and Time