In this article, we will learn how to convert java.util.Date to an Instant in different ways in Java 1.8 version
Date to an Instant conversion :
There are many ways to convert java.util.Date to an Instant –
- Using java.sql.Date & Timestamp & toInstant() method
- Using Timestamp & toInstant() method
- Using LocalDateTime.parse() and atZone() & toInstant() methods
- Using GregorianCalendar and toZonedDateTime() & toInstant() methods
- Using Instant & toInstant() method
- Using Instant.ofEpochMilli() & toInstant() methods
DifferentWaysToConvertJavaUtilDateToInstant.java
package in.bench.resources.java8.instant.examples;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DifferentWaysToConvertJavaUtilDateToInstant {
public static void main(String[] args) {
// get current date
Date date = new Date();
System.out.println("Current Date is = " + date);
// print to console
System.out.println("\nDate to Instant conversion :-");
System.out.println("-----------------------------------------------------");
// 1. Using java.sql.Date & Timestamp & toInstant() method
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
Timestamp timestamp1 = new java.sql.Timestamp(sqlDate.getTime());
LocalDateTime localDateTime1 = timestamp1.toLocalDateTime();
ZoneOffset zoneOffset1 = ZoneOffset.of("+05:30");
Instant instant1 = localDateTime1.toInstant(zoneOffset1);
System.out.println("1. Using java.sql.Date & Timestamp & toInstant() method :- \n"
+ instant1);
// 2. Using Timestamp & toInstant() method
Date date2 = new Date();
Timestamp timestamp2 = new Timestamp(date2.getTime());
LocalDateTime localDateTime2 = timestamp2.toLocalDateTime();
ZoneOffset zoneOffset2 = ZoneOffset.of("+05:30");
Instant instant2 = localDateTime2.toInstant(zoneOffset2);
System.out.println("\n2. Using Timestamp & toInstant() method :- \n"
+ instant2);
// 3. Using LocalDateTime.parse() and atZone() & toInstant() methods
Date date3 = new Date();
LocalDateTime localDateTime3 = LocalDateTime.parse(
date3.toString(),
DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy")
);
ZonedDateTime zonedDateTime3 = localDateTime3
.atZone(ZoneId.systemDefault());
Instant instant3 = zonedDateTime3.toInstant();
System.out.println("\n3. Using LocalDateTime.parse() and"
+ " atZone() & toInstant() methods :- \n"
+ instant3);
// 4. Using GregorianCalendar and toZonedDateTime() & toInstant() methods
Date date4 = new Date();
GregorianCalendar gregorianCalendar = (GregorianCalendar) Calendar.getInstance();
gregorianCalendar.setTime(date4);
Instant instant4 = gregorianCalendar // GregorianCalendar
.toZonedDateTime() // ZonedDateTime
.toInstant(); // Instant
System.out.println("\n4. Using GregorianCalendar and"
+ " toZonedDateTime() & toInstant() methods :- \n"
+ instant4);
// 5. Using Instant & toInstant() method
Date date5 = new Date();
Instant instant5 = date5.toInstant();
System.out.println("\n5. Using Instant & toInstant() method :- \n"
+ instant5);
// 6. Using Instant.ofEpochMilli() & toInstant() methods
Date date6 = new Date();
Instant instant6 = Instant.ofEpochMilli(date6.getTime());
System.out.print("\n6. Using Instant.ofEpochMilli() & toInstant() methods :- \n"
+ instant6);
}
}
Output:
Current Date is = Sat Aug 20 17:42:18 IST 2022
Date to Instant conversion :-
-----------------------------------------------------
1. Using java.sql.Date & Timestamp & toInstant() method :-
2022-08-20T12:12:18.464Z
2. Using Timestamp & toInstant() method :-
2022-08-20T12:12:18.474Z
3. Using LocalDateTime.parse() and atZone() & toInstant() methods :-
2022-08-20T12:12:18Z
4. Using GregorianCalendar and toZonedDateTime() & toInstant() methods :-
2022-08-20T12:12:18.593Z
5. Using Instant & toInstant() method :-
2022-08-20T12:12:18.611Z
6. Using Instant.ofEpochMilli() & toInstant() methods :-
2022-08-20T12:12:18.611Z
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/OffsetDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Date.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
- https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
Happy Coding !!
Happy Learning !!