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

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

1. Convert OffsetDateTime to java.sql.Timestamp :

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

ConvertOffsetDateTimeToJavaSqlTimestamp.java

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

import java.sql.Timestamp;
import java.time.OffsetDateTime;

public class ConvertOffsetDateTimeToJavaSqlTimestamp {

	public static void main(String[] args) {

		// 1. get Offset Date/time
		OffsetDateTime offsetDateTime = OffsetDateTime.now();
		System.out.println("Offset Date/time is :- \n" 
				+ offsetDateTime);


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

Output:

Offset Date/time is :- 
2022-08-17T12:26:03.433774900+05:30

Conversion of OffsetDateTime to Timestamp is :- 
2022-08-17 12:26:03.4337749

2. Convert java.sql.Timestamp to OffsetDateTime :

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

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

2.1 Timestamp to OffsetDateTime via LocalDateTime :

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

ConvertJavaSqlTimestampToOffsetDateTime1.java

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

import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class ConvertJavaSqlTimestampToOffsetDateTime1 {

	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
		ZoneOffset zoneOffset = ZoneOffset.of("+05:30");
		System.out.println("\nZoneOffset is :- \n" 
				+ zoneOffset);


		// 3. Convert java.sql.Timestamp to OffsetDateTime
		OffsetDateTime offsetDateTime = timestamp.toLocalDateTime().atOffset(zoneOffset);
		System.out.print("\nConversion of java.sql.Timestamp to an `OffsetDateTime is :- \n"
				+ offsetDateTime);
	}
}

Output:

Current Timestamp is :- 
2022-08-17 12:27:45.464

ZoneOffset is :- 
+05:30

Conversion of java.sql.Timestamp to an `OffsetDateTime is :- 
2022-08-17T12:27:45.464+05:30

2.2 Timestamp to OffsetDateTime via Instant :

  • First, convert Timestamp to Instant using toInstant() method and then add Offset information using atOffset() method passing ZoneOffset as input-argument which will return OffsetDateTime
  • In Short, Timestamp -> Instant -> OffsetDateTime

ConvertJavaSqlTimestampToOffsetDateTime2.java

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

import java.sql.Timestamp;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class ConvertJavaSqlTimestampToOffsetDateTime2 {

	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
		ZoneOffset zoneOffset = ZoneOffset.of("+05:30");
		System.out.println("\nZoneOffset is :- \n" 
				+ zoneOffset);


		// 4. Convert java.sql.Timestamp to an OffsetDateTime
		OffsetDateTime offsetDateTime = instant.atOffset(zoneOffset);
		System.out.print("\nConversion of java.sql.Timestamp to an OffsetDateTime via Instant is :- \n"
				+ offsetDateTime);
	}
}

Output:

Current Timestamp is :- 
2022-08-17 12:28:38.177

ZoneOffset is :- 
+05:30

Conversion of java.sql.Timestamp to an OffsetDateTime via Instant is :- 
2022-08-17T12:28:38.177+05:30

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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