Java - Converting a GregorianCalendar to New Datetime Types and Vice Versa

Introduction

You can convert GregorianCalendar to a ZonedDateTime, which can be converted to any other classes in the new Date-Time API.

Calendar class provides a toInstant() method to convert its instance to an Instant.

Converting an Instant to a GregorianCalendar would be a two-step process:

  • Convert the Instant to a ZonedDateTime,
  • Use the from() static method of the GregorianCalendar class to get a GregorianCalendar.

Demo

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
  public static void main(String[] args) {
    // Create a GC for the default time zone
    GregorianCalendar gc = new GregorianCalendar(2018, 1, 11, 15, 45, 50);
    System.out.println("Gregorian Calendar: " + gc.getTime());

    // Convert the GC to a LocalDate
    LocalDate ld = gc.toZonedDateTime().toLocalDate();
    System.out.println("Local Date: " + ld);

    // Convert the GC to a LocalTime
    LocalTime lt = gc.toZonedDateTime().toLocalTime();
    System.out.println("Local Time: " + lt);

    // Convert the GC to a LocalDateTime
    LocalDateTime ldt = gc.toZonedDateTime().toLocalDateTime();
    System.out.println("Local DateTime: " + ldt);

    // Convert the GC to an OffsetDate
    OffsetDateTime od = gc.toZonedDateTime().toOffsetDateTime();
    System.out.println("Offset Date: " + od);

    // Convert the GC to an OffsetTime
    OffsetTime ot = gc.toZonedDateTime().toOffsetDateTime().toOffsetTime();
    System.out.println("Offset Time: " + ot);

    // Convert the GC to an ZonedDateTime
    ZonedDateTime zdt = gc.toZonedDateTime();
    System.out.println("Zoned DateTime: " + zdt);

    // Convert the ZonedDateTime to a GC. In GC month starts at 0
    // and in new API at 1
    ZoneId zoneId = zdt.getZone();
    TimeZone timeZone = TimeZone.getTimeZone(zoneId);
    System.out.println("Zone ID: " + zoneId);
    System.out.println("Time Zone ID: " + timeZone.getID());

    GregorianCalendar gc2 = GregorianCalendar.from(zdt);
    System.out.println("Gregorian Calendar: " + gc2.getTime());
  }// w  w  w. j a v a  2s  . co  m
}

Result

Related Topic