Java Date Start getStartDate(Date date)

Here you can find the source of getStartDate(Date date)

Description

Description:Get Monday of the week the day specified by parameter belongs to

License

Open Source License

Parameter

Parameter Description
Date date any date

Return

Date Monday of the week

Declaration

public static Date getStartDate(Date date) 

Method Source Code

//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();
    }
}

Related

  1. getStart(Date date)
  2. getStartDate(Date date)
  3. getStartDate(Date date)
  4. getStartDate(String date)
  5. getStartDateOfCurrentSemester()
  6. getStartDateOfCurrMonth()
  7. getStartDay(Date date)