Use Java 8 Date and Time API

We can use Java 8 Date and Time API by using the following steps:

  1. Import the java.time package: To use the Java 8 Date and Time API, you need to import the java.time package. You can do this by adding the following line at the top of your Java file:
import java.time.*;
  1. Create a LocalDate object: To represent a date in the Java 8 Date and Time API, you can use the LocalDate class. To create a LocalDate object, you can use one of its static factory methods. For example, to create a LocalDate object representing the current date, you can use the following code:
LocalDate currentDate = LocalDate.now();

This will create a new LocalDate object that represents the current date.

  1. Create a LocalTime object: To represent a time in the Java 8 Date and Time API, you can use the LocalTime class. To create a LocalTime object, you can use one of its static factory methods. For example, to create a LocalTime object representing the current time, you can use the following code:
LocalTime currentTime = LocalTime.now();

This will create a new LocalTime object that represents the current time.

  1. Create a LocalDateTime object: To represent both date and time in the Java 8 Date and Time API, you can use the LocalDateTime class. To create a LocalDateTime object, you can use one of its static factory methods. For example, to create a LocalDateTime object representing the current date and time, you can use the following code:
LocalDateTime currentDateTime = LocalDateTime.now();

This will create a new LocalDateTime object that represents the current date and time.

  1. Create a DateTimeFormatter object: To format a LocalDateTime object, you can use the DateTimeFormatter class. To create a DateTimeFormatter object, you can use one of its static factory methods. For example, to create a DateTimeFormatter object that formats a date and time as “dd/MM/yyyy HH:mm:ss”, you can use the following code:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");

This will create a new DateTimeFormatter object that uses the specified format string.

  1. Format the LocalDateTime object: To format the LocalDateTime object using the DateTimeFormatter object, you can use the format() method. For example, to format the currentDateTime object using the formatter object, you can use the following code:
String formattedDateTime = currentDateTime.format(formatter);

This will create a new String object that contains the formatted date and time value.

  1. Parse a String to a LocalDateTime object: To parse a String that represents a date and time to a LocalDateTime object, you can use the parse() method of the DateTimeFormatter class. For example, to parse the String “12/02/2023 12:34:56” to a LocalDateTime object using the formatter object, you can use the following code:
String dateTimeString = "12/02/2023 12:34:56";
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeString, formatter);

This will create a new LocalDateTime object that represents the parsed date and time value.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author