本文介绍了如何将Instant转换为LocalTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在阅读了大量教程之后,我也不是真正了解Temporal Adjusters或Java的新时间库。

I'm not really understanding Temporal Adjusters or Java's new time library even after reading numerous tutorials.

如何将Instant对象转换为LocalTime对象。我正在考虑以下几点:

How would I convert an Instant object to a LocalTime object. I was thinking something along the lines of the following:

LocalTime time = LocalTime.of(
        instantStart.get(ChronoField.HOUR_OF_DAY),
        instantStart.get(ChronoField.MINUTE_OF_HOUR)
    );

但它不起作用。我该怎么做?

But it isn't working. How would I do this?

推荐答案

我理解它的方式......即时是一种UTC风格的时间,区域不可知世界标准时间。 LocaleTime是给定区域的时间。因此,如果Instant实现TemporalAccessor,

The way I understand it... Instant is a UTC style time, agnostic of zone always UTC. LocaleTime is time at a given zone. So you'd expect the following would work given that Instant implements TemporalAccessor,

Instant instant = Instant.now();
LocalTime local =  LocalTime.from(instant);

但是你得到无法从TemporalAccessor获取LocalTime错误。相反,你需要说明本地的位置。没有默认值 - 可能是件好事。

but you get "Unable to obtain LocalTime from TemporalAccessor" error. Instead you need to state where "local" is. There is no default - probably a good thing.

Instant instant = Instant.now();
LocalTime local =  LocalTime.from(instant.atZone(ZoneId.of("GMT+3")));
System.out.println(String.format("%s => %s", instant, local));

输出

2014-12-07T07:52:43.900Z => 10:52:43.900

instantStart.get(ChronoField.HOUR_OF_DAY)抛出错误,因为它不是概念上的支持它,你只能通过LocalTime实例访问HOUR_OF_DAY等。

instantStart.get(ChronoField.HOUR_OF_DAY) throws an error because it does not conceptually support it, you can only access HOUR_OF_DAY etc. via a LocalTime instance.

这篇关于如何将Instant转换为LocalTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 09:16