计算两个日期之间的分钟数

时间:2021-04-20 06:10:07

标签: android datetime

如何计算两个日期之间的分钟数?

开始日期 20/4/2021 09:00

结束日期 28/4/2021 09:00

结果应该是 11520 Minute

我只想要分钟而不计算天、小时或秒,我该怎么做?

2 个答案:

答案 0 :(得分:1)

您可以像这样使用 ZonedDateTime.until 方法:

ZonedDateTime start = ZonedDateTime.of(2021, 4, 20, 9, 0, 0, 0, ZoneId.of("GMT"));
ZonedDateTime end = ZonedDateTime.of(2021, 4, 28, 9, 0, 0, 0, ZoneId.of("GMT"));
long mins = start.until(end, ChronoUnit.MINUTES);
System.out.println(mins);

答案 1 :(得分:1)

首先将日期转换为毫秒,然后减去并转换回分钟。

例如,

// Works, `obj.foo` is string
findObjectByKey("foo", "x", [{ foo: "y" }])
// Works, `obj[42]` is string
findObjectByKey(42, "x", ["1"])
// Works, `obj.foo` is number
findObjectByKey("foo", 42, [{ foo: 27, bar: "x" }])
// Error as desired, `obj.foo` is number but `value` is string
findObjectByKey("foo", "x", [{ foo: 27, bar: "x" }])
//                     ^−−−−− Argument of type 'string' is not assignable to parameter of type 'number'.