Java Date Previous getPreviousFriday(final Date d)

Here you can find the source of getPreviousFriday(final Date d)

Description

Finds the Friday preceding the date.

License

Apache License

Parameter

Parameter Description
d date to be evaluated

Return

date adjusted to the previous Friday

Declaration

public static Date getPreviousFriday(final Date d) 

Method Source Code


//package com.java2s;
/*//from  w  ww  . ja  v  a 2  s .  com
 * Copyright 2014 Scott J. Johnson (http://scottjjohnson.com)
 *
 * Licensed 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;
import java.util.TimeZone;

public class Main {
    private static final TimeZone exchangeTZ = TimeZone.getTimeZone("America/New_York");

    /**
     * Finds the Friday preceding the date. If the date is already a Friday, the original date is returned. Time
     * properties in the Date object are not modified.
     *
     * @param d date to be evaluated
     *
     * @return date adjusted to the previous Friday
     */
    public static Date getPreviousFriday(final Date d) {

        Calendar cal = getStockExchangeCalendar();
        cal.setTime(d);

        int dow = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_WEEK, -(dow + 1) % 7);

        return cal.getTime();
    }

    /**
     * Gets a reference to a Calendar object with the right time zone for the NYSE.
     *
     * @return Calendar in the stock exchange time zone
     */
    public static Calendar getStockExchangeCalendar() {
        return new GregorianCalendar(exchangeTZ);
    }
}

Related

  1. getPreviousDate(int period)
  2. getPreviousDateStart()
  3. getPreviousDay(Date date)
  4. getPreviousDay(Date dateTime)
  5. getPreviousDay(Date inputDate)
  6. getPreviousMonday(Date day)
  7. getPreviousMonth(Date time)
  8. getPreviousMonth(long date)
  9. getPreviousMonthFirst(Date appointDate)