Java Convert Timestamp to ZonedDateTime
Tags: Java Timestamp Java ZonedDateTime Java 8
In this Java core tutorial we learn how to convert a java.sql.Timestamp object to a java.time.ZonedDateTime object in Java programming language.
How to convert Timestamp to ZonedDateTime in Java
In Java, with a given Timestamp object we can follow these step to convert it to a ZonedDateTime object.
- Step 1: use the Timestamp.toLocalDateTime() method to convert the Timestamp object to a LocalDateTime object.
- Step 1: use the LocalDateTime.atZone(ZoneId zone) method to convert the LocalDateTime object of step 1 to a ZonedDateTime object with system default time zone.
ConvertTimestampToZonedDateTimeExample1.java
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ConvertTimestampToZonedDateTimeExample1 {
public static void main(String... args) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// Convert Timestamp object to ZonedDateTime object
LocalDateTime localDateTime = timestamp.toLocalDateTime();
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
System.out.println("Timestamp: " + timestamp);
System.out.println("ZonedDateTime: " + zonedDateTime);
}
}
Timestamp: 2022-05-24 13:23:05.939
ZonedDateTime: 2022-05-24T13:23:05.939+07:00[Asia/Bangkok]
Happy Coding 😊
Related Articles
Java Convert ZonedDateTime to Timestamp
Java Convert LocalDate to Timestamp
Java Convert Instant to Timestamp
Java Convert LocalDateTime to Timestamp
Java Convert Timestamp to LocalDateTime
Java Convert Timestamp to LocalDate
Java Convert Timestamp to Instant