Java Day of Week previousWeek(long date)

Here you can find the source of previousWeek(long date)

Description

Returns the week before date.

License

Open Source License

Parameter

Parameter Description
date Date used in calculating previous week

Return

week before date.

Declaration

public static long previousWeek(long date) 

Method Source Code

//package com.java2s;
/*/*from w  ww  .  j  a  va2 s. com*/
 * $Id: DateUtils.java,v 1.4 2005/10/10 18:02:45 rbair Exp $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * Returns the week before <code>date</code>.
     *
     * @param date Date used in calculating previous week
     * @return week before <code>date</code>.
     */
    public static long previousWeek(long date) {
        return addDays(date, -7);
    }

    /**
     * Adds <code>amount</code> days to <code>time</code> and returns the
     * resulting time.
     *
     * @param time Base time
     * @param amount Amount of increment.
     *
     * @return the <var>time</var> + <var>amount</var> days
     */
    public static long addDays(long time, int amount) {
        Calendar calendar = Calendar.getInstance();
        synchronized (calendar) {
            calendar.setTimeInMillis(time);
            calendar.add(Calendar.DAY_OF_MONTH, amount);
            return calendar.getTimeInMillis();
        }
    }

    /**
     * Adds the specified number of days to todays date. If endOfDay is true,
     * the date returned is the end of the calculated date
     *
     * @param amount Number of days to add.
     * @param endOfDay flag to indicate the end of the day
     * @return the new date
     */
    public static Date addDays(int amount, boolean endOfDay) {
        Calendar cal = Calendar.getInstance();
        return addDays(cal.getTime(), amount, endOfDay);
    }

    /**
     * Adds the specified number of days to the stated. If endOfDay is true, the
     * date returned is the end of the calculated date
     *
     * @param The date to add the specified number of days to.
     * @param amount Number of days to add.
     * @param endOfDay flag to indicate the end of the day
     * @return the new date
     */
    public static Date addDays(Date baseDate, int amount, boolean endOfDay) {
        Calendar cal = Calendar.getInstance();
        synchronized (cal) {
            cal.setTime(baseDate);
            cal.add(Calendar.DATE, amount);
        }
        if (endOfDay) {
            return endOfDay(cal.getTime());
        }
        return cal.getTime();
    }

    /**
     * Returns the last millisecond of the specified date.
     *
     * @param date Date to calculate end of day from
     * @return Last millisecond of <code>date</code>
     */
    public static Date endOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        synchronized (calendar) {
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MINUTE, 59);
            return calendar.getTime();
        }
    }
}

Related

  1. javaCalendarToNewTimeApi(int dayOfWeek)
  2. mergeDayIntoDate(Date baseDate, int dayOfWeek)
  3. nextDayOfWeek(Date fromDate, int dow)
  4. nextWeek(Date date)
  5. nextWeek(final Date date)
  6. previousWeek(long date)
  7. startOfWeek(Date date)
  8. toStartWeek(Date date)
  9. toWeekDayUS(Date date, int expect)