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
- from(Instant) – gets an instance of Timestamp from an Instant object
- toInstant() – converts invoking Timestamp object to an Instant
- toLocalDateTime() – converts invoking Timestamp object to a LocalDateTime
- 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 input–argument 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 –
- 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:
- Java 8 – Instant with method details and examples
- Java 8 – How to get instantaneous moment at UTC/GMT using Instant ?
- Java 8 – How to form an Instant passing Seconds and Nanoseconds fields ?
- Java 8 – How to get Seconds and Nanoseconds from an Instant ?
- Java 8 – How to parse Instant in String form ?
- Java 8 – How to convert Instant to LocalDate ?
- Java 8 – How to convert Instant to LocalTime ?
- Java 8 – How to convert Instant to LocalDateTime ?
- Java 8 – How to convert Instant to ZonedDateTime ?
- Java 8 – How to convert Instant to an OffsetDateTime ?
- Java 8 – How to convert Instant to number of Seconds and vice-versa ?
- Java 8 – How to convert Instant to number of Milliseconds and vice-versa ?
- Java 8 – How to convert Instant to java.util.Date and vice-versa ?
- Java 8 – How to convert Instant to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert Instant to Calendar and vice-versa ?
- Java 8 – How to convert Instant to GregorianCalendar and vice-versa ?
- Java 8 – How to convert Instant to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an Instant in different ways ?
- Java 8 – How to add Second, Millisecond and Nanosecond to an Instant ?
- Java 8 – How to subtract Second, Millisecond and Nanosecond from an Instant ?
- Java 8 – How to check whether an Instant is Before another Instant ?
- Java 8 – How to check whether an Instant is After another Instant ?
- Java 8 – How to compare two Instant instances ?
- Java 8 – How to find difference between two Instant instances using Duration ?
- Java 9 – Find difference between two Instant instances upto nanosecond precision ?
- Java 9 – How to convert Instant to LocalDate using ofInstant() method ?
- Java 9 – How to convert Instant to LocalTime using ofInstant() method ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!