![](/img/trans.png)
[英]How would I convert the exact amount of time (years, months, days, hours, minutes, seconds) between two Calendar objects?
[英]How to change time unit from seconds to days/months/years
我反对Java的Temporal API。 我在Java文档或其他任何地方都找不到任何可以回答我需要做的事情。 我只需要以10亿秒为单位返回一个日期并计算该日期,即某人活出10亿秒(大约30年左右)的时间。
public Temporal getGigasecondDate(Temporal given) {
final long gigasecond = 1000000000;
final long gigaDays = gigasecond / 60 / 60 / 24;
Duration amount = Duration.ofDays(gigaDays);
Temporal date = amount.addTo(given);
return date;
}
我收到一条错误消息,说Exception in thread "main" java.time.DateTimeException: Unit must be Years, Months or Days, but was Seconds
,我一生无法在JavaDocs或其他任何地方找到方法,弄清楚如何使单位为年,月或日。
我需要做的就是返回一个Temporal对象,该对象声明某人生活了十亿秒的时刻。
ZonedDateTime.of( // Mom gave birth at 3 AM in year 2000, New Zealand time.
2000 , 1 , 23 , 3 , 45 , 0 , 0 , ZoneId.of( "Pacific/Auckland" )
) // Returns a `ZonedDateTime` object.
.plus(
Duration.ofSeconds( 1_000_000_000L ) // A billion seconds.
) // Returns another `ZonedDateTime` object. Immutable objects in *java.time* means we get a fresh object rather than “mutating” (altering) the original.
.toString() // Generate a `String` representing the value of our `ZonedDateTime` object, using standard ISO 8601 format wisely extended to append the name of the time zone in square brackets.
2031-10-01T05:31:40 + 13:00 [太平洋/奥克兰]
Temporal
java.time的文档解释说,通常在应用程序中,您应该使用具体的类,而不是接口和超类。 从Temporal
接口引用:
该接口是框架级别的接口,不应在应用程序代码中广泛使用。 相反,应用程序应创建并传递具体类型的实例,例如LocalDate。 造成这种情况的原因很多,部分原因是该接口的实现可能在ISO以外的日历系统中。 有关问题的更详细讨论,请参见ChronoLocalDate。
[将] 1,000,000,000秒添加到日期并返回日期,
确定该人的出生时刻。
LocalDate ld = LocalDate.of( 2000 , Month.JANUARY , 23 ) ;
LocalTime lt = LocalTime.of( 3 , 45 ) ; // Mom gave birth at 3 AM in New Zealand time.
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
如果有用,请在UTC中查看同一时刻。
Instant instant = zdt.toInstant() ;
定义添加时间。 Duration
类表示未附加到时间轴上的时间跨度。
Duration d = Duration.ofSeconds( 1_000_000_000L ) ;
加。
ZonedDateTime zdtLater = zdt.plus( d ) ;
或者,以UTC(相同的时刻,不同的时钟时间)。
Instant instantLater = instant.plus( d ) ;
看到此代码在IdeOne.com上实时运行 。
zdt.toString():2000-01-23T03:45 + 13:00 [太平洋/奥克兰]
Instant.toString():2000-01-22T14:45:00Z
d.toString():PT277777H46M40S
zdtLater.toString():2031-10-01T05:31:40 + 13:00 [太平洋/奥克兰]
InstantLater.toString():2031-09-30T16:31:40Z
如果您只关心日期,没有时间和时区,则提取LocalDate
。
LocalDate ldLater = zdtLater.toLocalDate() ;
您可以调用plus(long amountToAdd, TemporalUnit unit)
,单位为ChronoUnit.SECONDS
:
public static Temporal getGigasecondDate(Temporal given) {
return given.plus(1_000_000_000, ChronoUnit.SECONDS);
}
测试
System.out.println(getGigasecondDate(LocalDateTime.of(2000, 1, 1, 0, 0)));
System.out.println(getGigasecondDate(ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.of("America/New_York"))));
输出量
2031-09-09T01:46:40
2031-09-09T02:46:40-04:00[America/New_York]
因此,假设没有夏令时,那么在2000年1月1日午夜出生的人将在2031年9月9日上午1:46:40达到10亿秒的年龄。
如果他们住在纽约,那么就会是美国东部时间凌晨2:46:40。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.