Here you can find the source of getCalendarForNextRun( GregorianCalendar startTime, int targetDay, int targetHour)
Parameter | Description |
---|---|
startTime | start time |
targetDay | day of the week to choose |
targetHour | hour of the day to choose |
public static GregorianCalendar getCalendarForNextRun( GregorianCalendar startTime, int targetDay, int targetHour)
//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; } }