Back to: Java Tutorials
We can use Java 8 Date and Time API by using the following steps:
- Import the
java.timepackage: To use the Java 8 Date and Time API, you need to import thejava.timepackage. You can do this by adding the following line at the top of your Java file:
import java.time.*;
- Create a
LocalDateobject: To represent a date in the Java 8 Date and Time API, you can use theLocalDateclass. To create aLocalDateobject, you can use one of its static factory methods. For example, to create aLocalDateobject 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.
- Create a
LocalTimeobject: To represent a time in the Java 8 Date and Time API, you can use theLocalTimeclass. To create aLocalTimeobject, you can use one of its static factory methods. For example, to create aLocalTimeobject 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.
- Create a
LocalDateTimeobject: To represent both date and time in the Java 8 Date and Time API, you can use theLocalDateTimeclass. To create aLocalDateTimeobject, you can use one of its static factory methods. For example, to create aLocalDateTimeobject 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.
- Create a
DateTimeFormatterobject: To format aLocalDateTimeobject, you can use theDateTimeFormatterclass. To create aDateTimeFormatterobject, you can use one of its static factory methods. For example, to create aDateTimeFormatterobject 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.
- Format the
LocalDateTimeobject: To format theLocalDateTimeobject using theDateTimeFormatterobject, you can use theformat()method. For example, to format thecurrentDateTimeobject using theformatterobject, 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.
- Parse a
Stringto aLocalDateTimeobject: To parse aStringthat represents a date and time to aLocalDateTimeobject, you can use theparse()method of theDateTimeFormatterclass. For example, to parse theString“12/02/2023 12:34:56” to aLocalDateTimeobject using theformatterobject, 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
