New Date-Time API in Java 8

Java 8 introduced a new Date-Time API that replaces the old java.util.Date and java.util.Calendar classes, which were known to be flawed and difficult to work with. The new Date-Time API in Java 8 is more intuitive, easier to use, and more efficient. In this blog post, we will dive into the features of the new Date-Time API in Java 8 and explore how to use it.

LocalDate, LocalTime, LocalDateTime

Java 8 introduced new classes called LocalDate, LocalTime, and LocalDateTime, which represent dates, times, and date-times, respectively. These classes are immutable and thread-safe, which means that they can be safely shared between multiple threads without the need for synchronization.

LocalDate represents a date without a time zone, such as “2023-04-17”. LocalTime represents a time without a time zone, such as “12:00:00”. LocalDateTime represents a date-time without a time zone, such as “2023-04-17T12:00:00”.

Here is an example of how to create a LocalDate, LocalTime, and LocalDateTime object in Java 8:

LocalDate date = LocalDate.of(2023, 4, 17);
LocalTime time = LocalTime.of(12, 0, 0);
LocalDateTime dateTime = LocalDateTime.of(2023, 4, 17, 12, 0, 0);

In the above code, we are creating a LocalDate object representing April 17, 2023, a LocalTime object representing 12:00:00, and a LocalDateTime object representing April 17, 2023, 12:00:00.

Instant

Java 8 introduced a new class called Instant, which represents an instant in time. It is similar to the old java.util.Date class, but it is more precise and accurate.

An Instant represents a point on the time-line, which is measured in seconds and nanoseconds since the Unix epoch time of January 1, 1970, at 00:00:00 UTC. Here is an example of how to create an Instant object in Java 8:

Instant instant = Instant.now();


In the above code, we are creating an Instant object representing the current date and time.

Duration and Period

Java 8 introduced two new classes called Duration and Period, which represent a duration of time and a period of time, respectively.

Duration represents a duration of time in terms of seconds and nanoseconds, while Period represents a period of time in terms of years, months, and days.

Here is an example of how to create a Duration and Period object in Java 8:

Duration duration = Duration.ofHours(1);
Period period = Period.ofDays(1);


In the above code, we are creating a Duration object representing one hour and a Period object representing one day.

Date-Time Formatting and Parsing

The DateTimeFormatter class is used to format and parse dates and times in Java 8. It provides a variety of pre-defined formatting patterns that can be used to format dates and times. It also allows you to define custom patterns.

Here is an example of how to format a LocalDate object using a pre-defined pattern:


LocalDate date = LocalDate.of(2023, 4, 17);
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
String formattedDate = date.format(formatter);
System.out.println(formattedDate);


In the above code, we are creating a LocalDate object representing April 17, 2023, and using the ISO_DATE pattern to format it. The output will be “2023-04-17”.

Here is an example of how to parse a date string using a custom pattern:


String dateString = "17-04-2023";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(dateString, formatter);
System.out.println(date);


In the above code, we are parsing a date string “17-04-2023” using a custom pattern “dd-MM-yyyy” and converting it to a LocalDate object. The output will be “2023-04-17”.

LocalizedDateTimeFormatter

The LocalizedDateTimeFormatter class is a subclass of DateTimeFormatter that provides a way to format and parse dates and times using the rules of a specific locale.

Here is an example of how to format a LocalDateTime object using the rules of the US locale:


LocalDateTime dateTime = LocalDateTime.of(2023, 4, 17, 12, 0, 0);
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.US);
String formattedDateTime = dateTime.format(formatter);
System.out.println(formattedDateTime);

In the above code, we are creating a LocalDateTime object representing April 17, 2023, 12:00:00, and using the FULL format style and the US locale to format it. The output will be “Sunday, April 17, 2023 at 12:00:00 PM EDT”.

ZoneDateTimeFormatter

The ZoneDateTimeFormatter class is a subclass of DateTimeFormatter that provides a way to format and parse dates and times with time zones.

Here is an example of how to format a ZonedDateTime object with a time zone:


ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 4, 17, 12, 0, 0, 0, ZoneId.of("America/New_York"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss z");
String formattedZonedDateTime = zonedDateTime.format(formatter);
System.out.println(formattedZonedDateTime);


In the above code, we are creating a ZonedDateTime object representing April 17, 2023, 12:00:00, in the America/New_York time zone and using a custom pattern to format it. The output will be “17-04-2023 12:00:00 EDT”.