Some things about programming, design and methodologies, both in English and in Russian. Translation of some articles that I like into Russian.
Некоторые мысли о программировании, архитектуре и методологиях. Перевод понравившихся статей на Русский.

Oct 12, 2012

Schedule at fixed time

Extract of some useful tip I've posted on stackoverflow,
how to schedule task at fixed time at specified timezone:

timer =newTimer("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 schedulelong adjustedDelay =(delay >0? delay : day + delay);
timer.scheduleAtFixedRate(newStartReportTimerTask(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);