public static <T extends Calendar> T round(final T calendar, final int field) {
        N.checkArgNotNull(calendar, "calendar");

        final Calendar rounded = (Calendar) calendar.clone();
        modify(rounded, field, ModifyType.ROUND);

        if (rounded.getClass().equals(calendar.getClass())) {
            return (T) rounded;
        }

        return createCalendar(calendar, rounded.getTimeInMillis());
    }
public static < T extends Calendar > T round ( final T calendar , final int field ) { N . checkArgNotNull ( calendar , "calendar" ) ; final Calendar rounded = ( Calendar ) calendar . clone ( ) ; modify ( rounded , field , ModifyType . ROUND ) ; if ( rounded . getClass ( ) . equals ( calendar . getClass ( ) ) ) { return ( T ) rounded ; } return createCalendar ( calendar , rounded . getTimeInMillis ( ) ) ; }
Copied from Apache Commons Lang under Apache License v2.
<br />

<p>Rounds a date, leaving the field specified as the most
significant field.</p>

<p>For example, if you had the date-time of 28 Mar 2002
13:45:01.231, if this was passed with HOUR, it would return
28 Mar 2002 14:00:00.000. If this was passed with MONTH, it
would return 1 April 2002 0:00:00.000.</p>

<p>For a date in a timezone that handles the change to daylight
saving time, rounding to Calendar.HOUR_OF_DAY will behave as follows.
Suppose daylight saving time begins at 02:00 on March 30. Rounding a
date that crosses this time would produce the following values:
</p>
<ul>
<li>March 30, 2003 01:10 rounds to March 30, 2003 01:00</li>
<li>March 30, 2003 01:40 rounds to March 30, 2003 03:00</li>
<li>March 30, 2003 02:10 rounds to March 30, 2003 03:00</li>
<li>March 30, 2003 02:40 rounds to March 30, 2003 04:00</li>
</ul>

@param calendar  the date to work with, not null
@param field  the field from {@code Calendar} or <code>SEMI_MONTH</code>
@return the different rounded date, not null
@throws IllegalArgumentException if the date is <code>null</code>
@throws ArithmeticException if the year is over 280 million