Legacy Date-Time Code
Prior to the Java SE 8 release, the Java date and time mechanism was provided by the
java.util.TimeZone classes, as well as their subclasses, such as
java.util.GregorianCalendar. These classes had several drawbacks, including:
Calendarclass 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
Calendarobject to an
GregorianCalendarinstance to a
GregorianCalendarobject using the default locale from a
Dateobject from an
Dateobject to an
TimeZoneobject to a
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
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.
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
Correspondence between legacy Date and Instant
- 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
Correspondence between GregorianCalendar and ZonedDateTime
LocalDate: year, month, day
LocalTime: hours, minutes, seconds, nanoseconds
ZoneId: time zone
ZoneOffset: current offset from GMT
Correspondence between legacy TimeZone and ZoneId or ZoneOffset
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
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
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
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
String.format(), using the same pattern-based formatting that you use with the
java.util date and time classes.
Last update: January 27, 2022