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

In this article, we will learn how to convert Instant to Timestamp & vice-versa using newly introduced methods in Timestamp class in Java 1.8 version

Timestamp class in Java 1.8 version :

  • There are 4 new methods introduced in Java 1.8 version for Timestamp class, those are
    1. from(Instant) – gets an instance of Timestamp from an Instant object
    2. toInstant() – converts invoking Timestamp object to an Instant
    3. toLocalDateTime() – converts invoking Timestamp object to a LocalDateTime
    4. valueOf(LocalDateTime) – gets 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
  • Note: Many legacy methods of Timestamp class are deprecated

1. Convert Timestamp to java.util.Date :

  • Converting Instant to Timestamp is very straightforward as,
    • Timestamp.from() method accepts Instant as inputargument and returns Timestamp
  • Converted Timestamp will have,
    • Date & Time & Offset/Zone parts shifted to that particular Zone from the given Instant
  • In short, Instant -> Timestamp
  • Lets see an example for conversion of Instant to Timestamp in the below illustration

ConvertInstantToJavaSqlTimestamp.java

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

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

public class ConvertInstantToJavaSqlTimestamp {

	public static void main(String[] args) {

		// get instantaneous moment at UTC/GMT
		Instant instant = Instant.now();
		System.out.println("Current Instant at UTC/GMT is :- \n" 
				+ instant);


		// convert Instant to Timestamp
		Timestamp date = Timestamp.from(instant);
		System.out.print("\nConversion of Instant to Date is :- \n" 
				+ date);
	}
}

Output:

Current Instant at UTC/GMT is :- 
2022-08-20T12:41:19.803777600Z

Conversion of Instant to Date is :- 
2022-08-20 18:11:19.8037776

2. Convert Timestamp to an Instant :

  • Get Timestamp instantiating Timestamp object for conversion to an Instant
  • Conversion steps
    1. Convert Timestamp to Instant using toInstant() method
  • In short, Timestamp -> Instant
  • Lets see an example for conversion of Timestamp to an Instant in the below illustration

ConvertJavaSqlTimestampToInstant.java

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

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

public class ConvertJavaSqlTimestampToInstant {

	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. Convert java.sql.Timestamp to Instant
		Instant instant = timestamp.toInstant();
		System.out.print("\nConversion of Timestamp to an Instant is :- \n" 
				+ instant);
	}
}

Output:

Current Timestamp is :- 
2022-08-20 18:12:58.978

Conversion of Timestamp to an Instant is :- 
2022-08-20T12:42:58.978Z

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert java.util.Date to an Instant in different ways ?
Java 8 – How to convert Instant to java.util.Date and vice-versa ?