Java 8 – How to convert LocalDate to an OffsetDateTime ?

In this article, we will learn how to convert LocalDate to OffsetDateTime using atTime() method of LocalDate provided in Java 1.8 version

For OffsetDateTime to LocalDate conversion, read Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from OffsetDateTime ?

Convert LocalDate to OffsetDateTime :

  • LocalDate has a method atTime() which takes OffsetTime as argument and returns OffsetDateTime
    • atTime() – Returns a Offset date-time from invoking LocalDate with an offset time
  • Using this method, it is very easy to convert LocalDate to OffsetDateTime
  • After conversion, OffsetDateTime have Date part same as that of LocalDate and Time part will consist of hour, minute, second, nano and offset information, for example 20220801T15:50:58.797738+05:30
  • Lets see an example for conversion of LocalDate to OffsetDateTime in the below illustration

ConvertLocalDateToOffsetDateTime.java

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

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.OffsetTime;

public class ConvertLocalDateToOffsetDateTime {

	public static void main(String[] args) {

		// 1. get current System Date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate);


		// 2. get current Offset Time
		OffsetTime offsetTime = OffsetTime.now();
		System.out.println("\nSystem Offset Time is :- \n" + offsetTime);


		// 3. convert LocalDate to OffsetDateTime using atTime(OffsetTime time)
		OffsetDateTime offsetDateTime = localDate.atTime(offsetTime);
		System.out.print("\nConversion of LocalDate to OffsetDateTime is :- \n"
				+ offsetDateTime);
	}
}

Output:

Current System Date is :- 
2022-08-01

System Offset Time is :- 
15:59:13.511289300+05:30

Conversion of LocalDate to OffsetDateTime is :- 
2022-08-01T15:59:13.511289300+05:30

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDate to an Instant ?
Java 8 – How to convert LocalDate to ZonedDateTime ?