Java_8 New - New DateTime API
With Java 8, a new Data Time API is introduced to cover the following drawbacks of old data time API.
- Not thread safe - java.util.Date is not thread safe, thus developer have to deal with concurrency issue while using date. New date time API are immutable and do not have setter methods.
- Poor design - Default Date starts from 1900 , month starts from 1 and day starts from 0 so no uniformity. Less direct methods for date operations. New APIs provides numerous utility methods for such operations.
- Difficult time zone handling - Developers have to write lot of code to deal with time zone issues. New API design is developend keeping domain specific design in mind.
JAVA 8 introduces a new date time API under package java.time. Following are some of the important classes introduced in java.time package.
- Local - Simplified Date time API with no complexity of Time Zone handling.
- Zoned - Specialized Date time API to deal with various time zones.
SN | Methods with Description |
1 | Local Date Time API LocalDate/ LocalTime and LocalDateTime classes simplifies the development where timezone are not required. |
2 | Zoned Date Time API Zoned Date Time APIs is used when time zone is to be considered. |
3 | Chrono Units Enum java.time.temporal.ChronoUnit enum is added in java 8 to replace integer values used in old API to represent day, month etc. |
4 | Period and Duration These classes are introduced to deal with time differences. |
5 | Temporal Adjusters TemporalAdjuster is to do the date mathematics. For example to get the "Second Saturday of the Month" or "Next Tuesday". |
6 | Backwards Compatibility A toInstant() method is added to the original Date and Calendar objects which can be used to convert them to the new Date-Time API. |