A blog about programming and software development topics, mostly focused on Java technologies including Java EE, Spring and Grails.

Monday, February 24, 2014

A deeper look into the Java 8 Date and Time API

Within this post we will have a deeper look into the new Date/Time API we get with Java 8 (JSR 310). Please note that this post is mainly driven by code examples that show the new API functionality. I think the examples are self-explanatory so I did not spent much time writing text around them :-)

Let's get started!Working with date and time objects
All classes of the Java 8 Date/Time API are located within the java.time package. The first class we want to look at is java.time.LocalDate. A LocalDate represents a year-month-day date without time. We start with creating new LocalDate instances:

TemporalAdjusters are another nice way for date manipulation. TemporalAdjuster is a single method interface that is used to separate the process of adjustment from actual date/time objects. A set of common TemporalAdjusters can be accessed using static methods of the TemporalAdjusters class.

Time zones
Working with time zones is another big topic that is simplified by the new API. The LocalDate/Time classes we have seen so far do not contain information about a time zone. If we want to work with a date/time in a certain time zone we can use ZonedDateTime or OffsetDateTime:

Timestamps
Classes like LocalDate and ZonedDateTime provide a human view on time. However, often we need to work with time viewed from a machine perspective. For this we can use the Instant class which represents timestamps. An Instant counts the time beginning from the first second of January 1, 1970 (1970-01-01 00:00:00) also called the EPOCH. Instant values can be negative if they occured before the epoch. They follow ISO 8601 the standard for representing date and time.

Periods and DurationsPeriod and Duration are two other important classes. Like the names suggest they represent a quantity or amount of time. A Period uses date based values (years, months, days) while a Duration uses seconds or nanoseconds to define an amount of time. Duration is most suitable when working with Instants and machine time. Periods and Durations can contain negative values if the end point occurs before the starting point.

Conversion
Of course we do not always have objects of the type we need. Therefore, we need an option to convert different date/time related objects between each other. The following examples show some of the possible conversion options:

Conclusion
With Java 8 we get a very rich API for working with date and time located in the java.time package. The API can completely replace old classes like java.util.Date or java.util.Calendar with newer, more flexible classes. Due to mostly immutable classes the new API helps in building thread safe systems.

Indeed great examples and new Date and Time API looks very good, I can see some similarity with Joda time though. By the way I have also shared few examples on lambda expressions and streams, you may like it http://javarevisited.blogspot.sg/2014/02/10-example-of-lambda-expressions-in-java8.html

It will never fully replace java.util.Date and Calendar due to a complete lack of modularity and a very bad design (no Spec just RI;-/) of JSR 310. It is Joda with a few twists, but even bigger than that used to be and you have 3(!) other Date/Time APIs (low level Concurrency, the good old Date/Calendar classes in java.util and newly added JavaFX with its own Duration/Date handling) competing with it in Java 8 alone...