Here you can find the source of roundCalToNextQuarterHour(GregorianCalendar cal)
Parameter | Description |
---|---|
cal | calendar to preform calculations on |
public static GregorianCalendar roundCalToNextQuarterHour(GregorianCalendar cal)
//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; } }