Here you can find the source of truncateToWeek(GregorianCalendar date)
Parameter | Description |
---|---|
date | A GregorianCalendar object. |
public static GregorianCalendar truncateToWeek(GregorianCalendar date)
//package com.java2s; /*//from w w w. j a v a2 s . c om * Copyright (C) 2014 Dell, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Calendar; import java.util.GregorianCalendar; public class Main { /** * 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. * * @param date A GregorianCalendar object. * @return A copy of the same value, truncated to the nearest Monday. */ 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; } }