Converting java.util.GregorianCalendar to java.time.ZonedDateTime

This section provides a tutorial example how to migrate Java code from the old java.util.GregorianCalendar class to the new java.time.ZonedDateTime class.

If you are currently using the old java.util.GregorianCalendar class to manipulate time measured as date, time, and timezone component, you should migrate your code to use the new java.time.ZonedDateTime class.

If you are using both classes in your code, you may use two new methods provided on java.util.GregorianCalendar class:

The following sample program provides some comparison between java.util.GregorianCalendar and java.time.ZonedDateTime and might be helpful to you in migrating your code:

/* CalendarClassMigration.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.util.GregorianCalendar;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.time.temporal.ChronoField;
class CalendarClassMigration {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;
      GregorianCalendar oCalendar = new GregorianCalendar();
      ZonedDateTime oDateTime = ZonedDateTime.now();

      out.println();
      out.println("Epoch seconds:");
      out.println("GregorianCalendar class: "
         + (long) oCalendar.getTimeInMillis()/1000);
      out.println("ZonedDateTime class: "
         + oDateTime.getLong(ChronoField.INSTANT_SECONDS));

      out.println();
      out.println("Epoch milliseconds:");
      out.println("GregorianCalendar class: "
         + oCalendar.getTimeInMillis());
      out.println("ZonedDateTime class: "
         + (oDateTime.getLong(ChronoField.INSTANT_SECONDS)*1000
            +oDateTime.getLong(ChronoField.MILLI_OF_SECOND)));

      out.println();
      out.println("Nanoseconds-of-Second:");
      out.println("GregorianCalendar class: Not supported");
      out.println("ZonedDateTime class: " + oDateTime.getNano());

      out.println();
      out.println("Calendar date:");
      out.println("GregorianCalendar class: "
         + oCalendar.get(Calendar.YEAR) + "-"
         + (oCalendar.get(Calendar.MONTH)+1) + "-"
         + oCalendar.get(Calendar.DAY_OF_MONTH));
      out.println("ZonedDateTime class: "
         + oDateTime.getLong(ChronoField.YEAR) + "-"
         + oDateTime.getLong(ChronoField.MONTH_OF_YEAR) + "-"
         + oDateTime.getLong(ChronoField.DAY_OF_MONTH));

      out.println();
      out.println("Timezone ID:");
      out.println("GregorianCalendar class: "
         + oCalendar.getTimeZone().getID());
      out.println("ZonedDateTime class: "
         + oDateTime.getZone());

      out.println();
      out.println("UTC offset:");
      out.println("GregorianCalendar class: "
         + (oCalendar.get(Calendar.ZONE_OFFSET)
            +oCalendar.get(Calendar.DST_OFFSET))/(60*60*1000));
      out.println("ZonedDateTime class: "
         + oDateTime.getOffset());

      out.println();
      out.println("String presentation:");
      out.println("GregorianCalendar class: "
         + oCalendar.getTime().toString());
      out.println("ZonedDateTime class: " + oDateTime.toString());

      out.println();
      out.println("Converting GregorianCalendar to ZonedDateTime:");
      ZonedDateTime cDateTime = oCalendar.toZonedDateTime();
      out.println("GregorianCalendar class: "
         + oCalendar.getTime().toString());
      out.println("ZonedDateTime class: " + cDateTime.toString());

      out.println();
      out.println("Converting GregorianCalendar from ZonedDateTime:");
      GregorianCalendar cCalendar = GregorianCalendar.from(oDateTime);
      out.println("GregorianCalendar class: "
         + cCalendar.getTime().toString());
      out.println("ZonedDateTime class: " + oDateTime.toString());
   }
}

When running CalendarClassMigration.java, I got the following output:

herong> java CalendarClassMigration.java

Epoch seconds:
GregorianCalendar class: 1708372665
ZonedDateTime class: 1708372665

Epoch milliseconds:
GregorianCalendar class: 1708372665235
ZonedDateTime class: 1708372665244

Nanoseconds-of-Second:
GregorianCalendar class: Not supported
ZonedDateTime class: 244850000

Calendar date:
GregorianCalendar class: 2024-2-19
ZonedDateTime class: 2024-2-19

Timezone ID:
GregorianCalendar class: America/Argentina/Buenos_Aires
ZonedDateTime class: America/Argentina/Buenos_Aires

UTC offset:
GregorianCalendar class: -3
ZonedDateTime class: -03:00

String presentation:
GregorianCalendar class: Mon Feb 19 16:57:45 ART 2024
ZonedDateTime class: 2024-02-19T16:57:45.244850-03:00[America/...]

Converting GregorianCalendar to ZonedDateTime:
GregorianCalendar class: Mon Feb 19 16:57:45 ART 2024
ZonedDateTime class: 2024-02-19T16:57:45.235-03:00[America/...]

Converting GregorianCalendar from ZonedDateTime:
GregorianCalendar class: Mon Feb 19 16:57:45 ART 2024
ZonedDateTime class: 2024-02-19T16:57:45.244850-03:00[America/...]

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

Java Date-Time API

 What Is Date-Time API

 java.time.Instant - Representing a Moment of Time

 java.time.Instant Usage Examples

 java.time.Instant - get(INSTANT_SECONDS) Error

 Converting java.util.Date to java.time.Instant

 java.time.ZonedDateTime - Calendar and Timezone

 java.time.ZonedDateTime Usage Examples

Converting java.util.GregorianCalendar to java.time.ZonedDateTime

 java.time.OffsetDateTime - Calendar and UTC Offset

 java.time.OffsetDateTime Usage Examples

 java.time.LocalDateTime - Local Date and time without Timezone

 Partial Date and Time Objects and Classes

 java.time.format.DateTimeFormatter - Date-Time Strings

 java.time.Duration - Time Durations

 java.time.Duration Usage Examples

 java.time.Period - Periods in Days and Months

 java.time.Period Usage Examples

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Java Logging

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 Full Version in PDF/EPUB