Java 8:多个单位中两个 LocalDateTime 之间的差异 [英] Java 8: Difference between two LocalDateTime in multiple units
问题描述
我正在尝试计算两个 LocalDateTime
之间的差异.
输出的格式需要为y 年 m 月 d 天 h 小时 m 分 s 秒
.这是我写的:
import java.time.Duration;导入 java.time.Instant;导入 java.time.LocalDateTime;导入 java.time.Period;导入 java.time.ZoneId;公共课主要{静态最终 int MINUTES_PER_HOUR = 60;静态最终 int SECONDS_PER_MINUTE = 60;静态最终 int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;公共静态无效主(字符串 [] args){LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 9, 19, 46, 45);LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);Period period = getPeriod(fromDateTime, toDateTime);long time[] = getTime(fromDateTime, toDateTime);System.out.println(period.getYears() + "年" +period.getMonths() + 月" +period.getDays() + " 天 " +时间[0] + "小时" +时间[1] + "分钟" +时间[2] + "秒");}private static Period getPeriod(LocalDateTime dob, LocalDateTime now) {return Period.between(dob.toLocalDate(), now.toLocalDate());}private static long[] getTime(LocalDateTime dob, LocalDateTime now) {今天的 LocalDateTime = LocalDateTime.of(now.getYear(),now.getMonthValue(), now.getDayOfMonth(), dob.getHour(), dob.getMinute(), dob.getSecond());Duration 持续时间 = Duration.between(today, now);长秒 = duration.getSeconds();长时间 = 秒/SECONDS_PER_HOUR;长分钟 = ((秒 % SECONDS_PER_HOUR)/SECONDS_PER_MINUTE);long secs = (seconds % SECONDS_PER_MINUTE);返回新的长[]{小时,分钟,秒};}}
我得到的输出是 29 年 8 个月 24 天 12 小时 0 分 50 秒
.我已经从这个(结果:29年8个月24天12小时0分钟和 50 秒).
更新
由于我从两个不同的站点得到两个不同的结果,我想知道我的计算算法是否合法.如果我使用以下两个 LocalDateTime
对象:
LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45);LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);
然后输出来了:29 years 8 months 25 days -1 hours -5 minutes -10 seconds.
从此link 它应该是 29 年 8 个月 24 天 22 小时 54 分 50 秒代码>.所以算法也需要处理负数.
请注意,问题不在于哪个网站给了我什么结果,我需要知道正确的算法并需要有正确的结果.
不幸的是,似乎没有跨时间的 period 类,因此您可能需要自己进行计算.
幸运的是,日期和时间类有很多实用方法,可以在一定程度上简化它.这是一种计算差异的方法,虽然不一定是最快的:
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45);LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime );长年 = tempDateTime.until( toDateTime, ChronoUnit.YEARS );tempDateTime = tempDateTime.plusYears(years);长月 = tempDateTime.until( toDateTime, ChronoUnit.MONTHS );tempDateTime = tempDateTime.plusMonths(months);long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS );tempDateTime = tempDateTime.plusDays( days );long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS );tempDateTime = tempDateTime.plusHours( hours );长分钟 = tempDateTime.until( toDateTime, ChronoUnit.MINUTES );tempDateTime = tempDateTime.plusMinutes( 分钟);long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS );System.out.println(年+年"+月 + 月" +天 + 天" +小时 + " 小时 " +分钟 + "分钟" +秒 + "秒");//打印:29 年 8 个月 24 天 22 小时 54 分 50 秒.
基本思想是:创建一个临时的开始日期并获得完整的年份结束.然后按年数调整该日期,使开始日期距结束日期不到一年.按降序对每个时间单位重复此操作.
最后免责声明:我没有考虑不同的时区(两个日期应该在同一时区),我也没有测试/检查夏令时或其他变化日历(如萨摩亚的时区变化)会影响此计算.所以请谨慎使用.
I am trying to calculate the difference between two LocalDateTime
.
The output needs to be of the format y years m months d days h hours m minutes s seconds
. Here is what I have written:
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
public class Main {
static final int MINUTES_PER_HOUR = 60;
static final int SECONDS_PER_MINUTE = 60;
static final int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
public static void main(String[] args) {
LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 9, 19, 46, 45);
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);
Period period = getPeriod(fromDateTime, toDateTime);
long time[] = getTime(fromDateTime, toDateTime);
System.out.println(period.getYears() + " years " +
period.getMonths() + " months " +
period.getDays() + " days " +
time[0] + " hours " +
time[1] + " minutes " +
time[2] + " seconds.");
}
private static Period getPeriod(LocalDateTime dob, LocalDateTime now) {
return Period.between(dob.toLocalDate(), now.toLocalDate());
}
private static long[] getTime(LocalDateTime dob, LocalDateTime now) {
LocalDateTime today = LocalDateTime.of(now.getYear(),
now.getMonthValue(), now.getDayOfMonth(), dob.getHour(), dob.getMinute(), dob.getSecond());
Duration duration = Duration.between(today, now);
long seconds = duration.getSeconds();
long hours = seconds / SECONDS_PER_HOUR;
long minutes = ((seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
long secs = (seconds % SECONDS_PER_MINUTE);
return new long[]{hours, minutes, secs};
}
}
The output that I am getting is 29 years 8 months 24 days 12 hours 0 minutes 50 seconds
. I have checked my result from this website (with values 12/16/1984 07:45:55
and 09/09/2014 19:46:45
). The following screenshot shows the output:
I am pretty sure that the fields after the month value is coming wrong from my code. Any suggestion would be very helpful.
Update
I have tested my result from another website and the result I got is different. Here it is: Calculate duration between two dates (result: 29 years, 8 months, 24 days, 12 hours, 0 minutes and 50 seconds).
Update
Since I got two different results from two different sites, I am wondering if the algorithm of my calculation is legitimate or not. If I use following two LocalDateTime
objects:
LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45);
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);
Then the output is coming: 29 years 8 months 25 days -1 hours -5 minutes -10 seconds.
From this link it should be 29 years 8 months 24 days 22 hours, 54 minutes and 50 seconds
. So the algorithm needs to handle the negative numbers too.
Note the question is not about which site gave me what result, I need to know the right algorithm and need to have right results.
Unfortunately, there doesn't seem to be a period class that spans time as well, so you might have to do the calculations on your own.
Fortunately, the date and time classes have a lot of utility methods that simplify that to some degree. Here's a way to calculate the difference although not necessarily the fastest:
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);
LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45);
LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime );
long years = tempDateTime.until( toDateTime, ChronoUnit.YEARS );
tempDateTime = tempDateTime.plusYears( years );
long months = tempDateTime.until( toDateTime, ChronoUnit.MONTHS );
tempDateTime = tempDateTime.plusMonths( months );
long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS );
tempDateTime = tempDateTime.plusDays( days );
long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS );
tempDateTime = tempDateTime.plusHours( hours );
long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES );
tempDateTime = tempDateTime.plusMinutes( minutes );
long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS );
System.out.println( years + " years " +
months + " months " +
days + " days " +
hours + " hours " +
minutes + " minutes " +
seconds + " seconds.");
//prints: 29 years 8 months 24 days 22 hours 54 minutes 50 seconds.
The basic idea is this: create a temporary start date and get the full years to the end. Then adjust that date by the number of years so that the start date is less then a year from the end. Repeat that for each time unit in descending order.
Finally a disclaimer: I didn't take different timezones into account (both dates should be in the same timezone) and I also didn't test/check how daylight saving time or other changes in a calendar (like the timezone changes in Samoa) affect this calculation. So use with care.
这篇关于Java 8:多个单位中两个 LocalDateTime 之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!