public static GregorianCalendar truncateToWeek ( GregorianCalendar date ) { // Round the date down to the MONDAY of the same week. GregorianCalendar result = ( GregorianCalendar ) date . clone ( ) ; switch ( result . get ( Calendar . DAY_OF_WEEK ) ) { case Calendar . TUESDAY : result . add ( Calendar . DAY_OF_MONTH , - 1 ) ; break ; case Calendar . WEDNESDAY : result . add ( Calendar . DAY_OF_MONTH , - 2 ) ; break ; case Calendar . THURSDAY : result . add ( Calendar . DAY_OF_MONTH , - 3 ) ; break ; case Calendar . FRIDAY : result . add ( Calendar . DAY_OF_MONTH , - 4 ) ; break ; case Calendar . SATURDAY : result . add ( Calendar . DAY_OF_MONTH , - 5 ) ; break ; case Calendar . SUNDAY : result . add ( Calendar . DAY_OF_MONTH , - 6 ) ; break ; default : break ; } return result ; }
Truncate the given GregorianCalendar date to the nearest week . This is done by cloning it and rounding the value down to the closest Monday . If the given date already occurs on a Monday a copy of the same date is returned .