1. Using LocalDate class atTime() method [Recommended]
2. Using LocalDateTime class of() method
Read Also: Convert LocalDate to Instant in Java
Convert LocalDate to Timestamp in Java
1. Using LocalDate class atTime() method
We can easily convert LocalDate to Timestamp using the LocalDate class atTime() method as shown below in the example:import java.sql.Timestamp; import java.time.LocalDate; import java.time.LocalTime; public class LocalDateToTimestamp { public static void main(String args[]) { // Get Current LocalDate LocalDate localDate = LocalDate.now(); // Printing LocalDate System.out.println("Current date is: " + localDate); // Convert LocalDate to Timestamp Timestamp timestamp = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT)); // Printing Timestamp System.out.println("Timestamp is: " + timestamp); } }
Output:
Current date is: 2024-03-15
Timestamp is: 2024-03-15 00:00:00.0
2. Using LocalDateTime class of() method
There is another way to convert LocalDate to Timestamp using the LocalDateTime class of() method as shown below in the example:import java.time.LocalDateTime; import java.sql.Timestamp; import java.time.LocalDate; import java.time.LocalTime; public class LocalDateToTimestamp2 { public static void main(String args[]) { // Fetch Current LocalDate LocalDate localDate = LocalDate.now(); // Displaying LocalDate System.out.println("Current date is: " + localDate); // Converting LocalDate to Timestamp Timestamp timestamp = Timestamp.valueOf(LocalDateTime.of(localDate, LocalTime.MIDNIGHT)); // Displaying Timestamp System.out.println("Timestamp is: " + timestamp); } }
Output:
Current date is: 2024-03-15
Timestamp is: 2024-03-15 00:00:00.0
That's all for today. Please mention in the comments if you know any other way of converting LocalDate to Timestamp in Java.