Java Convert LocalDateTime to Timestamp
Tags: Java LocalDateTime Java Timestamp Java 8
In this Java core tutorial we learn how to convert a java.time.LocalDateTime object to a java.sql.Timestamp object in Java programming language.
How to convert LocalDateTime to Timestamp in Java
In Java, with a given LocalDateTime object we can use the Timestamp.valueOf(LocalDateTime dateTime) static method to convert it to a Timestamp object as the following example Java code.
ConvertLocalDateTimeToTimestampExample1.java
import java.sql.Timestamp;
import java.time.LocalDateTime;
public class ConvertLocalDateTimeToTimestampExample1 {
public static void main(String... args) {
LocalDateTime localDateTime = LocalDateTime.now();
Timestamp timestamp = Timestamp.valueOf(localDateTime);
System.out.println("LocalDateTime: " + localDateTime);
System.out.println("Timestamp: " + timestamp);
}
}
LocalDateTime: 2022-05-22T21:14:43.095622500
Timestamp: 2022-05-22 21:14:43.0956225
Happy Coding 😊
Related Articles
Java Convert ZonedDateTime to Timestamp
Java Convert LocalDate to Timestamp