Date Time API In Java 8

There were several issues with Date API from Java 7 and Previous version, which new Date & time API from Java 8 fixes these issues. I am listing some of problem below:

Why new Java Date Time API introduced In Java 8?

  • The date API in java 7 and earlier version have one class java.util.Date (contains both date and time) that is extended in java.sql.Date (contains only date) .As we have Date Class in both java.util as well as java.sql packages, this in java.sql package doesn’t make sense. Also both the classes have same name, that is a very bad design itself.
  • All the Date classes are mutable, so they are not thread safe. It’s one of the biggest problem with Java Date and Calendar classes.
  • Formatting and parsing classes are defined separately in java.text package.
  • For time, timestamp, formatting and parsing no classes defined clearly. We have java.text.DateFormat class for parsing and formatting. Most of the time SimpleDateFormat class is used for parsing and formatting.
  • Date class doesn’t provide internationalization, there is no timezone support. So java.util.Calendar and java.util.TimeZone classes were introduced, but they also have all the problems statement as listed above.

Example 1 + Problem + Old Date and Time API

In Java 7 and earlier version, we have one simple pattern to create a date.

The above statement creates a date object and the date of that object is the time at which this statement was executed. What if I need to create a date for 2016 / 07 /12. In this case we have Calendar class in java.util package. The Calendar class can be used to create an exact date. For example, 7th Dec 2016 can be created like below:

I take the instance of the Calendar and set it to the 7th December 2016. Note: the month start from 0, so January is 0 and December is 11. And if from calendar object I want corresponding date, I need to call getTime() method on calendar object.

How can I add 10 days more to my date 7th Dec 2016 . We have an add method on calendar, by calling this we can add days to our calendar, which takes unit of time I want to add, in our case its days.

We said the Date class in mutable since java 1. What does it mean mutable class? It means I can modify the value kept in that class from the outside. Suppose I have Employee class as below.

I have joiningDate in this Employee class and a getter method getJoiningDate() that returns joining date of an Employee. I don’t want some outside code modify this joining date field of an Employee, so I choose not to expose the setter method of the Employee joining date field. So no one can modify this joining date at runtime after this Employee object has been constructed. So the joining date field will be initialized at the time creation of Employee object. Some other code can do the following:

So when I create an Employee object using constructor I set the employee joining date properly. Then I can get this joining date from an outside code using employee.getJoiningDate() and I can just reset the joining date from outside of an Employee just by calling setTime. Thus modifying the value of joining date of an Employee object from outside is possible. So how I can prevent from someone modifying the joining date of an Employee from outside? I need a well know pattern called Defensive Copy.

In new implementation of getJoiningDate() method , we are creating new date object, that is different from joining date object owned by this instance of Employee, and creating date by using right value in this joining date. Thus is some outside code try to modify joining date my inside object value is still secure. But problem with this getJoiningDate() method is that, if someone call this new getJoiningDate method , It’s need to create a new Date object on each call, once this new object is used will be send to Garbage collector. So much more overhead in protecting a date value inside a class from being modified outside code. So having this mutable Date class having a price and price a little bit high.

The above problem statement has been addressed in Java 8 new API using java.time package. This completely replace java.util.Date class  and java.util.Calendar class too. This new package maintain interoperation with legacy classed with those legacy API. It introduced some new concepts like Instant, Duration etc. All the classes in the new Date Time API are immutable so solve the problem related to multi-threading. New API defines separate API classes for Date, Time, DateTime, Timestamp, Timezone etc, clearly define human readable date time and machine time (unix timestamp). For formatting and parsing format() and parse() methods defined in all these classes rather than having a separate class for them.

We have some of new concepts In Java 8 Date and Time API

  • The new concept of Date in like Instant and Duration in Java 8.
  • We also have Duration and Period concept in new Date Time API.
  • To Compute a new date from a given date.
  • New API to deal with time zones issue.
  • Formatting the date with well-known established standards.

Leave a Reply

Your email address will not be published. Required fields are marked *