First, we see how the new API is better than the older one:
Thread Safety:
Since old date-time was not thread-safe. So we have to deal with concurrency issues while using date and to write additional code to handle thread-safety. So in Java 8 date and time are immutable and thread-safe, thus taking that concurrency headache away from us.
Poor Designing:
It had no uniformity. As default Date starts from 1900, the month starts from 1, and the day starts from 0. It had inadequate methods to perform day-to-day operations and there were fewer operations present. The new date-time API is ISO centric and follows consistent domain models for time, dates, duration, and periods and there is a wide variety of utility methods for such operations.
Zone Handling:
We had to write a lot of code to handle the zoning issue. But in new API handling of timezone can be done with Local and ZonedDate APIs.
Let’s perform some basic hands-on:
We can have handle Date and Time in 3 ways:
– LocalDate: LocalDate represents a date in ISO format (yyyy-mm-dd) without time.
– LocalTime: LocalTime represents time without a date.
– LocalDateTime: LocalDateTime is used to represent a combination of date and time.
#java #java8