Java ZonedDateTime Calculate getDuration(ZonedDateTime input, TemporalField roundTo, int roundIncrement)

Here you can find the source of getDuration(ZonedDateTime input, TemporalField roundTo, int roundIncrement)

Description

Returns the time in nanoseconds (long) until the next rounded time.

License

Open Source License

Parameter

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

Return

the duration until the rounded time in long

Declaration

private static long getDuration(ZonedDateTime input, TemporalField roundTo, int roundIncrement) 

Method Source Code


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

Related

  1. diffMs(ZonedDateTime date1, ZonedDateTime date2)
  2. elapsedTime(ZonedDateTime zonedDateTime)
  3. equals(ZonedDateTime ZDT1, ZonedDateTime ZDT2)
  4. filter(final Set list, final ZonedDateTime base, final long minute)
  5. findIdx(int startidx, int endidx, ZonedDateTime endDate, List holidays)
  6. getShortStringDate(ZonedDateTime time)
  7. getStartOfDay(ZoneId zoneId, ZonedDateTime time)
  8. getStartOfWeek(ZoneId zoneId, ZonedDateTime time)
  9. getTime(ZonedDateTime zonedTime)