- Legacy Date-Time Code
- Interoperability with Legacy Code
- Mapping java.util Date and Time Functionality to java.time
- Date and Time Formatting
- Lesson: Standard Calendar
- Overview
- DayOfWeek and Month Enums
- Date Classes
- Date and Time Classes
- Time Zone and Offset Classes
- Instant Class
- Parsing and Formatting
- The Temporal Package
- Period and Duration
- Clock
- Non-ISO Date Conversion
- Legacy Date-Time Code
- Summary
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:
- Calendar.toInstant() converts the Calendar object to an Instant.
- GregorianCalendar.toZonedDateTime() converts a GregorianCalendar instance to a ZonedDateTime.
- GregorianCalendar.from(ZonedDateTime) creates a GregorianCalendar object using the default locale from a ZonedDateTime instance.
- Date.from(Instant) creates a Date object from an Instant.
- Date.toInstant() converts a Date object to an Instant.
- TimeZone.toZoneId() converts a TimeZone object to a ZoneId.
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 java.util Date and Time Functionality to java.time
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.
java.util Functionality | java.time Functionality | Comments |
---|---|---|
java.util.Date | java.time.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. |
java.util.GregorianCalendar | java.time.ZonedDateTime | The ZonedDateTime class is the replacement for GregorianCalendar. It provides the following similar functionality. Human time representation is as follows: LocalDate: year, month, day LocalTime: hours, minutes, seconds, nanoseconds ZoneId: time zone ZoneOffset: current offset from GMT The GregorianCalendar.from(ZonedDateTime) and GregorianCalendar.to(ZonedDateTime) methods facilitate conversions between these classes. |
java.util.TimeZone | java.time.ZoneId or java.time.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. |
GregorianCalendar with the date set to 1970-01-01 | java.time.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. |
GregorianCalendar with time set to 00:00. | java.time.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.
Lesson: Standard Calendar
The core of the Date-Time API is the java.time package. The classes defined in java.time base their calendar system on the ISO calendar, which is the world standard for representing date and time. The ISO calendar follows the proleptic Gregorian rules. The Gregorian calendar was introduced in 1582; in the proleptic Gregorian calendar, dates are extended backwards from that time to create a consistent, unified timeline and to simplify date calculations.
This lesson covers the following topics:
Overview
This section compares the concepts of human time and machine time provides a table of the primary temporal-based classes in the java.time package.
DayOfWeek and Month Enums
This section discusses the enum that defines the days of the week (DayOfWeek) and the enum that defines months (Month).
Date Classes
This section shows the temporal-based classes that deal only with dates, without time or time zones. The four classes are LocalDate, YearMonth, MonthDay and Year.
Date and Time Classes
This section presents the LocalTime and LocalDateTime classes, which deal with time, and date and time, respectively, but without time zones.
Time Zone and Offset Classes
This section discusses the temporal-based classes that store time zone (or time zone offset) information, ZonedDateTime, OffsetDateTime, and OffsetTime. The supporting classes, ZoneId, ZoneRules, and ZoneOffset, are also discussed.
Instant Class
This section discusses the Instant class, which represents an instantaneous moment on the timeline.
Parsing and Formatting
This section provides an overview of how to use the predefined formatters to format and parse date and time values.
The Temporal Package
This section presents an overview of the java.time.temporal package, which supports the temporal classes, fields (TemporalField and ChronoField) and units (TemporalUnit and ChronoUnit). This section also explains how to use a temporal adjuster to retrieve an adjusted time value, such as «the first Tuesday after April 11», and how to perform a temporal query.
Period and Duration
This section explains how to calculate an amount of time, using both the Period and Duration classes, as well as the ChronoUnit.between method.
Clock
This section provides a brief overview of the Clock class. You can use this class to provide an alternative clock to the system clock.
Non-ISO Date Conversion
This section explains how to convert from a date in the ISO calendar system to a date in a non-ISO calendar system, such as a JapaneseDate or a ThaiBuddhistDate.
Legacy Date-Time Code
This section offers some tips on how to convert older java.util.Date and java.util.Calendar code to the Date-Time API.
Summary
This section provides a summary of the Standard Calendar lesson.