Chapter 18. Date and Time API

The Date and Time API (JSR 310) provides support for date, time, and calendar calculations. The reference implementation (RI) for this JSR is the ThreeTen Project and was provided for inclusion into JDK 1.8. The Date and Time API is relative to the java.time package and java.time.chrono, java.time.format, java.time.temporal, and java.time.zone subpackages.

JSR 310 achieved several design goals:

  • Fluent API; easy-to-read (e.g., chained methods)
  • Thread-safe design; immutable value classes
  • Extensible API; calendar systems, adjusters, and queries
  • Expectable behavior

The Date and Time API uses the International Organization for Standardization date and time data exchange model (ISO 8601). The ISO 8601 standard is formally called “Data elements and interchange formats—Information interchange—Representation of dates and times.” The standard is based on the Gregorian calendar. Regional calendars are also supported.

See Appendix A for more information on fluent APIs.

Legacy Interoperability

JSR 310 supercedes but does not deprecate java.util.Date, java.util.Calendar, java.util.DateFormat, java.util.GregorianCalendar, java.util.TimeZone, and java.sql.Date. JDK 8 provides methods to these classes to convert to and from the JSR 310 types for legacy support.

// Legacy -> New -> Legacy
Calendar c = Calendar.getInstance();
Instant i =  c.toInstant();
Date d = Date.from(i);

// New -> Legacy -> New
 ZonedDateTime zdt
   = ZonedDateTime.parse("2014-02-24T11:17:00+01:00"
   +

Get Java 8 Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.