Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?

In this article, we will learn how to convert ZonedDateTime to java.sql.Timestamp in Java 1.8 version and vice-versa

1. Convert ZonedDateTime to java.sql.Timestamp :

  • Timestamp.valueOf() method accepts LocalDateTime as inputargument 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
  • First, convert ZonedDateTime to LocalDateTime using toLocalDateTime() method and pass converted LocalDateTime to Timestamp.valueOf() method which will return Timestamp
  • After conversion, Date & Time parts remain same as that of ZonedDateTime
  • Lets see an example for conversion of ZonedDateTime to java.sql.Timestamp in the below illustration

ConvertZonedDateTimeToJavaSqlTimestamp.java

package in.bench.resources.java8.zoneddatetime.examples;

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

public class ConvertZonedDateTimeToJavaSqlTimestamp {

	public static void main(String[] args) {

		// 1. get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Zoned Date/time is :- \n" 
				+ zonedDateTime);


		// 2. convert ZonedDateTime to Timestamp
		Timestamp timestamp1 = Timestamp
				.valueOf(zonedDateTime.toLocalDateTime());
		System.out.print("\nConversion of ZonedDateTime to Timestamp is :- \n" 
				+ timestamp1);
	}
}

Output:

Zoned Date/time is :- 
2022-08-13T16:33:22.282324200+05:30[Asia/Calcutta]

Conversion of ZonedDateTime to Timestamp is :- 
2022-08-13 16:33:22.2823242

2. Convert java.sql.Timestamp to ZonedDateTime :

There are 2 ways to convert java.sql.Timestamp to ZonedDateTime, those options are,

  1. Convert Timestamp to ZonedDateTime via LocalDateTime using toLocalDateTime() & atZone() methods
  2. Convert Timestamp to LocalDateTime via Instant using toInstant() & atZone() methods

2.1 Timestamp to ZonedDateTime :

  • Convert Timestamp to LocalDateTime using toLocalDateTime() method and then invoke atZone() method passing ZoneId as input-argument which will return ZonedDateTime
  • In Short, Timestamp -> LocalDateTime -> ZonedDateTime

ConvertJavaSqlTimestampToZonedDateTime1.java

package in.bench.resources.java8.zoneddatetime.examples;

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

public class ConvertJavaSqlTimestampToZonedDateTime1 {

	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. get system default zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nDefault System Zone is :- \n" 
				+ zoneId);


		// 3. Convert java.sql.Timestamp to ZonedDateTime
		ZonedDateTime zonedDateTime = timestamp.toLocalDateTime().atZone(zoneId);
		System.out.print("\nConversion of java.sql.Timestamp to ZonedDateTime is :- \n"
				+ zonedDateTime);
	}
}

Output:

Current Timestamp is :- 
2022-08-13 16:33:35.75

Default System Zone is :- 
Asia/Calcutta

Conversion of java.sql.Timestamp to ZonedDateTime is :- 
2022-08-13T16:33:35.750+05:30[Asia/Calcutta]

2.2 Timestamp to ZonedDateTime via Instant :

  • First, convert Timestamp to Instant using toInstant() method and then add zone information using atZone() method passing ZoneId as input-argument which will return ZonedDateTime
  • In Short, Timestamp -> Instant -> ZonedDateTime

ConvertJavaSqlTimestampToZonedDateTime2.java

package in.bench.resources.java8.zoneddatetime.examples;

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

public class ConvertJavaSqlTimestampToZonedDateTime2 {

	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 ZonedDateTime
		ZonedDateTime zonedDateTime = instant.atZone(zoneId);
		System.out.print("\nConversion of java.sql.Timestamp to ZonedDateTime via Instant is :- \n"
				+ zonedDateTime);
	}
}

Output:

Current Timestamp is :- 
2022-08-13 16:33:48.13

System default Zone is :- 
Asia/Calcutta

Conversion of java.sql.Timestamp to ZonedDateTime via Instant is :- 
2022-08-13T16:33:48.130+05:30[Asia/Calcutta]

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?