Java ZonedDateTime Calculate roundTimeMinQuarter(ZonedDateTime dateTime)

Here you can find the source of roundTimeMinQuarter(ZonedDateTime dateTime)

Description

Round the date time in argument to the nearest quarter (e.g.

License

Open Source License

Parameter

Parameter Description
dateTime the date time to round

Return

The rounded date time

Declaration

static public ZonedDateTime roundTimeMinQuarter(ZonedDateTime dateTime) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class Main {
    /** Round the date time in argument to the nearest quarter 
     *  (e.g. 23:57 -> 0:00 (the next day))
     * @param dateTime the date time to round
     * @return The rounded date time//from ww  w  . j  a  va  2s.c o  m
     */
    static public ZonedDateTime roundTimeMinQuarter(ZonedDateTime dateTime) {
        //Round the sec
        int unroundedSec = dateTime.getSecond();
        if (unroundedSec >= 30)
            dateTime = dateTime.plusMinutes(1);
        // We get rid of nanoseconds et seconds
        dateTime = dateTime.truncatedTo(ChronoUnit.MINUTES);

        //Round the min
        int unroundedMin = dateTime.getMinute();
        int div = unroundedMin / 15;
        int mod = unroundedMin % 15;
        if (mod >= 8)
            div++;
        if (div == 4) {
            dateTime = dateTime.plusHours(1);
            div = 0;
        }
        dateTime = dateTime.withMinute(div * 15);
        return dateTime;
    }
}

Related

  1. parseZdtToDate(ZonedDateTime zdt)
  2. parseZonedDateTime(String zonedDateTimeString, String variableName)
  3. preProcess(String Val, ZonedDateTime BaseTimeMarker)
  4. printDateTimeCompact(ZonedDateTime ZDT, boolean PrintTime, boolean PrintSeconds)
  5. printDateTimeSuperCompact(ZonedDateTime ZDT)
  6. roundUp(ZonedDateTime date)
  7. secondsSinceMidnight(ZonedDateTime ZDT)
  8. simplifyZonedDateTime(ZonedDateTime now, long timeToWait)
  9. stringToZonedDateTimeUTC(String dateAsString)