Java Hour getTaskTrigger(int taskTriggerHour, boolean tomorrow)

Here you can find the source of getTaskTrigger(int taskTriggerHour, boolean tomorrow)

Description

Get date of next time for triggering task

License

Open Source License

Parameter

Parameter Description

Return

Date of next trigger

Declaration

public static Date getTaskTrigger(int taskTriggerHour, boolean tomorrow) 

Method Source Code


//package com.java2s;

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

public class Main {
    /**//  ww w. jav  a 2s. c  o m
     * Get date of next time for triggering task
     * @param taskTriggerHour: hour of triggering task
     * @return Date of next trigger
     */
    public static Date getTaskTrigger(int taskTriggerHour, boolean tomorrow) {
        return getTaskTrigger(taskTriggerHour, 0, tomorrow);
    }

    /**
     * Get date of next time for triggering task
     * @param taskTriggerHour: hour of triggering task
     * @param taskTriggerMinute: minutes of triggering task
     * @return Date of next trigger
     */
    public static Date getTaskTrigger(int taskTriggerHour, int taskTriggerMinute, boolean tomorrow) {
        // change to start from given time
        Calendar cal = getCalendar();
        cal.set(Calendar.HOUR_OF_DAY, taskTriggerHour);
        cal.set(Calendar.MINUTE, taskTriggerMinute);

        if (tomorrow) {
            cal.add(Calendar.DAY_OF_YEAR, 1);
        } else {
            /*
            int curHour = cal.get(Calendar.HOUR_OF_DAY);
            int curMinute = cal.get(Calendar.MINUTE);
            if ((curHour > taskTriggerHour) || 
               ((curHour == taskTriggerHour) && (curMinute > taskTriggerMinute)))
            {
               cal.add(Calendar.MINUTE, 2);
            }
            */

        }
        return cal.getTime();
    }

    /**
     * Get calendar instance with default time zone of GMT+8 
     * @return Calendar instance
     */
    public static Calendar getCalendar() {
        TimeZone.setDefault(getDefaultTimeZone());
        return Calendar.getInstance();
    }

    /**
     * Get default time zone of China
     * @return default time zone
     */
    public static TimeZone getDefaultTimeZone() {
        return TimeZone.getTimeZone("GMT+8");
    }
}

Related

  1. getMinOfHour(long l1)
  2. getPreHour()
  3. getPreHourText(String pattern)
  4. getPrevHour(Date date)
  5. getSecondsPassedInHour()
  6. getTicksTill(int hour)
  7. getTicksTillHour()
  8. hour()
  9. hour()