Java Day of Week getWeekStart(Date date)

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

Description

Returns week start date.

License

Open Source License

Parameter

Parameter Description
date the date

Return

week start date

Declaration

public static Date getWeekStart(Date date) 

Method Source Code


//package com.java2s;
import java.util.Calendar;
import java.util.Date;

public class Main {
    /** The Constant MILLISECOND. */
    public final static long MILLISECOND = 1l;

    /**/*from   w  w  w .ja  va2  s .  c o  m*/
     * Returns week start date.
     *
     * @param date the date
     * @return week start date
     */
    public static Date getWeekStart(Date date) {

        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        int day = calendar.get(Calendar.DAY_OF_WEEK);
        if (day == Calendar.SUNDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -6);
            return calendar.getTime();
        }

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return calendar.getTime();
    }
}

Related

  1. getWeekOfYear(Date date)
  2. getWeekOfYear(Date date)
  3. getWeekOfYear(Date date)
  4. getWeekOfYear(String date)
  5. getWeeksBetweenDate(Date begin, Date end)
  6. getWeekStart(Date date)
  7. getWeekStartDateBeforeCurrent(int weekNum, Date current)
  8. getWeekth(String sDate)
  9. getYearOfWeek(Date date)