Java 8 Time and Date – What has changed for better

33 Flares Twitter 0 Facebook 0 Google+ 33 LinkedIn 0 Filament.io 33 Flares ×

In this post we will have a deeper look into the new Date/Time API introduced in Java ( JDK ) 8. One of the long standing pain points of java was its poorly defined date and time APIs. It was not intuitive and was responsible for developers creating hard to debug bugs !. Yes a poorly defined API can confuse developers. The Calendar class of java is a horrible way to deal with dates, it is very difficult to use due to the lack of simple intuitive methods.  The list is many. Because of these issues, many serious java projects switched to Joda time. Joda time made working with date and time easier and better from a performance stand point as well. JDK 8 has adopted Joda time and all its goodness.  If your project is going to be developed in Java 8, just use the java.time library included in Java 8, no need for joda time.  Let us look at new java.time libraries using examples.

LocalDate and LocalTime

Just like the name, these are Local to the system in which it is running. That is it uses the default time zone of the running desktop. A LocalDate represents the date without the time part.  When you run the below example, you can see that there is no timezone component in the out of local date and local time. This avoids a lots of confusion. Now it is pretty clear and intuitive that the output is in local machines default time zone.

 Parsing date String

 Referring to a particular day of year, addition & subtraction

You can add or subtract days using the plus / minus methods. There are methods for plusDays, plusYear, plusWeeks etc. It is a much cleaner API. Please note that the new java.time classes are immutable and hence when you modify using the above operations, what you get is a new instance of LocalDate.

Easier, Intuitive boolean comparison

 Cleaner Representation of Month Text and Value

Year Class

If you just need the year information with out the need for date and time, then just use the Year

Temporal Adjusters

The new API introduced the concept of an adjuster—a block of code that can be used to wrap up common processing logic. There are many default adjusters provided as static methods. The most common types are WithAdjusters – to set some field values and PlusAdjusters – to add or subtract date time values. Let us look at few examples to learn this.

You can also use the static import feature to make the code more readable.

 Time Zone Conversion

In Java 8, working with Time Zones and converting from one time zone to another is pretty straightforward. The code and API looks so clean and better than the earlier Calendar equivalents. The important classes are ZoneId and ZonedDateTime.  The below example converts the current time in indian time zone to canadian timezone.

 List All Available Time Zone Ids

The newer Java 8 Time API is neat, thread safe and intuitive. It can completely replace java.util.Date and java.util.Calendar completely. The new API is going to make programming in java a lot easier.  A great addition to Java !.

  • Jasar

    Thanks…. Simply explained