Java Calendar Create getCalendarForNextRun( GregorianCalendar startTime, int targetDay, int targetHour)

Here you can find the source of getCalendarForNextRun( GregorianCalendar startTime, int targetDay, int targetHour)

Description

Given a start time, computes the next time when the wallclock will reach a certain hour of the day, on a certain day of the week Eg: From today, when is the next Saturday, 12PM ?

License

Apache License

Parameter

Parameter Description
startTime start time
targetDay day of the week to choose
targetHour hour of the day to choose

Return

calendar object representing the target time

Declaration

public static GregorianCalendar getCalendarForNextRun(
        GregorianCalendar startTime, int targetDay, int targetHour) 

Method Source Code

//package com.java2s;
/*//from   w  w w.  j av a  2s .co  m
 * Copyright 2008-2013 LinkedIn, Inc
 * 
 * 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.GregorianCalendar;

public class Main {
    /**
     * Given a start time, computes the next time when the wallclock will reach
     * a certain hour of the day, on a certain day of the week Eg: From today,
     * when is the next Saturday, 12PM ?
     * 
     * @param startTime start time
     * @param targetDay day of the week to choose
     * @param targetHour hour of the day to choose
     * @return calendar object representing the target time
     */
    public static GregorianCalendar getCalendarForNextRun(
            GregorianCalendar startTime, int targetDay, int targetHour) {
        long startTimeMs = startTime.getTimeInMillis();
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTimeInMillis(startTimeMs);

        // adjust time to targetHour on startDay
        cal.set(Calendar.HOUR_OF_DAY, targetHour);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        // check if we are past the targetHour for the current day
        if (cal.get(Calendar.DAY_OF_WEEK) != targetDay
                || cal.getTimeInMillis() < startTimeMs) {
            do {
                cal.add(Calendar.DAY_OF_YEAR, 1);
            } while (cal.get(Calendar.DAY_OF_WEEK) != targetDay);
        }
        return cal;
    }
}

Related

  1. getCalendarDayString(Calendar date)
  2. getCalendarField(char c)
  3. getCalendarField(Date date, int calFieldId)
  4. getCalendarFieldName(int field)
  5. getCalendarFormYear(int year)
  6. getCalendarForSchedule(Calendar cal)
  7. getCalendarForTime(final long timeInMilliseconds)
  8. getCalendarFromDates(Date date, Date hourMinuteMillisseconds)
  9. getCalendarFromSystemTimeInMillis(long date)