Here you can find the source of getDuration(ZonedDateTime input, TemporalField roundTo, int roundIncrement)
Parameter | Description |
---|---|
input | ZonedDateTime, the time from which the duration is calculated |
roundTo | the unit that is rounded, e.g. ChronoField.MINUTE_OF_HOUR |
roundIncrement | the number in the unit of roundTo we want the time to round to |
private static long getDuration(ZonedDateTime input, TemporalField roundTo, int roundIncrement)
//package com.java2s; //License from project: Open Source License import java.time.Duration; import java.time.ZonedDateTime; import java.time.temporal.TemporalField; public class Main { /**/*from w ww. jav a2 s . c o m*/ * Returns the time in nanoseconds (long) until the next rounded time. * * If e.g. the parameters are ZonedDateTime.now(), ChronoField.MINUTE_OF_HOUR, 15, it will return the time until * the next quarter hour. * @param input ZonedDateTime, the time from which the duration is calculated * @param roundTo the unit that is rounded, e.g. ChronoField.MINUTE_OF_HOUR * @param roundIncrement the number in the unit of roundTo we want the time to round to * @return the duration until the rounded time in long */ private static long getDuration(ZonedDateTime input, TemporalField roundTo, int roundIncrement) { int field = input.get(roundTo); int r = field % roundIncrement; ZonedDateTime ceiling = input.plus(roundIncrement - r, roundTo.getBaseUnit()) .truncatedTo(roundTo.getBaseUnit()); return Duration.between(input, ceiling).getSeconds(); } }