【发布时间】:2017-04-14 00:55:45
【问题描述】:
我有一个由字符串“0002-01-04T00:49:40.000”表示的日期,即公元 2 年的日期。我需要通过将其与时区 ID“Etc/UTC”相结合,将其转换为java.util.Date 的实例。以下代码显示了我是如何做到的:
public static Date toDate(LocalDateTime localDateTime, String timezoneId){
if(localDateTime == null) return null;
if(timezoneId != null) {
localDateTime.toDateTime(DateTimeZone.forID(timezoneId)).toDate();
} else {
return localDateTime.toDateTime().toDate()
}
}
但是LocalDateTime.toDate() 不能正常工作。迄今为止,它增加了 +1 天。
“0002-01-04T00:49:40.000Z”--->“太平洋标准时间 2 月 05 日星期四 16:49:40”。
【问题讨论】:
-
toDate()是JodaTime 的方法。 -
java.util.Date.toString()使用 0002 年的儒略历!所以我认为没有问题或失败。 -
@DimaSan 您发布的链接没有解决 OP 关于观察到的输出的具体问题,请参阅我的回答。
标签: java datetime jodatime to-date localdate