9.0 Date and Time API in java 8 | java 8 Date and Time API | Example programs for Date and Time API

preview_player
Показать описание
Java 8 introduced the new Date and Time API. This new API is inspired by the popular JodaTime library. It was added to overcome the drawbacks of the existing Date API.
Drawbacks of Existing API(Until Java 7):
Thread Safety -
The existing Date and Calendar classes are not thread-safe and require additional efforts to handle concurrency.
Time zones -
The old APIs did not provide any support for time zones. The new ZonedDateTime class supports time zones as well.
Other Improvements -
The new API meets the ISO standards and is easier to understand. It provides a lot of methods to manipulate date and time. The existing API is a bit difficult to use and understand as compared to the new API.
Java 8 Date Time  Improvements:
1. Immutability: All the classes in the new Date-Time API are immutable and good for multithreaded environments.
2. Separation of Concerns:  The new API separates clearly between human-readable date time and machine time (Unix timestamp). It defines separate classes for Date, Time, DateTime, Timestamp, Timezone, etc.
3. Utility operations:
All the new Date-Time API classes come with methods to perform common tasks, such as plus, minus, format, parsing, getting the separate part in date/time, etc.
4. Clarity: 
All the classes use Factory Pattern and Strategy Pattern for better handling. Once you have used the methods in one of the classes, working with other classes won’t be hard.
5. Extendable:
The new Date Time API works on the ISO-8601 calendar system but we can use it with other non-ISO calendars as well.

LocalDate
LocalTime
LocalDateTime class
ZonedDateTime class
Period and Duration classes
ChronoUnits Enum
Рекомендации по теме