【发布时间】:2019-11-06 18:10:03
【问题描述】:
我试过这段代码:
public class TimePassed {
private long seconds;
private long minutes;
private long hours;
private long days;
private long years;
...
public TimePassed(double unixSeconds) {
Instant now = Instant.now();
Instant ago = Instant.ofEpochSecond((long) unixSeconds);
this.seconds = ChronoUnit.SECONDS.between(
ago.atZone(ZoneId.systemDefault()),
now.atZone(ZoneId.systemDefault())); //6100
this.minutes = ChronoUnit.MINUTES.between(
ago.atZone(ZoneId.systemDefault()),
now.atZone(ZoneId.systemDefault())); //101
this.hours = ChronoUnit.HOURS.between(
ago.atZone(ZoneId.systemDefault()),
now.atZone(ZoneId.systemDefault())); //1
this.days = ChronoUnit.DAYS.between(
ago.atZone(ZoneId.systemDefault()),
now.atZone(ZoneId.systemDefault())); //0
this.years = ChronoUnit.YEARS.between(
ago.atZone(ZoneId.systemDefault()),
now.atZone(ZoneId.systemDefault())); //0
}
}
然后TimePassed 对象将具有seconds = 6100 和minutes = 101 和hours = 1,而我希望它是hours = 1、minutes = 41, seconds = 40,以便60*60 + 41*60 + 40 = 6100。可以使用java.time 包吗?因为到目前为止,我只能通过秒、通过分钟或通过小时等。两者都不会考虑另一个。
【问题讨论】:
标签: java datetime unix-timestamp