Home Java 8 - Date and time API
Post
Cancel

Java 8 - Date and time API

Java 8 - Date and time API

The Java 8 release introduced a new Date and Time API, which offers a lot of improvements over the old Date and Calendar classes. The new API is more flexible, easier to use, and has better support for internationalization.

In this blog, we will take a closer look at the Date and Time API in Java 8 and explore its various features.

The LocalDate, LocalTime, and LocalDateTime Classes

The Date and Time API in Java 8 introduces three new classes that represent date and time values: LocalDate, LocalTime, and LocalDateTime. These classes are immutable and thread-safe, making them a great choice for representing date and time values in your programs.

1
2
3
4
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
LocalDateTime currentDateTime = LocalDateTime.now();

The LocalDate class represents a date value, such as “2022-01-01”. The LocalTime class represents a time value, such as “15:30:00”. The LocalDateTime class represents a date and time value, such as “2022-01-01T15:30:00”.

Parsing and Formatting Dates and Times

The DateTimeFormatter class is used for formatting and parsing dates and times. The class provides a wide range of formatting options, such as pattern-based formatting and localized formatting.

1
2
3
4
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
String formattedDateTime = currentDateTime.format(formatter);

The ofPattern method of the DateTimeFormatter class is used to create a formatter with the given pattern. The pattern string contains a combination of letters and symbols that represent various parts of the date and time value.

Time Zones and Offset

The ZonedDateTime class represents a date and time value with a time zone. The OffsetDateTime class represents a date and time value with an offset from UTC.

1
2
3
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneOffset.ofHoursMinutes(5, 30));

The ZoneId class is used to represent a time zone, and the ZoneOffset class is used to represent a fixed offset from UTC.

Duration and Period

The Duration class represents a duration of time between two Instant objects. The Period class represents a period of time between two LocalDate objects.

1
2
3
4
5
6
7
8
9
Instant start = Instant.now();
Thread.sleep(5000);
Instant end = Instant.now();
Duration duration = Duration.between(start, end);

LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.of(2023, Month.JANUARY, 1);
Period period = Period.between(startDate, endDate);

Conclusion

The Date and Time API in Java 8 is a powerful and flexible tool for working with dates and times in your Java programs. Its flexible and easy-to-use classes and methods make it much easier to work with date and time values, and its support for time zones and offsets makes it possible to work with dates and times in a global context. Whether you are a seasoned Java developer or just getting started, the Date and Time API in Java 8 is a feature that you should definitely explore.

This post is licensed under CC BY 4.0 by the author.