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

Java examples for Date Time:Calendar

Description

Converting a GregorianCalendar to New Datetime Types and Vice Versa

Demo Code

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(2017, 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());
  }/*www.  j  a va  2 s  .c  om*/
}

Result


Related Tutorials