How to convert LocalDateTime to OffsetDateTime?
Asked Answered
L

6

35

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

Lampedusa answered 1/4, 2019 at 12:4 Comment(5)
OffsetDataTime::of(LocalDataTime, ZoneOffset)?Sniperscope
How to get that ZoneOffset ?Lampedusa
from where? it is up to you to know itSniperscope
I mean a LocalDataTime is exactly that, a Local... whatever the offset you want to apply is up to you, there are some predefined types, the most common UTC I guessSniperscope
I need to keep all the information, so i cant guess what ZoneOffset to use.Lampedusa
P
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
Popovich answered 8/8, 2019 at 9:2 Comment(0)
N
38

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).
Nuclei answered 14/3, 2021 at 7:33 Comment(0)
P
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
Popovich answered 8/8, 2019 at 9:2 Comment(0)
T
0

How about:

 OffsetDateTime convertToOffsetDateTime(LocalDateTime ldt) {
        ZoneOffset offset = OffsetDateTime.now().getOffset();
        OffsetDateTime offsetDateTime = ldt.atOffset(offset);
        return offsetDateTime;
    }
Thorsten answered 13/10, 2020 at 9:10 Comment(3)
explain it in detailChief
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 itThorsten
if you want the offset of the jvm default timezone, you can write it in a single line : ldt.atZone(ZoneId.systemDefault()).toOffsetDateTime()Feathercut
I
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();
Inkblot answered 30/10, 2020 at 21:47 Comment(0)
R
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
    );
}
Recline answered 29/1, 2021 at 9:0 Comment(0)
B
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
Braxton answered 11/11, 2022 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.