如何将LocalDateTime转换为OffsetDateTime?

32

如何将LocalDateTime转换为OffsetDateTime

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

Payment::getEntryDate将返回LocalDateTime


3
OffsetDataTime::of(LocalDataTime, ZoneOffset) 是什么意思? - Eugene
如何获取该ZoneOffset? - Melad Basilius
从哪里?这取决于你自己去了解。 - Eugene
我的意思是 LocalDataTime 就是一个 本地的 时间... 无论你想应用什么偏移量都由你决定,有一些预定义的类型,最常见的是 UTC - Eugene
我需要保留所有信息,因此我无法猜测要使用什么ZoneOffset。 - Melad Basilius
6个回答

32

有很多种方法可以将LocalDateTime转换为OffsetDateTime,下面列出了其中一些方法:

1. 使用LocalDateTime#atOffset​(ZoneOffset offset)

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

2. 使用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. 使用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();

注意事项:

  1. 在上述所有解决方案中,根据需要替换示例 ZoneOffset,例如: ZoneOffset offset = ZoneOffset.of("+02:00")
  2. 在上述所有解决方案中,根据需要替换示例 LocalDateTime,例如: LocalDateTime ldt = LocalDateTime.of(2021, 3, 14, 10, 20)

18

在创建OffsetDateTime时,您需要获取ZoneOffset。一种方法是使用适用于您所在位置的ZoneId:

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

0
如果您想将特定的LocalDateTime转换为OffsetDateTime,可以尝试以下方法:
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);

输出:

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

0

这是我的解决方案:

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

public OffsetDateTime toOffsetDateTime(LocalDate date) {
    return OffsetDateTime.ofInstant(
        toInstant(date),
        ZoneOffset.UTC
    );
}

0

这样怎么样:

 OffsetDateTime convertToOffsetDateTime(LocalDateTime ldt) {
        ZoneOffset offset = OffsetDateTime.now().getOffset();
        OffsetDateTime offsetDateTime = ldt.atOffset(offset);
        return offsetDateTime;
    }

explain it in detail - abhinav
你提供一个LocalDateTime对象并在本地机器上创建一个偏移量。然后,使用此偏移量创建一个OffsetDateTime并返回它。 原始的偏移量无论如何都会丢失,因为LocalDateTime不会存储它,所以我们必须创建它。 - Christof
3
如果你想获取JVM默认时区的偏移量,可以将以下代码写成一行:ldt.atZone(ZoneId.systemDefault()).toOffsetDateTime() - Olivier Boissé

0

OffsetDateTime 是一个带有与 UTC 偏移量的日期时间。

因此,如果您有一个固定的偏移量(例如从 UTC +02),您可以像这样转换 localDateTime:

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

大多数情况下,您希望获得特定时区的偏移量,在这种情况下最好使用 ZonedDateTime,因为对于大多数时区,夏季/冬季的偏移量不同,ZonedDateTime 将自动为您处理。
如果您绝对需要具有特定时区偏移量的 OffsetDateTime,可以编写以下代码:
localDateTime.atZone(ZoneId.of("Europe/Paris")).toOffsetDateTime();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接