Java 8 Date and Time API Enhancements

The new Date and Time API is released in Java 8 to address the shortcomings of the older java.util.Date and java.util.Calendar APIs.
Enhancement to Date/Time APIs
1. Immutable and Thread Safe — java.util.Date and java.util.Calendar APIs are not thread safe and lead to potential concurrency issues, adding additional headaches to handle thread safety. Date and Time APIs in Java 8 are immutable and therefore thread safe.
2. Adequate API design and Variety of Utility methods— java.util.Date and java.util.Calendar APIs were poorly designed utility methods to support common operations. The new Date/Time API is ISO centric and has awide variety of utility methods to support common operations.
3. ZonedDate and ZonedTime Functionality — Earlier, additional logic was written to handle timezone logic with java.util.Date and java.util.Calendar APIs, whereas now ZonedDate/Time APIs can be used to handle timezone specific data.
Immutable Class Concept
As new Date and Time API’S are immutable, let’s dig into the points that make a class immutable:
1. Classes must be declared as final, such that no child classes can be created.
2. Data members in the class must be declared as final, so only one value is assigned.
3. Data members in the class must be declared as private, access is restricted via getters.
4. A parameterized constructor or deep copy constructor.
5. Public getter method for all the variables.
6. No setters.
LocalDate, LocalTime and LocalDateTime Core APIs
LocalDate — Local date in ISO format (yyyy-MM-dd) without time.
LocalTime — Local time in ISO format.
LocalDateTime — Local date and time, where time zone is not taken into account.
Period and Duration Core Classes
Period — Quantity of days in terms of year, month, etc.
Duration — Quantity of time in terms of hour, min, etc.
ZonedDateTime Core API
Java 8 provides ZonedDateTime to deal with time zone specific date and time. The ZoneId is an identifier used to represent different zones.
Compatibility with Date and Calendar
The toInstant() method converts existing Date and Calendar instances into new Date Time API object.
Joda-Time Library
In fact, Java 8 Date Time APIs has been led jointly by the author of Joda-Time library (Stephen Colebourne) and Oracle. This library provides pretty much all capabilities that are supported in the Java 8 Date Time project.




