Java 8 - 将LocalDate转换为ZonedDateTime

Sel*_*ght 19 java java-8 java-time

我是java.time包的新手.我有一个LocalDate 2015-12-10.我需要将其转换为ZonedDateTime.时间应为00:00:00,区域为ZoneOffset.UTC.

转换后,ZonedDateTime应为2015-12-10T00:00:00 + 02:00.

我将LocalDate存储在名为startDate的变量中.

我试过了:

ZonedDateTime.ofInstant(Instant.from(startDate), ZoneOffset.UTC)
Run Code Online (Sandbox Code Playgroud)

但得到错误

java.time.DateTimeException:无法从TemporalAccessor获取Instant:2015-12-10,类型为java.time.LocalDate]

我也尝试过:

startDate.atStartOfDay().atZone(ZoneOffset.UTC)
Run Code Online (Sandbox Code Playgroud)

这给出了意外的结果.

我查看了API并尝试了其他一些方法,但到目前为止还没有运气.

有没有其他方法可以将LocalDate转换为ZonedDateTime?

Jod*_*hen 34

将较少特定对象转换为更具体的对象时,请使用'at'方法:

ZonedDateTime zdt = startDate.atStartOfDay(ZoneOffset.UTC);
Run Code Online (Sandbox Code Playgroud)

传递UTC偏移量不会得到+02:00的结果,这表明您正在尝试实现其他目标.