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

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 –

  1. Using java.sql.Date & Timestamp & toInstant() method
  2. Using Timestamp & toInstant() method
  3. Using LocalDateTime.parse() and atZone() & toInstant() methods
  4. Using GregorianCalendar and toZonedDateTime() & toInstant() methods
  5. Using Instant & toInstant() method
  6. 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:



References:



Happy Coding !!
Happy Learning !!

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