Convert OffsetDateTime to SQL TimeStamp
0 CommentsLast Updated on May 2, 2021 by jt
Java 8 introduces a new date and time API defined inside the java.time package. The key date and time classes defined in this package are LocalDateTime, ZonedDateTime, and OffsetDateTime.
OffsetDateTime is an immutable representation of a date-time with an offset from UTC. It stores all date and time fields, to a precision of nanoseconds. For example, you can store the value 20th January 2021 at 10:35.40.123456789 +03:00 in OffsetDateTime.
In my previous post Convert OffsetDateTime to LocalDateTime, I explained how to convert OffsetDateTime to LocalDateTime.
I also have a post to convert OffsetDateTime to ZonedDateTime.
In this post, I will discuss how to convert OffsetDateTime to SQL TimeStamp.
java.sql.Timestamp extends java.util.Date class. This class represents a SQL TIMESTAMP to store timestamps in database. An example of a timestamp is 2021-01-20 15:22:33.9441128.
Approach 1: Converting OffsetDateTime to SQL TimeStamp
The code to convertOffsetDateTime to TimeStamp is this.
public Timestamp asTimestamp(OffsetDateTime offsetDateTime) {
System.out.println("OffsetDateTime: "+offsetDateTime);
if (offsetDateTime != null) {
return Timestamp.valueOf(offsetDateTime.atZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
}
else
return null;
}
In the preceding code,TimeStamp.value() gets the timestamp value from offsetDateTime and converts it to UTC.
Let’s call this method from the main class.
DateConverter dateConvertor = new DateConverterImpl();
OffsetDateTime offsetDateTime = OffsetDateTime.now();
System.out.println("SqlTimestamp: "+dateConvertor.asTimestamp(offsetDateTime));
In the preceding code, OffsetDateTime.now() gets the current date-time in the default time-zone with an offset. Then, the asTimestamp() method of DateConverter is called passing the OffsetDateTime to obtain a Timestamp.
On running the application, the output is this.
Approach 2: Converting OffsetDateTime to SQL TimeStamp
Next, I will convert offsetdatetime to timestamp with ZoneId.of() method to get the timestamp of any particular time zone.
This is the code.
public Timestamp timestampZone(OffsetDateTime offsetDateTime) {
if (offsetDateTime!=null)
return Timestamp.valueOf(offsetDateTime.atZoneSameInstant(ZoneId.of("Asia/Shanghai")).toLocalDateTime());
else
return null;
}Now, let’s call this method.
System.out.println("Timestamp : "+dateConvertor.timestampZone(offsetDateTime));
On running the application, you will get the timestamp at that time of time-zone “Asia/Shanghai” as shown in the following output figure.
Approach 3: Converting OffsetDateTime to SQL TimeStamp
The code to convert offsetdatetime to timestamp is this.
public Timestamp timestampDate(OffsetDateTime offsetDateTime){
if(offsetDateTime!=null)
return Timestamp.valueOf(OffsetDateTime.now().toLocalDate().atStartOfDay());
else
return null;
}In Line 3 toLocalDate().atStartOfDay() gets the timestamp value as the date and time as all zeroes.
Let’s call this method.
System.out.println(dateConvertor.timestampDate(offsetDateTime));
The output for the preceding code is this.
You can find the source code for this post here on Github.



