Java Calendar Calculate roundCalToNextQuarterHour(GregorianCalendar cal)

Here you can find the source of roundCalToNextQuarterHour(GregorianCalendar cal)

Description

Returns a calendar that has been rounded to the next quarter hour and zeros out the seconds and milliseconds.

License

Apache License

Parameter

Parameter Description
cal calendar to preform calculations on

Return

cal calendar to return

Declaration

public static GregorianCalendar roundCalToNextQuarterHour(GregorianCalendar cal) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**/*from   w  w w.  j a v  a2 s. c om*/
     * 1 hour, represented as millis
     */
    public static final long HOUR = (1000 * 60) * 60;
    /**
     * 1 minute, represented as millis
     */
    public static final long MINUTE = 1000 * 60;
    /**
     * 1 second, represented as millis
     */
    public static final long SECOND = 1000;

    /**
     * Returns a calendar that has been rounded to the next quarter hour and zeros out the seconds and milliseconds.
     *
     * @param cal calendar to preform calculations on
     * @return cal calendar to return
     */
    public static GregorianCalendar roundCalToNextQuarterHour(GregorianCalendar cal) {
        int minutes = cal.get(Calendar.MINUTE);

        if (minutes > 45) {
            cal.set(Calendar.MINUTE, 0);
            cal.add(Calendar.HOUR, 1);
        } else if (minutes > 30) {
            cal.set(Calendar.MINUTE, 45);
        } else if (minutes > 15) {
            cal.set(Calendar.MINUTE, 30);
        } else if (minutes > 0) {
            cal.set(Calendar.MINUTE, 15);
        } else {
            cal.set(Calendar.MINUTE, 0);
        }

        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal;
    }
}

Related

  1. normalize(final Calendar cal)
  2. normalizedClone(final Calendar cal)
  3. postpone(final Calendar calendar, final int measureUnit, final int amount)
  4. resetCalendarTime(Calendar calendar)
  5. roundBack(Calendar start)
  6. transform(Calendar cal)
  7. truncate(Calendar ca)
  8. truncateHour(Calendar ca)