Java Day of Week getLastWeekDay(int weekDay, Date end)

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

Description

get Last Week Day

License

Apache License

Declaration

public static final Date getLastWeekDay(int weekDay, Date end) 

Method Source Code

//package com.java2s;
/**//  www. j  a v a 2 s  . c om
 * DateTimeUtil.java
 *
 * 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:
 *
 *    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.Date;
import java.util.GregorianCalendar;

public class Main {

    public static final 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();
    }

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

    public static final 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();
    }

    private static final 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. getLastDayOfWeek(Date date)
  2. getLastDayOfWeek(String str, int week)
  3. getLastDayWeek(String s)
  4. getLastMondayOfWeek(Date date)
  5. getLastWeekDate(Date date)
  6. getMondayFirstOfWeek(Date baseDate)
  7. getMondayOfThisWeek(Date date)
  8. getMondayOfWeek(Date date)
  9. getNext(final java.util.Date date, final int dayofweek)