【问题标题】:How DO I convert ZoneId to DateTimeZone in Java如何在 Java 中将 ZoneId 转换为 DateTimeZone
【发布时间】:2016-08-20 19:35:16
【问题描述】:

如何将 ZoneId 转换为 DateTimeZone? 我有:

 Private Instant logTime;  //java.time
    Private ZoneId timeZoneId;   //java.time

但我需要从上面获取 DateTime 和 DateTimeZone(均为 org.joda.time)。我从 Instant 得到 DateTime 喜欢 =

Private DateTime  dateTime ;   //org.joda.time
private DateTimeZone dateTimeZone;   //org.joda.time

LocalDateTime currentTime = new LocalDateTime(logTime);
dateTime = currentTime.toDateTime();

对于 dateTimeZone,我做到了:

dateTimeZone = DateTimeZone.forID(timeZoneId.getId()); // getting java.lang.NullPointerException

但出现错误。有人可以告诉mw如何正确地做到这一点

这给了我错误。有人可以告诉我应该如何转换它。我需要获取给定 zoneId 的当前时间。

【问题讨论】:

  • A ZoneId 没有任何日期/时间信息。您的意思是获取特定区域 ID 中的当前时间吗?
  • 所以我可以获取特定区域 id 中的当前时间(localTimezone)?
  • 你能edit你的帖子说明你有什么和你想要什么吗?因为目前还不清楚。
  • jodatime 中没有ZoneId 类。这是来自java.time 的课程。混合使用这两个 API 会引起很多混乱,因为许多类的名称相同。例如,不清楚private Instant logTime 是java.time.Instant 还是org.joda.time.Instant
  • 用您遇到的错误更新您的问题。请阅读How to Ask 和minimal reproducible example 了解指南。

标签: datetime java-8 jodatime


【解决方案1】:

我遇到了相同的时区转换问题,我可以通过调用org.joda.time.DateTimeZone#forTimeZone(java.util.TimeZone zone) 完成此帖子Converting from java.util.TimeZone to org.joda.DateTimeZone 上的@basil-bourque 回答来实现转换。

final java.time.ZoneId timeZoneId;
final org.joda.time.DateTimeZone timeZone = org.joda.time.DateTimeZone.forTimeZone(java.util.TimeZone.getTimeZone(timeZoneId))

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 2023-04-01
    相关资源
    最近更新 更多