gpt4 book ai didi

java - 如何在 YMDHMS (ISO 8601) 中获取两个日期/时间之间的持续时间

转载 作者:行者123 更新时间:2023-12-05 02:29:12 24 4
gpt4 key购买 nike

给定 2 日期,我如何根据 ISO 8601Year Month Day, Hour Minute Second 格式计算它们之间的差异持续时间?

我只找到了可以在几天或更短时间内给出差异的 Java 库。

鉴于月份和年份的天数不规则,我不确定如何找出月份和年份的差异。

即使 Duration.between() 很接近,但它以小时分钟秒为单位给出结果:

ZonedDateTime event1 = ZonedDateTime.of(2022, 2, 2, 2, 2, 2, 0, ZoneId.of("UTC-7"));
ZonedDateTime event2 = ZonedDateTime.of(2025, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC-7"));
//ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC-7"));
Duration duration = Duration.between(event1, event2);
Log.d("Duration ISO-8601: ", duration.toString());


Output: PT25533H57M58S

也就是 25533 小时57 分钟58

我想看到这样的东西:

__ years, __ months, __ days, 9 hours, 57 minutes, 58 seconds

最佳答案

您可以创建一个自定义类来维护 PeriodDuration 字段(归功于 @Ole V.V.,因为他早些时候在评论)。

下面是此类实现的示例,它公开了一个静态方法 between(),它需要两个类型为 LocalDateTime 的参数。

getYears()getHours() 等方法会将调用委托(delegate)给 PeriodDuration 对象。

class DateTimeSlot {
private Period period;
private Duration duration;

private DateTimeSlot(Period period, Duration duration) {
this.period = period;
this.duration = duration;
}

public int getYears() {
return period.getYears();
}

public int getMonth() {
return period.getMonths();
}

public int getDays() {
return period.getDays();
}

public int getHours() {
return duration.toHoursPart(); // this method can be safely used instead `toHours()` because `between()` implementation guerantees that duration will be less than 24 hours
}

public int getMinutes() {
return duration.toMinutesPart();
}

public int getSeconds() {
return (int) (duration.getSeconds() % 60);
}

public static DateTimeSlot between(LocalDateTime from, LocalDateTime to) {
if (from.isAfter(to) || from.equals(to)) {
throw new IllegalArgumentException();
}

Duration duration;
Period period;

if (from.toLocalTime().isBefore(to.toLocalTime())) {
duration = Duration.between(from.toLocalTime(), to.toLocalTime());
period = Period.between(from.toLocalDate(), to.toLocalDate());
} else {
duration = Duration.between(to.withHour(from.getHour())
.withMinute(from.getMinute())
.withSecond(from.getSecond())
.minusDays(1), to); // apply shift one day back
period = Period.between(from.toLocalDate()
.plusDays(1), to.toLocalDate()); // apply shift one day forward (to compensate the previous shift)
}
return new DateTimeSlot(period, duration);
}

@Override
public String toString() {
return String.format("%s years, %s months, %s days, %s hours, %s minutes, %s seconds",
getYears(), getMonth(), getDays(), getHours(), getMinutes(), getSeconds());
}
}

main() - 演示

public static void main(String[] args) {
LocalDateTime now = LocalDateTime.of(2022, 5, 20, 18, 0, 18);
LocalDateTime withTimeBefore = LocalDateTime.of(2020, 12, 31, 15, 9, 27);
LocalDateTime withTimeAfter = LocalDateTime.of(2020, 12, 31, 22, 50, 48);

DateTimeSlot slot1 = DateTimeSlot.between(withTimeBefore, now);
DateTimeSlot slot2 = DateTimeSlot.between(withTimeAfter, now);

System.out.println(slot1);
System.out.println(slot2);
}

输出

1 years, 4 months, 20 days, 2 hours, 50 minutes, 51 seconds  // between 2020-12-31T15:09:27 and 2022-05-20T18:00:18
1 years, 4 months, 19 days, 19 hours, 9 minutes, 30 seconds // between 2020-12-31T22:50:48 and 2022-05-20T18:00:18

关于java - 如何在 YMDHMS (ISO 8601) 中获取两个日期/时间之间的持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72321174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com