Here you can find the source of getStartDate(Date date)
Description:Get Monday of the week the day specified by parameter belongs to
Parameter | Description |
---|---|
Date | date any date |
public static Date getStartDate(Date date)
//package com.java2s; /*/*from ww w .ja va 2 s. c o m*/ * $RCSfile: DatetimeUtil,v $$ * $Revision: 1.0 $ * $Date: 2011 $ * * Copyright (C) 2011 GyTech, Inc. All rights reserved. * * This software is the proprietary information of GyTech, Inc. * Use is subject to license terms. */ import java.util.Calendar; import java.util.Date; public class Main { /**array reprsents offset bewwen Monday and current date, start from Sunday*/ private static final int[] offsets = { -6, 0, -1, -2, -3, -4, -5 }; /** *<p>Description:Get Monday of the week the day specified by parameter belongs to</p> * @param Date date any date * @return Date Monday of the week */ public static Date getStartDate(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); /*get day of week */ int day = calendar.get(Calendar.DAY_OF_WEEK); /*get offset between current date and Monday of the week*/ int offset = offsets[day - Calendar.SUNDAY]; /*adjust to Monday*/ calendar.add(Calendar.DAY_OF_YEAR, offset); return calendar.getTime(); } }