In this article, we will learn how to convert LocalDateTime to java.sql.Timestamp in Java 1.8 version and vice-versa
1. Convert LocalDateTime to java.sql.Timestamp :
- Timestamp.valueOf() method accepts LocalDateTime as input–argument and returns Timestamp
- valueOf(LocalDateTime) – Obtains an instance of Timestamp from a LocalDateTime object, with the same year, month, day of month, hours, minutes, seconds and nanos date-time value as the provided LocalDateTime
- valueOf(LocalDateTime) method is static so there is no need to create/instantiate an object of Timestamp for this conversion
- Lets see an example for conversion of LocalDateTime to java.sql.Timestamp in the below illustration
ConvertLocalDateTimeToJavaSqlTimestamp.java
package in.bench.resources.java8.localdatetime.examples;
import java.sql.Timestamp;
import java.time.LocalDateTime;
public class ConvertLocalDateTimeToJavaSqlTimestamp {
public static void main(String[] args) {
// 1. get current System Date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n"
+ localDateTime);
// 2. convert LocalDateTime to Timestamp
Timestamp timestamp1 = Timestamp.valueOf(localDateTime);
System.out.print("\nConversion of LocalDateTime to Timestamp is :- \n"
+ timestamp1);
}
}
Output:
Current System Date/time is :-
2022-08-09T15:37:30.015990200
Conversion of LocalDateTime to Timestamp is :-
2022-08-09 15:37:30.0159902
2. Convert java.sql.Timestamp to LocalDateTime :
There are 2 ways to convert java.sql.Timestamp to LocalDateTime, those options are,
- Convert Timestamp to LocalDateTime using toLocalDateTime() method
- Convert Timestamp to LocalDateTime via Instant using toInstant() method
2.1 Timestamp to LocalDateTime :
- Convert Timestamp to LocalDateTime using toLocalDateTime() method
- In Short, Timestamp -> LocalDateTime
ConvertJavaSqlTimestampToLocalDateTime1.java
package in.bench.resources.java8.localdatetime.examples;
import java.sql.Timestamp;
import java.time.LocalDateTime;
public class ConvertJavaSqlTimestampToLocalDateTime1 {
public static void main(String[] args) {
// 1. get current java.sql.Timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println("Current Timestamp is :- \n"
+ timestamp);
// 2. Convert java.sql.Timestamp to LocalDateTime
LocalDateTime localDateTime = timestamp.toLocalDateTime();
System.out.print("\nConversion of java.sql.Timestamp to LocalDateTime is :- \n"
+ localDateTime);
}
}
Output:
Current Timestamp is :-
2022-08-09 15:38:02.993
Conversion of java.sql.Timestamp to LocalDateTime is :-
2022-08-09T15:38:02.993
2.2 Timestamp to LocalDateTime via Instant :
- First, convert Timestamp to Instant using toInstant() method and then
- Add zone information using atZone() method passing ZoneId as argument
- And invoke toLocalDateTime() method which returns LocalDateTime
- In Short, Timestamp -> Instant -> LocalDateTime
ConvertJavaSqlTimestampToLocalDateTime2.java
package in.bench.resources.java8.localdatetime.examples;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class ConvertJavaSqlTimestampToLocalDateTime2 {
public static void main(String[] args) {
// 1. get current java.sql.Timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println("Current Timestamp is :- \n" + timestamp);
// 2. First, convert java.sql.Timestamp to Instant
Instant instant = timestamp.toInstant();
// 3. get system default zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nSystem default Zone is :- \n" + zoneId);
// 4. Convert java.sql.Timestamp to LocalDateTime
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
System.out.print("\nConversion of java.sql.Timestamp to LocalDateTime via Instant is :- \n"
+ localDateTime);
}
}
Output:
Current Timestamp is :-
2022-08-09 15:38:34.485
System default Zone is :-
Asia/Calcutta
Conversion of java.sql.Timestamp to LocalDateTime via Instant is :-
2022-08-09T15:38:34.485
Related Articles:
- Java 8 – LocalDateTime with method details and examples
- Java 8 – How to get Date and Time fields from LocalDateTime ?
- Java 8 – How to form LocalDateTime passing Date and Time fields ?
- Java 8 – How to form LocalDateTime passing LocalDate and LocalTime ?
- Java 8 – How to form LocalDateTime passing Instant and ZoneId ?
- Java 8 – How to form LocalDateTime passing Second/Nano and ZoneOffset ?
- Java 8 – How to parse LocalDateTime in String form ?
- Java 8 – How to convert String to LocalDateTime ?
- Java 8 – How to convert LocalDateTime to String ?
- Java 8 – How to convert LocalDateTime in different formats ?
- Java 8 – How to convert LocalDateTime in different Format Style ?
- Java 8 – How to convert LocalDateTime to ZonedDateTime ?
- Java 8 – How to convert LocalDateTime to an OffsetDateTime ?
- Java 8 – How to convert LocalDateTime to an Instant ?
- Java 8 – How to extract LocalDateTime and LocalTime from LocalDateTime ?
- Java 8 – How to convert LocalDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDateTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime in different ways ?
- Java 8 – How to add Date and Time fields to LocalDateTime ?
- Java 8 – How to subtract Date and Time fields from LocalDateTime ?
- Java 8 – How to alter Date and Time fields of LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ?
- Java 8 – How to compare two LocalDateTime instances ?
- Java 8 – How to find difference between two LocalDateTime using Period & Duration ?
- Java 8 – What are all the Temporal Fields supported by LocalDateTime ?
- Java 8 – What are all the Temporal Units supported by LocalDateTime ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!