是否有等效于ChronoUnit.between的函数返回分数而不是整数? [英] Is there a equivalent to ChronoUnit.between that returns fraction instead of integer?

查看:651
本文介绍了是否有等效于ChronoUnit.between的函数返回分数而不是整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ChronoUnit.HOURS.between(start, end)这样的方法会返回long,所以我无法从那里获取分数.

Methods such is ChronoUnit.HOURS.between(start, end) returns long so I can't get the fraction from there.

有没有其他方法/方法可以返回分数?

Is there an alternative method/approach that would return a fraction?

推荐答案

ChronoUnit.HOURS.between(start, end)的全部要点是获取两个时间点之间的HOURS数.例如:间隔1或2个小时,没有1.5658个小时*.如果您需要更多的精度,请使用另一个ChronoUnit,即分钟或秒.

The whole point of ChronoUnit.HOURS.between(start, end) is to get the number of HOURS between the two timepoints. For example: it's either 1 or 2 hours between, there is no such thing as 1.5658 hours*. If you need more precicion, use another ChronoUnit, i.e. Minutes or seconds.

十进制小数的问题在于,它们通常基于10,而时间单位是基于圆(360°,2pi等)的分数,可以更好地用1/4、1//等整数表示8、1/2等,而不是浮点值.此处的关键字是浮点数",您可以通过移动该点数来表示一个值,或者将基值乘以10的幂并使用单位前缀(即1.5 * 10 ^ 3 == 1500000 * 10 ^ -3 ,其中10 ^ 3是千克,而10 ^ -3是毫米),但是您不能用几天,几小时,几分钟和几秒钟来做到这一点.没有千小时或毫秒之类的东西,并且您不能简单地通过乘以10 *的幂来将小时转换为分钟.

The problem with decimal fractions is, that they are usually based on 10, while time units are fractions based on a circle (360°, 2pi, etc) which are better expressed in fractions of integers like 1/4, 1/8, 1/2, etc. and not in a floating point value. The keyword here is "floating point", you could express a value by moving the point or multiply a base-value with powers of 10 and use a prefix to the unit (i.e. 1.5 * 10^3 == 1500000 * 10^-3, where 10^3 is kilo and 10^-3 is milli), but you can't do that with days, hours, minutes and seconds. There is no such thing as kilo-hour or milli-days and you can't simply translate hours to minutes by multipling a power of 10*.

在现实生活中,没有人会说是1小时和8.3%"或"6.5小时后"-因为这通常会导致混乱(6.30 vs 6:30)-所以说是1小时5分钟"更为实用"或"x过去5点"或"x的四分之一".

In real life nobody would say "it's 1 hour and 8.3%" or "6.5 hours later" - as this usually ends up in confusion (6.30 vs 6:30) - so it's more practical to say "it's 1hr and 5mins" or "5 past x" or "a quarter to x".

因此,在时间API *中进行十进制分数计算并没有实际的用途.但是,如果您需要它(即出于计算目的),则可以很简单地对其进行计算.

So there is no real practical use for having a decimal fraction calculation in a time API*. But in case you need it (i.e. for computational purposes), you may quite simply calculate it.

如果您需要分数(即分钟或秒),则必须使用相应的ChronoUnit(即ChronoUnit.MINUTESChronoUnit.SECONDS),然后根据所需的整体时间单位数来进行划分.占有一小部分.例如

If you require a fraction - i.e. minutes or seconds - you have to use the corresponding ChronoUnit, i.e. ChronoUnit.MINUTES or ChronoUnit.SECONDS, and devide it by the amount of time units required for the whole you'd like to have a fraction of. For example

  • 60分钟创造一个小时
  • 3600秒创造了一个小时
  • 3600000毫秒使一个小时,等等
  • 60秒创造一分钟
  • ...

您可以使用简单的数学公式(即

You may calculate the fraction with minute-precision using simple math i.e.

double fracH = (double)ChronoUnit.MINUTES.between(start,end) / 60;

您也可以使用提供了between方法的Duration.持续时间可以轻松地转换为分钟,小时等.

As alternative you could use Duration which offers a between method, too. A duration can be easily converted into Minutes, Hours etc.

Duration dur = Duration.between(start, end);
double fracH = (double)dur.toMinutes() / 60;

要获得最高的精度,您必须以最小的时间单位(即毫微秒或毫秒)计算分数

To get the highest precision, you have to calculate the fraction on the smallest timeunit, i.e. nanos or millis

double fracHns = (double)dur.toNanos() / 3_600_000_000_000;

您甚至可以使用Duration来获取基本(100%)值:

You may even use Duration to get the base (the 100%) value:

//fraction of an hour
double fracHns = (double)dur.toNanos() / Duration.ofHours(1).toNanos();

//fraction of a minute
double fracMns = (double)dur.toNanos() / Duration.ofMinutes(1).toNanos();

或更笼统,例如 David SN已发布

TemporalUnit unit = ChronoUnit.HOURS;
double frac = (double)Duration.between(start, end).toNanos() 
              / Duration.of(1, unit).toNanos();

*)没有规则,无一例外:对于时间单位,事情变得困难,对于小于1秒的单位,计算基准会更改,因为这样它就会以10为基础,即毫秒(1/1000),微秒(1 /1000000th),...因此,对于秒数或更短的时间,通常使用十进制小数,例如:1.74秒,因为只需移动十进制分隔符即可将其转换为下一个较小的时间单位:

*) No rule without exception: To make things difficult, for time units the calculation base changes for units smaller than 1 second, because then it gets 10-based, i.e. milli-seconds (1/1000th), microseconds (1/1000000th),... So for seconds and smaller it is common to use decimal fractions, for example: 1.74 second, because it is simple to translate it to the next-smaller time unit by simply moving the decimal separator:

1.74 s <-> 1740.0 ms. 

但这不是那么容易的事情:

But it's not that easy for hours:

1.74 hours <-> 104.40 minutes.

这篇关于是否有等效于ChronoUnit.between的函数返回分数而不是整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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