Is there an equivalent to ChronoUnit.between that returns a fraction instead of an integer?

Methods such as ChronoUnit.HOURS.between(start, end)returns long, so I can not get from there.

Is there an alternative method / approach that returns a part?

+4
source share
2 answers

The whole point ChronoUnit.HOURS.between(start, end)is to get the number of HOURs between two time points. For example: it is either 1 or 2 hours between them, there is no such thing as 1.5658 hours *. If you need more attention, 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 *.

" 1 8,3%" "6,5 " - (6.30 6:30) - " 1 5 " "" 5 x "" x ".

, API . (.. ), .

- , ChronoUnit, ChronoUnit.MINUTES ChronoUnit.SECONDS, , , .

  • 60
  • 3600
  • 3600000 ..
  • 60
  • ...

, ..

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

Duration, between. , ..

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

, ,

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

Duration, (100%) :

//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();

, 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 , , :

1.74 s <-> 1740.0 ms. 

:

1.74 hours <-> 104.40 minutes.
+4

ChronoUnit .

. Duration . , ( ) :

public static double between(Temporal startInclusive, Temporal endExclusive, ChronoUnit unit) {
    Duration duration = Duration.between(startInclusive, endExclusive);
    long conversion = Duration.of(1, unit).toNanos();
    return (double) duration.toNanos() / conversion;
}
+2

All Articles