Extract of some useful tip I've posted on stackoverflow,
how to schedule task at fixed time at specified timezone:
how to schedule task at fixed time at specified timezone:
timer = new Timer("Timer", true);
Calendar cr = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cr.setTimeInMillis(System.currentTimeMillis());
long day = TimeUnit.DAYS.toMillis(1);
//Pay attention - Calendar.HOUR_OF_DAY for 24h day model
//(Calendar.HOUR is 12h model, with p.m. a.m. )
cr.set(Calendar.HOUR_OF_DAY, it.getHours());
cr.set(Calendar.MINUTE, it.getMinutes());
long delay = cr.getTimeInMillis() - System.currentTimeMillis();
//insurance for case then time of task is before time of schedule
long adjustedDelay = (delay > 0 ? delay : day + delay);
timer.scheduleAtFixedRate(new StartReportTimerTask(it), adjustedDelay, day);
//you can use this schedule instead is sure your time is after current time
//timer.scheduleAtFixedRate(new StartReportTimerTask(it), cr.getTime(), day);
No comments:
Post a Comment