Java Day of Week getNextDay(long date, int startOfWeek)

Here you can find the source of getNextDay(long date, int startOfWeek)

Description

Returns the first day after date that has the day of week matching startOfWeek.

License

Open Source License

Parameter

Parameter Description
date Base date
startOfWeek Calendar constant correspoding to start of week.

Return

start of week, return value will have 0 hours, 0 minutes, 0 seconds and 0 ms.

Declaration

public static long getNextDay(long date, int startOfWeek) 

Method Source Code


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

public class Main {
    private static Calendar CALENDAR = Calendar.getInstance();

    /**/*from   w  w  w . j a  v  a 2 s. co  m*/
     * Returns the first day after <code>date</code> that has the
     * day of week matching <code>startOfWeek</code>.  For example, if you
     * want to find the next monday relative to <code>date</code> you
     * would call <code>getPreviousDay(date, Calendar.MONDAY)</code>.
     *
     * @param date Base date
     * @param startOfWeek Calendar constant correspoding to start of week.
     * @return start of week, return value will have 0 hours, 0 minutes,
     *         0 seconds and 0 ms.
     *
     */
    public static long getNextDay(long date, int startOfWeek) {
        return getDay(date, startOfWeek, 1);
    }

    private static long getDay(long date, int startOfWeek, int increment) {
        Calendar calendar = CALENDAR;
        synchronized (calendar) {
            calendar.setTimeInMillis(date);
            int day = calendar.get(Calendar.DAY_OF_WEEK);
            // Normalize the view starting date to a week starting day
            while (day != startOfWeek) {
                calendar.add(Calendar.DATE, increment);
                day = calendar.get(Calendar.DAY_OF_WEEK);
            }
            return startOfDayInMillis(calendar.getTimeInMillis());
        }
    }

    /**
    * Returns day in millis with the hours, milliseconds, seconds and minutes
    * set to 0.
    *
    * @param date long used in calculating start of day
    * @return Start of <code>date</code>
    */
    public static long startOfDayInMillis(long date) {
        Calendar calendar = CALENDAR;
        synchronized (calendar) {
            calendar.setTimeInMillis(date);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MINUTE, 0);
            return calendar.getTimeInMillis();
        }
    }
}

Related

  1. getMondayOfThisWeek(Date date)
  2. getMondayOfWeek(Date date)
  3. getNext(final java.util.Date date, final int dayofweek)
  4. getNextClosestDateTime(DayOfWeek[] daysOfWeek, int hour, int min)
  5. getNextClosestDateTime(DayOfWeek[] daysOfWeek, int hour, int min)
  6. getNextWeekDay(Date startDate, int day)
  7. getSatDayOfWeek(Date date)
  8. getScheduledDate(int dayOfWeek, int hourOfDay, int minute, int second)
  9. getSeqWeek(String date)