Skip to main content

Convert ZonedDateTime to Timestamp in Java [2021]

Convert ZonedDateTime to Timestamp in Java [2021] 

In this tutorial, we are going to cover how to convert ZonedDateTime to Timestamp and how to convert TImestamp to ZonedDateTime.

To Convert ZonedDateTime to sql Timestamp instance we have TimeStamp.valueOf(LocalDateTime time) method which takes LocalDateTime as parameter.

So to convert ZonedDateTime to java.sql.Timestamp we first have to convert ZonedDateTime to LocalDateTime and then we use the Timestamp.valueOf(LocalDateTime time) method to convert LocalDateTime to Timestamp.

Convert ZonedDateTime to Timestamp

Convert ZonedDateTime to TimeStamp

To Convert java.time.ZonedDateTime to java.sql.Timestamp instance we have two steps:
  1. First Convert ZonedDateTime to LocalDateTime using ZonedDateTime.toLocalDateTime() method.
  2. Second Convert LocalDateTime to Timestamp using Timestamp.valueOf(LocalDateTime time) method.

Example :

 import java.sql.Timestamp;
 import java.time.LocalDateTime;
 import java.time.ZonedDateTime;

 public class ZonedDateTimeToTimestampEx {
   public static void main(String[] args) {
     //Create ZonedDateTime instance
     ZonedDateTime time = ZonedDateTime.now();
     System.out.println("ZonedDateTime : "+time);
     //Convert ZonedDateTime to LocalDateTime instance
     LocalDateTime dateTime = time.toLocalDateTime();
     System.out.println("LocalDateTime : "+dateTime);
     //Finally Convert ZonedDateTime to Timestamp
     Timestamp timestamp = Timestamp.valueOf(dateTime);
     System.out.println("Timestamp : "+timestamp);

Output :

ZonedDateTime : 2020-07-14T19:46:31.602+05:30[Asia/Calcutta]
LocalDateTime : 2020-07-14T19:46:31.602
Timestamp : 2020-07-14 19:46:31.602

Convert Timestamp to ZonedDateTime

Convert TimeStamp to ZonedDateTime 

To convert java.sql.Timestamp to ZonedDateTime we have two steps:
  1.  First convert Timestamp to LocalDateTime using Timestamp.toLocalDateTime() method.
  2.  Second convert LocalDateTime to ZonedDateTime using ZonedDateTime.of(datetime, zoneId) method using systemdefault timezone.

Example :

 import java.sql.Timestamp;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.time.ZonedDateTime;

 public class TimestampToZonedDateTimeEx {
   public static void main(String[] args) {
     //Create Timestamp instance
     Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now());
     System.out.println("Timestamp : " + timestamp);

     //Convert Timestamp to LocalDateTime
     LocalDateTime datetime = timestamp.toLocalDateTime();
     ZoneId zoneId = ZoneId.systemDefault();
     System.out.println("LocalDateTime : " + datetime);

     //Finally Convert Timestamp to ZonedDateTime
     ZonedDateTime time = ZonedDateTime.of(datetime, zoneId);
     System.out.println("ZonedDateTime : " + time);

Output :

Timestamp : 2020-07-14 19:48:43.007
LocalDateTime : 2020-07-14T19:48:43.007
ZonedDateTime : 2020-07-14T19:48:43.007+05:30[Asia/Calcutta]

Conclusion :

In this tutorial, we have covered how we can 
  • Convert ZonedDateTime to Timestamp using Timestamp.valueOf(LocalDateTime time)  method.
  • Convert Timestamp to ZonedDateTime using ZonedDateTime.of(datetime, zoneId) method using systemdefault timezone.
Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.

Happy Learning!!!