尝试将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转换为ZonedDateTimeZonedDateTime提供了 ZonedDateTime.ofInstant(Instant, ZoneId) 方法。所以

因此,假设您要在默认时区输入ZonedDateTime,则您的代码应为

ZonedDateTime d = ZonedDateTime.ofInstant(calculateFromDate.toInstant(),
                                          ZoneId.systemDefault());

09-04 06:27
查看更多