Java Week Day getLastWeekDay(int weekDay)

Here you can find the source of getLastWeekDay(int weekDay)

Description

get Last Week Day

License

Apache License

Declaration

public static Date getLastWeekDay(int weekDay) 

Method Source Code

//package com.java2s;
/**/*from  ww  w .j a  v a2  s .  c  o m*/
 * DateTimeUtil.java
 * <p>
 * Copyright 2010 @company@, Inc.
 *
 * @company@ licenses this file to you 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:
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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.Date;
import java.util.GregorianCalendar;

public class Main {
    public static final long SECOND = 1000;
    public static final long MINUTE = 60 * SECOND;

    public static Date getLastWeekDay(int weekDay) {
        return getLastWeekDay(weekDay, new Date());
    }

    public static Date getLastWeekDay(Date end) {
        if (end == null)
            throw new IllegalArgumentException("The parameter [end date] should not be null!");
        GregorianCalendar cal = getCalender(end);
        cal.add(Calendar.DAY_OF_MONTH, -7);

        return cal.getTime();
    }

    public static Date getLastWeekDay(int weekDay, Date end) {
        if (end == null)
            throw new IllegalArgumentException("The parameter [end date] should not be null!");
        GregorianCalendar cal = getCalender(end);
        cal.set(Calendar.DAY_OF_WEEK, weekDay);

        if (!cal.getTime().before(end)) {
            cal.add(Calendar.DAY_OF_MONTH, -7);
        }
        return cal.getTime();
    }

    private static GregorianCalendar getCalender(Date date) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);

        return cal;
    }
}

Related

  1. getLastDayOfCurrWeek()
  2. getLastDayOfWeek(int year, int month)
  3. getLastSundayBeforeThisWeek()
  4. getLastWeekday()
  5. getLastWeekDay(int weekDay)
  6. getLastWeekMs()
  7. getMon()
  8. getMonday(String date)
  9. getMonday(String date, int weekDay)