Previous in the Series
Current Tutorial
Legacy Date-Time Code
That's the end of the series!

Previous in the Series: Non-ISO Date Conversion

Legacy Date-Time Code

Prior to the Java SE 8 release, the Java date and time mechanism was provided by the java.util.Date, java.util.Calendar, and java.util.TimeZone classes, as well as their subclasses, such as java.util.GregorianCalendar. These classes had several drawbacks, including:

  • The Calendar class was not type safe.
  • Because the classes were mutable, they could not be used in multithreaded applications.
  • Bugs in application code were common due to the unusual numbering of months and the lack of type safety.

 

Interoperability with Legacy Code

Perhaps you have legacy code that uses the java.util date and time classes and you would like to take advantage of the java.time functionality with minimal changes to your code.

Added to the JDK 8 release are several methods that allow conversion between java.util and java.time objects:

The following example converts a Calendar instance to a ZonedDateTime instance. Note that a time zone must be supplied to convert from an Instant to a ZonedDateTime:

Calendar now = Calendar.getInstance();
ZonedDateTime zdt = ZonedDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault()));

The following example shows conversion between a Date and an Instant:

Instant inst = date.toInstant();

Date newDate = Date.from(inst);

The following example converts from a GregorianCalendar to a ZonedDateTime, and then from a ZonedDateTime to a GregorianCalendar. Other temporal-based classes are created using the ZonedDateTime instance:

GregorianCalendar cal = ...;

TimeZone tz = cal.getTimeZone();
int tzoffset = cal.get(Calendar.ZONE_OFFSET);

ZonedDateTime zdt = cal.toZonedDateTime();

GregorianCalendar newCal = GregorianCalendar.from(zdt);

LocalDateTime ldt = zdt.toLocalDateTime();
LocalDate date = zdt.toLocalDate();
LocalTime time = zdt.toLocalTime();

 

Mapping Legacy Date and Time Functionality to the Date Time API

Because the Java implementation of date and time has been completely redesigned in the Java SE 8 release, you cannot swap one method for another method. If you want to use the rich functionality offered by the java.time package, your easiest solution is to use the toInstant() or toZonedDateTime() methods listed in the previous section. However, if you do not want to use that approach or it is not sufficient for your needs, then you must rewrite your date-time code.

The table introduced on the Overview page is a good place to begin evaluating which java.time classes meet your needs.

There is no one-to-one mapping correspondence between the two APIs, but the following table gives you a general idea of which functionality in the java.util date and time classes maps to the java.time APIs.

Correspondence between legacy Date and Instant

The Instant and Date classes are similar. Each class:

  • Represents an instantaneous point of time on the timeline (UTC)
  • Holds a time independent of a time zone
  • Is represented as epoch-seconds (since 1970-01-01T00:00:00Z) plus nanoseconds

The Date.from(Instant) and Date.toInstant() methods allow conversion between these classes.

Correspondence between GregorianCalendar and ZonedDateTime

The ZonedDateTime class is the replacement for GregorianCalendar. It provides the following similar functionality. Human time representation is as follows:

The GregorianCalendar.from(ZonedDateTime) and GregorianCalendar.toZonedDateTime() methods facilitate conversions between these classes.

Correspondence between legacy TimeZone and ZoneId or ZoneOffset

The ZoneId class specifies a time zone identifier and has access to the rules used each time zone. The ZoneOffset class specifies only an offset from Greenwich/UTC. For more information, see Time Zone and Offset Classes.

Correspondence between GregorianCalendar with the date set to 1970-01-01 and LocalTime

Code that sets the date to 1970-01-01 in a GregorianCalendar instance in order to use the time components can be replaced with an instance of LocalTime.

Correspondence between GregorianCalendar with time set to 00:00 and LocalDate

Code that sets the time to 00:00 in a GregorianCalendar instance in order to use the date components can be replaced with an instance of LocalDate. (This GregorianCalendar approach was flawed, as midnight does not occur in some countries once a year due to the transition to daylight saving time.)

 

Date and Time Formatting

Although the java.time.format.DateTimeFormatter provides a powerful mechanism for formatting date and time values, you can also use the java.time temporal-based classes directly with java.util.Formatter and String.format(), using the same pattern-based formatting that you use with the java.util date and time classes.


Last update: January 27, 2022


Previous in the Series
Current Tutorial
Legacy Date-Time Code
That's the end of the series!

Previous in the Series: Non-ISO Date Conversion