我在尝试将 java.util.Date
转换为 java.time.LocalDate
时遇到以下异常。
java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: 2014-08-19T05:28:16.768Z of type java.time.Instant
代码如下:
public static Date getNearestQuarterStartDate(Date calculateFromDate){
int[] quaterStartMonths={1,4,7,10};
Date startDate=null;
ZonedDateTime d=ZonedDateTime.from(calculateFromDate.toInstant());
int frmDateMonth=d.getMonth().getValue();
我使用 ZonedDateTime
类的方式有问题吗?
根据文档,这应该将 java.util.Date
对象转换为 ZonedDateTime
。上面的日期格式是标准日期吗?
我必须使用 Joda 时间吗?
如果有人可以提供一些建议,那就太好了。
最佳答案
要将 Instant
转换为 ZonedDateTime
,ZonedDateTime
提供了 ZonedDateTime.ofInstant(Instant, ZoneId)
方法.所以
因此,假设您希望 ZonedDateTime
在默认时区,您的代码应该是
ZonedDateTime d = ZonedDateTime.ofInstant(calculateFromDate.toInstant(),
ZoneId.systemDefault());
关于Java8 java.util.Date 转换为 java.time.ZonedDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25376242/