Java 8 – How to convert OffsetDateTime to java.util.Date and vice-versa ?

In this article, we will learn how to convert OffsetDateTime to java.util.Date using toInstant() method of OffsetDateTime provided in Java 1.8 version and vice-versa

Date class in Java 1.8 version :

  • There are 2 new methods introduced in Java 1.8 version for Date class, those are
    1. from(Instant) – This static method obtains an instance of Date from an Instant object
    2. toInstant() – This method converts invoking Date object to an Instant
  • Note: Many legacy methods of Date class are deprecated

1. Convert OffsetDateTime to java.util.Date :

  • Converting OffsetDateTime to Date is very straightforward as,
    • OffsetDateTime class has toInstant() method
    • Date.from() method accepts Instant as input-argument and returns java.util.Date
  • Converted java.util.Date will have,
    • Date & Time & Offset/Zone parts same as that of OffsetDateTime
  • In short, OffsetDateTime -> Instant -> java.util.Date
  • Lets see an example for conversion of OffsetDateTime to java.util.Date in the below illustration

ConvertOffsetDateTimeToJavaUtilDate.java

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

import java.time.OffsetDateTime;
import java.util.Date;

public class ConvertOffsetDateTimeToJavaUtilDate {

	public static void main(String[] args) {

		// 1. get Offset Date/time
		OffsetDateTime offsetDateTime = OffsetDateTime.now();
		System.out.println("Offset Date/time is :- \n" 
				+ offsetDateTime);


		// 2. get default offset
		System.out.println("\nDefault Offset is :- \n" 
				+ offsetDateTime.getOffset());


		// 3. convert OffsetDateTime to java.util.Date
		Date date = Date.from(offsetDateTime.toInstant());
		System.out.print("\nConversion of OffsetDateTime to java.util.Date is :- \n"
				+ date);
	}
}

Output:

Offset Date/time is :- 
2022-08-17T11:50:30.510221200+05:30

Default Offset is :- 
+05:30

Conversion of OffsetDateTime to java.util.Date is :- 
Wed Aug 17 11:50:30 IST 2022

2. Convert java.util.Date to OffsetDateTime :

  • Get java.util.Date instantiating Date object for conversion to OffsetDateTime
  • Conversion steps
    1. Convert Date to Instant using toInstant() method
    2. And then invoke atOffset() method passing ZoneOffset as argument which returns OffsetDateTime
  • In short, java.util.Date -> Instant -> OffsetDateTime
  • Read Java 8 – How to convert java.util.Date to an OffsetDateTime in different ways ? for more details and examples
  • Lets see an example for conversion of java.util.Date to OffsetDateTime in the below illustration

ConvertJavaUtilDateToOffsetDateTime.java

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

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Date;

public class ConvertJavaUtilDateToOffsetDateTime {

	public static void main(String[] args) {

		// 1. get current Date/time
		Date date = new Date();
		System.out.println("Current Date/time is :- \n" 
				+ date);


		// 2. get system default zone
		ZoneOffset zoneOffset = ZoneOffset.of("+05:30");
		System.out.println("\nZoneOffset is :- \n" 
				+ zoneOffset);


		// 3. convert java.util.Date to OffsetDateTime
		OffsetDateTime offsetDateTime  = date.toInstant().atOffset(zoneOffset);
		System.out.print("\nConversion of java.util.Date to an OffsetDateTime is :- \n"
				+ offsetDateTime);
	}
}

Output:

Current Date/time is :- 
Wed Aug 17 11:52:03 IST 2022

ZoneOffset is :- 
+05:30

Conversion of java.util.Date to an OffsetDateTime is :- 
2022-08-17T11:52:03.066+05:30

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert OffsetDateTime to java.sql.Timestamp and vice-versa ?
Java 8 – How to convert OffsetDateTime to number of Seconds ?