Before starting Java date time API, let’s discuss some important drawbacks of old date time API.
Some drawbacks of old java date time api
- All the Date classes in old java date time api are mutable that’s why they are not thread safe.
- Old java date time classes are not defined consistently. Date Class is defined in both java.util as well as java.sql packages. Formatting and parsing classes are defined in a different java.text package.
- The java.util.Date contains both date and time, whereas java.sql.Date contains only date.
- Date class in old java date time api doesn’t provide internationalization and timezone support. The java.util.Calendar and java.util.TimeZone classes were introduced, but they also have all the problems listed above.
Java 8 introduced new Date-Time API is to cover the above drawbacks of old date-time API. The java.time package consists the classes for new java date time api.
Following are some of the important classes introduced in java.time package.
Local: Simplified date-time API with no complexity of timezone handling.
Zoned: Specialized date-time API to deal with various timezones.
Java 8 Date Time API Examples
- Java LocalDate class.
- Java LocalTime class.
- Java LocalDateTime class.
- Java MonthDay class.
- Java OffsetTime class.
- Java OffsetDateTime class.
- Java Clock class.
- Java ZonedDateTime class.
- Java ZoneId class.
- Java ZoneOffset class.
- Java Year class.
- Java YearMonth class.
- Java Period class.
- Java Duration class.
- Java Instant class.
- Java DayOfWeek class.
- Java Month enum.