How To Convert a Java8 LocalDate to OffsetDateTime

The Java 8 LocalDate represents a date value without a time zone in the ISO-8601 calendar system, whereas the Java 8 OffsetDateTime is a date-time representation with an offset value from UTC.

You can use the OffsetDateTime.of(localDate, localTime, zoneoffset) method to convert a Java 8 LocalDate value to the OffsetDateTime object.

In this tutorial, you will learn how to convert a LocalDate value to an OffsetDateTime value in Java 8.

Using OffsetDateTime.of()

The Java 8 OffsetDateTime expects date-time value along with time-zone.
To convert a LocalDate value to OffsetDatetime using OffsetDateTime.of(),

  • Invoke OffsetDateTime.of() method with below arguments.
  • LocalDate – date value to convert
  • LocalTime – time value can be either system time(LocalTime.now()) or parse a specific time using LocalTime.parse() or create a time using the LocalTime.of() method.
  • ZoneOffset – time-zone offset value or ZoneOffset.UTC for UTC time-zone.

Use this method when you have a date and time. Also preferred compared to others as it is a feature of OffsetDateTime.

Check this tutorial to learn about converting a string to LocalDate, How to convert a String to Java 8 LocalDate.

Code

    import java.time.LocalDate;  
    import java.time.LocalTime;  
    import java.time.OffsetDateTime;  
    import java.time.ZoneOffset;

    //main
    LocalDate localDate = LocalDate.now();  
    System.out.println(localDate);  //2023-03-04

    OffsetDateTime offsetDateTime = OffsetDateTime.of(localDate, LocalTime.of(12,12,12), ZoneOffset.UTC);  
    System.out.println(offsetDateTime);  //2023-03-04T12:12:12Z

Using LocalDate.atTime()

To convert a LocalDate to OffsetDateTime using the LocalDate.atTime() method,

  • Invoke the localDate.atTime() method with the OffsetTime value, which returns offsetDateTime.
  • OffsetTime represent time with an offset from UTC.

Use this method to convert to a specific time in a given time zone.

Code

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

    //main
    LocalDate localDate = LocalDate.now();
    OffsetDateTime offsetDateTime1 = localDate.atTime(OffsetTime.now(ZoneId.systemDefault()));  
    System.out.println(offsetDateTime1);  //2023-03-04T12:55:01.110640500Z

    OffsetDateTime offsetDateTime2 = localDate.atTime(OffsetTime.MIN);  
    System.out.println(offsetDateTime2);  //2023-03-04T00:00+18:00

Using LocalDate.atStartOfDay()

Convert a LocalDate to OffsetDateTime using the localDate.atStartOfDay() method.

  • Invoke localDate.atStartOfDay() method creates a LocalDateTime object with time set to midnight
  • Use .atOffset(ZoneOffset.UTC) to convert the date-time value to the OffsetDateTime object.
  • If Invoked with zoneId(.atStartOfDay(ZoneOffset.UTC)), then the date with the earliest reasonable time -zone value is returned and use .toOffsetDateTime() method to get the OffsetDateTime object.

Use this method to have time value, earliest to time-zone/ UTC time zone.

Code

    import java.time.LocalDate;   
    import java.time.OffsetDateTime;  
    import java.time.ZoneId;  
    import java.time.ZoneOffset;

    //main
    LocalDate localDate = LocalDate.now();
    OffsetDateTime offsetDateTime1 = localDate.atStartOfDay().atOffset(ZoneOffset.UTC);  
    System.out.println(offsetDateTime1);  //2023-03-04T00:00Z
    OffsetDateTime offsetDateTime2 = localDate.atStartOfDay(ZoneOffset.UTC).toOffsetDateTime();  
    System.out.println(offsetDateTime2);  //2023-03-04T00:00Z
    OffsetDateTime offsetDateTime3 = localDate.atStartOfDay(ZoneId.systemDefault()).toOffsetDateTime();  
    System.out.println(offsetDateTime3);  //2023-03-04T00:00Z

Related Topics

Leave a Comment