gpt4 book ai didi

java - 如何区分两个 JodaTime LocalTime 时间戳?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:09:32 26 4
gpt4 key购买 nike

我正在使用 JodaTime LocalTime为了指定时间。如何获得两次之间的分钟数差异?

LocalTime startT = new LocalTime(6,30);
LocalTime endT = new LocalTime(12,15);

// LocalTime duration = this.endT.minusMinutes(startT);
// int durationMinutes = duration.getMinutes();

最佳答案

如果你问

How can I get the difference in minutes between two times?

然后我会想到以下更直观的解决方案:

LocalTime startT = new LocalTime(6,30);
LocalTime endT = new LocalTime(12,15);
int minutes = Minutes.minutesBetween(startT, endT).getMinutes(); // 345m = 6h - 15m

表达式

Period.fieldDifference(startT, endT).getMinutes()

只会产生 -15,这意味着不考虑时差,因此不会将小时转换为分钟。如果第二个表达式确实是您想要的,那么问题应该是这样的:

如何获得两个 LocalTime 实例的分钟部分之间的差异?

针对 Java-8 更新:

LocalTime startT = LocalTime.of(6,30);
LocalTime endT = LocalTime.of(12,15);

long minutes = ChronoUnit.MINUTES.between(startT, endT);

System.out.println(minutes); // output: 345

// Answer to a recommendation in a comment:
// Attention JSR-310 has no typesafety regarding units (a bad design choice)
// => UnsupportedTemporalTypeException: Unsupported unit: Minutes
minutes = Duration.between(startT, endT).get(ChronoUnit.MINUTES);

关于java - 如何区分两个 JodaTime LocalTime 时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22374151/

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