32

How to convert LocalDateTime to OffsetDateTime?

private OffsetDateTime getEntryDate(Payment payment) {
    return Optional.ofNullable(payment)
                   .map(Payment::getEntryDate)
                   .map(SHOULD RETURN OffsetDateTime)
                   .orElse(null);
}

Payment::getEntryDate will return LocalDateTime

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
Melad Basilius
  • 3,847
  • 10
  • 44
  • 81

6 Answers6

30

There are many ways to convert LocalDateTime to OffsetDateTime. Some of them are listed below:

1. Using LocalDateTime#atOffset​(ZoneOffset offset):

LocalDateTime ldt = LocalDateTime.now();
ZoneOffset offset = ZoneOffset.UTC;
OffsetDateTime odt = ldt.atOffset(offset);

2. Using LocalDateTime#atZone​(ZoneId zone) => ZonedDateTime#toOffsetDateTime():

LocalDateTime ldt = LocalDateTime.now();

// Change the ZoneId as required e.g. ZoneId.of("Europe/London")
ZoneId zoneId = ZoneId.systemDefault();

OffsetDateTime odt = ldt.atZone(zoneId).toOffsetDateTime();

3. Using OffsetDateTime#of​(LocalDateTime dateTime, ZoneOffset offset):

LocalDateTime ldt = LocalDateTime.now();
ZoneOffset offset = ZoneOffset.UTC;
OffsetDateTime odt = OffsetDateTime.of(ldt, offset);

4. ZonedDateTime#of​(LocalDateTime localDateTime, ZoneId zone) => ZonedDateTime#toOffsetDateTime():

LocalDateTime ldt = LocalDateTime.now();

// Change the ZoneId as required e.g. ZoneId.of("Europe/London")
ZoneId zoneId = ZoneId.systemDefault();

OffsetDateTime odt = ZonedDateTime.of(ldt, zoneId).toOffsetDateTime();

Notes:

  1. In all the solutions given above, replace the sample ZoneOffset as required e.g. ZoneOffset offset = ZoneOffset.of("+02:00").
  2. In all the solutions given above, replace the sample LocalDateTime as required e.g. LocalDateTime ldt = LocalDateTime.of(2021, 3, 14, 10, 20).
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
18

You need to obtain the ZoneOffset to use when creating your OffsetDateTime. One approach is to use a ZoneId for your location:

final ZoneId zone = ZoneId.of("Europe/Paris");
LocalDateTime localDateTime = LocalDateTime.now();
ZoneOffset zoneOffSet = zone.getRules().getOffset(localDateTime);
OffsetDateTime offsetDateTime = localDateTime.atOffset(zoneOffSet);
System.out.println(offsetDateTime); // 2019-08-08T09:54:10.761+02:00
CeeTee
  • 778
  • 1
  • 9
  • 17
0

How about:

 OffsetDateTime convertToOffsetDateTime(LocalDateTime ldt) {
        ZoneOffset offset = OffsetDateTime.now().getOffset();
        OffsetDateTime offsetDateTime = ldt.atOffset(offset);
        return offsetDateTime;
    }
Christof
  • 1
  • 1
  • explain it in detail – abhinavsinghvirsen Oct 13 '20 at 17:03
  • You give a LocalDateTime object and create a offset on your localmaschine. Then you create an offsetDateTime with this offset and return it. The original Offset ist lost anyway because LocalDateTime don't store it, so we have to create it – Christof Oct 21 '20 at 14:36
  • 3
    if you want the offset of the jvm default timezone, you can write it in a single line : `ldt.atZone(ZoneId.systemDefault()).toOffsetDateTime()` – Olivier Boissé Oct 30 '20 at 21:53
0

An OffsetDateTime is just a date time with an offset from UTC.

So if you have a fixed offset (e.g. +02 from UTC), you can convert the localDateTime like this :

OffsetDateTime.of(localDateTime, ZoneOffset.of("+2"));
OffsetDateTime.of(localDateTime, ZoneOffset.of("+02"));
OffsetDateTime.of(localDateTime, ZoneOffset.of("+02:00"));

Most of the time you want to have the offset of a specific timezone, in this case it would be preferable to use a ZonedDateTime because for most timezone the offset is not the same in summer/winter and ZonedDateTime will automatically handle it for you.

If you absolutely want an OffsetDateTime with an offset from a specific timezone, you can write :

localDateTime.atZone(ZoneId.of("Europe/Paris")).toOffsetDateTime();
Olivier Boissé
  • 15,834
  • 6
  • 38
  • 56
0

Here is my solution:

public Instant toInstant(LocalDate date) {
    return date
        .atStartOfDay()
        .toInstant(ZoneOffset.UTC);
}

public OffsetDateTime toOffsetDateTime(LocalDate date) {
    return OffsetDateTime.ofInstant(
        toInstant(date),
        ZoneOffset.UTC
    );
}
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
0

If you want to convert a specific LocalDateTime to an OffsetDateTime this might help you:

final LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime = " + localDateTime);

final ZoneOffset offset = ZoneOffset.ofHours(3);
final OffsetDateTime offsetDateTimeRef = OffsetDateTime.now(offset);
System.out.println("offsetDateTimeRef = " + offsetDateTimeRef);

final OffsetDateTime offsetDateTimeFromLocalDateTime = OffsetDateTime.ofInstant(localDateTime.toInstant(ZoneId.systemDefault().getRules().getOffset(localDateTime)), offset);
System.out.println("offsetDateTimeFromLocalDateTime = " + offsetDateTimeFromLocalDateTime);

Output:

localDateTime                   = 2022-11-11T23:58:34.260550200
offsetDateTimeRef               = 2022-11-12T01:58:34.262501700+03:00
offsetDateTimeFromLocalDateTime = 2022-11-12T01:58:34.260550200+03:00