Java 8 introduced new APIs for Date and Time to address the shortcomings of the older java.util.Date and java.util.Calendar.

1. Java 8 Date Time API Packages

Java 8 Date Time API consists of following packages.

  1. java.time This is the base package of new Java Date Time API. All the major base classes are part of this package, such as LocalDate, LocalTime, LocalDateTime, Instant, Period, Duration etc. All of these classes are immutable and thread safe. Most of the times, these classes will be sufficient for handling common requirements.
  2. java.time.chrono This package defines generic APIs for non ISO calendar systems. We can extend AbstractChronology class to create our own calendar system.
  3. java.time.format This package contains classes used for formatting and parsing date time objects. Most of the times, we would not be directly using them because principle classes in java.time package provide formatting and parsing methods.
  4. java.time.temporal This package contains temporal objects and we can use it for find out specific date or time related to date/time object. For example, we can use these to find out the first or last day of the month. You can identify these methods easily because they always have format “withXXX”.
  5. java.time.zone This package contains classes for supporting different time zones and their rules.

Please note that this post is mainly driven by code examples that show the new API functionality. We think the examples are self-explanatory.

2. Using LocalDate, LocalTime and LocalDateTime API

  1. Exploring LocalDate

    LocalDate represents a date in ISO format (yyyy-MM-dd) without time.

  2. Exploring LocalTime

    LocalTime represents time without a date.

  3. Exploring LocalDateTime

    LocalDateTime is used to represent a combination of date and time.

    An instance of LocalDateTime can be obtained from the system clock similar to LocalDate and LocalTime

DateTime API Java 8
Package Structure of DateTime API

3. Using ZonedDateTime API

We can use ZonedDateTime to deal with Date and Time with specific Zone.

There are about 40 different time zones provided by ZoneId.

4. Using Instant API a.k.a Timestamps

An Instant counts the time beginning from the first second of January 1, 1970 (1970-01-01 00:00:00) also called the EPOCH.

5. Using Period and Duration

The Period class represents a quantity of time in terms of years, months and days and the Duration class represents a quantity of time in terms of seconds and nano seconds.

  1. Exploring Period

  2. Exploring Duration

6. Date and Time Formatting

Java 8 provides APIs for the easy formatting of Date and Time.

7. Conclusion

Java 8 provides a rich set of APIs with a consistent API design for easier development. The API can completely replace old classes like java.util.Date or java.util.Calendar with newer, more flexible classes.

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *