Java Convert LocalDate to Timestamp
Tags: Java LocalDate Java Timestamp Java 8
In this Java core tutorial we learn how to convert a java.time.LocalDate object to a java.sql.Timestamp object in Java programming language.
How to convert LocalDate to Timestamp in Java
In Java, with a given LocalDate object we can follow these steps to convert it to Timestamp object.
- Step 1: convert the LocalDate object to LocalDateTime object using LocalDate.atStartOfDay() method.
- Step 2: convert the LocalDateTime object from step 1 to Timestamp object using Timestamp.valueOf(LocalDateTime dateTime) method.
ConvertLocalDateToTimestampExample1.java
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class ConvertLocalDateToTimestampExample1 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
// Convert LocalDate object to Timestamp object
LocalDateTime localDateTime = localDate.atStartOfDay();
Timestamp timestamp = Timestamp.valueOf(localDateTime);
System.out.println("LocalDate: " + localDate);
System.out.println("Timestamp: " + timestamp);
}
}
LocalDate: 2022-05-22
Timestamp: 2022-05-22 00:00:00.0
Happy Coding 😊
Related Articles
Java Convert ZonedDateTime to Timestamp
Java Convert LocalDateTime to Timestamp