Back to: Java Tutorials
We can use Java 8 Date and Time API by using the following steps:
- Import the
java.time
package: To use the Java 8 Date and Time API, you need to import thejava.time
package. You can do this by adding the following line at the top of your Java file:
import java.time.*;
- Create a
LocalDate
object: To represent a date in the Java 8 Date and Time API, you can use theLocalDate
class. To create aLocalDate
object, you can use one of its static factory methods. For example, to create aLocalDate
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.
- Create a
LocalTime
object: To represent a time in the Java 8 Date and Time API, you can use theLocalTime
class. To create aLocalTime
object, you can use one of its static factory methods. For example, to create aLocalTime
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.
- Create a
LocalDateTime
object: To represent both date and time in the Java 8 Date and Time API, you can use theLocalDateTime
class. To create aLocalDateTime
object, you can use one of its static factory methods. For example, to create aLocalDateTime
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.
- Create a
DateTimeFormatter
object: To format aLocalDateTime
object, you can use theDateTimeFormatter
class. To create aDateTimeFormatter
object, you can use one of its static factory methods. For example, to create aDateTimeFormatter
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.
- Format the
LocalDateTime
object: To format theLocalDateTime
object using theDateTimeFormatter
object, you can use theformat()
method. For example, to format thecurrentDateTime
object using theformatter
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.
- Parse a
String
to aLocalDateTime
object: To parse aString
that represents a date and time to aLocalDateTime
object, you can use theparse()
method of theDateTimeFormatter
class. For example, to parse theString
“12/02/2023 12:34:56” to aLocalDateTime
object using theformatter
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