尝试将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());