2 localTime之间有多少unitTime(10分钟半小时)?Java 8 [英] How many unitTime(10 mins, half hour) there are between 2 localTime? Java 8

查看:48
本文介绍了2 localTime之间有多少unitTime(10分钟半小时)?Java 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一个单位时间,例如12分钟或25分钟,以了解Java中2 LocalTime之间有多少单位时间.

I want to define a unit Time for example, 12 minutes or 25 minutes, for know how many unit Times there are between 2 LocalTime in Java.

例如,如果我定义了15分钟(如单位时间),则在8:00至10:00之间,我应该获得8次.

For Example, if I defined 15 minutes like unit time, between 8:00 and 10:00, I should get 8 times.

推荐答案

您可以使用

You can use Duration class to get the duration between two LocalTime values. Then you can calculate the custom time units yourself:

int minutesUnit = 15;
LocalTime startTime = LocalTime.of(8, 0);
LocalTime endTime = LocalTime.of(10, 0);
Duration duration = Duration.between(startTime, endTime);
long unitsCount = duration.toMinutes() / minutesUnit;
System.out.println(unitsCount);

这将打印 8 .

如果您使用不同的时间单位,则可以将持续时间细分为毫秒并计算结果:

If you have different time units you could break the duration down to millis and calculate the result:

long millisUnit = TimeUnit.MINUTES.toMillis(15);
// ...
long unitsCount = duration.toMillis() / millisUnit;

这篇关于2 localTime之间有多少unitTime(10分钟半小时)?Java 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆