Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java 101: The next generation: It's time for a change

Catching up with the Java Date and Time API

  • Print
  • Feedback

Page 2 of 7

Joda Time in JSR 310

Joda Time is an open source date and time API. Released by Stephen Colebourne in February 2005, it became a popular alternative to the standard Java date and time APIs. While the JSR 310: Date and Time API isn't Joda Time (due to various design flaws) it is heavily influenced by it. In fact, Colebourne is a JSR 310 co-lead.

Java Date and Time API overview

JSR 310: Date and Time API was developed to overcome numerous problems with Java's previous date and time APIs. As a result, it has been architected around a number of important design principles:

  • Immutability and thread safety: All of the Date and Time API's core classes are immutable, which ensures that you don't have to worry about threading issues caused by lack of thread synchronization. Immutable objects are simple to construct, use, and test, they make for good hash keys, and so on.
  • Fluency: Date and Time presents a fluent interface, which should make its methods more readable and easier to learn, especially when chained together. Fluent factory methods (e.g., now(), from(), and of-prefixed methods) are used as an alternative to constructors. You'll also be able to use with-prefixed methods if you need to return a copy of the current instance with additional information.
  • Clarity: Each method in the Date and Time API is well-defined and clear about what it accomplishes. Additionally, Date and Time rejects null arguments early. Unless otherwise noted, passing a null argument to a method in any class or interface will cause a NullPointerException to be thrown. Validation methods that take object arguments and return Boolean values are an exception: they generally return false when null is passed.
  • Extensibility: The Strategy design pattern is used throughout the API to allow for extension while avoiding confusion. For example, although Date and Time's classes are based on the ISO-8601 calendar system, you can also work with the non-ISO calendars (such as Japanese Imperial) that are included in the API, or you could even introduce your own calendar.

Date and Time is described by some 60 types (at the time of this writing) that are organized into a main package and four subpackages:

  • java.time presents classes that represent the principal date-time concepts: instants, durations, dates, times, time zones, partials, and periods. All of this package's classes are immutable and thread-safe.
    • java.time.chrono provides a generic API that describes calendar systems other than the default ISO-8601 calendar system.
    • java.time.format presents classes for formatting and parsing date-time objects.
    • java.time.temporal offers field, unit, or adjustment access to a temporal object such as a date.
    • java.time.zone presents classes that support time zones and their rules.

The types in the java.time package should serve most of your needs. You'll work directly with the types in the other four packages only when you need to go beyond java.time's default ISO-8601 calendar system.

Retrofitting the old date and time APIs

To ease the transition to the new Date and Time API, the old date and time APIs have been retrofitted to interoperate with Date and Time. For example, an Instant toInstant() method has been added to java.util.Date to convert a Date instance to an Instant instance.

Touring the Date and Time API

Date and Time's many types can seem overwhelming. However, you'll often work with only a subset of the types in the java.time package. The remainder of the article will be a tour of the java.time types that you are most likely to use: classes used to store and manipulate machine time, local date and time, and international time zones in your Java applications. For each of the API types I'll include a brief overview followed by one or more working applications that demonstrate how classes are instantiated, how instances are accessed, and how instances are manipulated to obtain new instances.

  • Print
  • Feedback

Resources

Recommended

  • "All I want for Java 8 ..." (Dusting Marx, April 2011): A developer's annotated wish list for Java 8.
  • "How badly do we want a new Java Date and Time API?" (Dustin Marx, March 2012) surveys developer opinion about the Java date and time infrastructure prior to Java 8.
  • JavaWorld syndicated blogger Ted Neward explains why he believes that Java 8 will be a game changer for Java development.
  • Martin Fowler discusses precision in his blog post about Time Point implementation in computer programming (MartinFowler.com, March 2004).

More about the Java Date and Time API

  • JSR 310: Date and Time API: The specification is the definitive source for learning about the new types in java.time.
  • Also visit the OpenJDK Project ThreeTen homepage, which includes a link to JSR 310's Sourceforge repository and a current release timeline.
  • JSR 310 co-lead and Joda Time creator Stephen Colebourne has written extensively on his blog about the relationship between Joda Time and the new Java Date and Time APIs.
  • In "From Instants to Eras, the Future of Java" (JW Blogs, October 2012) Dustin Marx reports from JavaOne 2012 on the potential impact of the new Date and Time API, as well as some of its core concepts.

Popular articles in the Java 101 series