Timer Schedule TimerTask for same hour every day

Discussion in 'Java' started by Tomer, Jun 29, 2008.

  1. Tomer

    Tomer Guest

    If the current time is 18:50 and i schedule Repeating daily TimerTask
    to 18:52 then everything is file its being run at 18:52 daily however
    if i schedule it to 18:48 then its being run immediately (and i dont
    want that) as i run the application :(
    However i wanted it to be run every day at 18:48! how can i achieve
    that?

    following is my test code:

    public class TestTimer {

    public static class TestTimerTask extends TimerTask {

    public void run() {
    System.out.println(new Date() + " timer run...");
    }
    }

    public static void main(String[] args) throws Exception {
    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();
    // Timer starts yesterday (so that surely will run today when time
    comes.
    // date.setTimeInMillis(new Date().getTime() - 1000 * 60 * 60 * 24);
    DateFormat sdf = new SimpleDateFormat("HH:mm");
    Date dateToBackup = null;
    try {
    dateToBackup = sdf.parse("18:32");
    } catch (ParseException e) {
    e.printStackTrace();
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dateToBackup);
    date.set(Calendar.HOUR, calendar.get(Calendar.HOUR));
    date.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
    date.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
    date.set(Calendar.MILLISECOND, 0);

    // Schedule to run every day.
    timer.schedule(
    new TestTimerTask(),
    date.getTime(),
    1000 * 60 * 60 * 24
    );
    Thread.sleep(120000);
    }

    }

    Thanks

    Tomer
    Tomer, Jun 29, 2008
    #1
    1. Advertising

  2. Tomer

    Arne Vajhøj Guest

    Tomer wrote:
    > If the current time is 18:50 and i schedule Repeating daily TimerTask
    > to 18:52 then everything is file its being run at 18:52 daily however
    > if i schedule it to 18:48 then its being run immediately (and i dont
    > want that) as i run the application :(
    > However i wanted it to be run every day at 18:48! how can i achieve
    > that?


    > Timer timer = new Timer();
    > Calendar date = Calendar.getInstance();
    > // Timer starts yesterday (so that surely will run today when time
    > comes.
    > // date.setTimeInMillis(new Date().getTime() - 1000 * 60 * 60 * 24);
    > DateFormat sdf = new SimpleDateFormat("HH:mm");
    > Date dateToBackup = null;
    > try {
    > dateToBackup = sdf.parse("18:32");
    > } catch (ParseException e) {
    > e.printStackTrace();
    > }
    > Calendar calendar = Calendar.getInstance();
    > calendar.setTime(dateToBackup);
    > date.set(Calendar.HOUR, calendar.get(Calendar.HOUR));
    > date.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
    > date.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
    > date.set(Calendar.MILLISECOND, 0);


    Why not just roll one day forward ?

    >
    > // Schedule to run every day.
    > timer.schedule(
    > new TestTimerTask(),
    > date.getTime(),
    > 1000 * 60 * 60 * 24
    > );
    > Thread.sleep(120000);
    > }
    >
    > }


    Arne
    Arne Vajhøj, Jun 29, 2008
    #2
    1. Advertising

  3. Tomer

    ilkinulas Guest

    if timer execution time is earlier then the current time then
    roll one day forward.

    public void scheduleTimer(int hour, int minute) {
    Calendar now = new GregorianCalendar();
    int hourNow = now.get(Calendar.HOUR_OF_DAY);
    int minuteNow = now.get(Calendar.MINUTE);

    Calendar firstExecutionDate = new GregorianCalendar();
    firstExecutionDate.set(Calendar.HOUR_OF_DAY, hour);
    firstExecutionDate.set(Calendar.MINUTE, minute);
    firstExecutionDate.set(Calendar.SECOND, 0);
    firstExecutionDate.set(Calendar.MILLISECOND, 0);
    if (hour<hourNow || (hour==hourNow && minute<minuteNow)) {
    //Do not execute today, first execution will be tomorrow.
    firstExecutionDate.add(Calendar.DAY_OF_MONTH, 1);
    }

    long oneDay = 1000L * 60L * 60L * 24L;

    Timer timer = new Timer();
    timer.schedule(new MyTask(), firstExecutionDate.getTime(), oneDay);

    }
    ilkinulas, Jun 29, 2008
    #3
  4. Tomer

    Roedy Green Guest

    On Sun, 29 Jun 2008 08:47:59 -0700 (PDT), Tomer <>
    wrote, quoted or indirectly quoted someone who said :

    >However i wanted it to be run every day at 18:48! how can i achieve
    >that?


    I don't think that is a sensible way to handle the problem. You would
    need an JVM sitting in RAM 24-7.

    Better to use some light weight chron feature of the OS.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Jun 30, 2008
    #4
  5. Tomer

    Arne Vajhøj Guest

    Roedy Green wrote:
    > On Sun, 29 Jun 2008 08:47:59 -0700 (PDT), Tomer <>
    > wrote, quoted or indirectly quoted someone who said :
    >> However i wanted it to be run every day at 18:48! how can i achieve
    >> that?

    >
    > I don't think that is a sensible way to handle the problem. You would
    > need an JVM sitting in RAM 24-7.
    >
    > Better to use some light weight chron feature of the OS.


    cron

    There is that little aspect called portability.

    But if it is a server running 24 x 7 it should probably
    use Java EE and scheduling has been part of Java EE since
    version 1.4 !

    Arne
    Arne Vajhøj, Jun 30, 2008
    #5
  6. Tomer

    Tomer Guest

    On Jun 29, 7:08 pm, Arne Vajhøj <> wrote:
    > Tomer wrote:
    > > If the current time is 18:50 and ischeduleRepeating dailyTimerTask
    > > to 18:52 then everything is file its being run at 18:52 daily however
    > > if ischeduleit to 18:48 then its being run immediately (and i dont
    > > want that) as i run the application :(
    > > However i wanted it to be runeverydayat 18:48! how can i achieve
    > > that?
    > > Timertimer= newTimer();
    > > Calendar date = Calendar.getInstance();
    > > //Timerstarts yesterday (so that surely will run today when time
    > > comes.
    > > // date.setTimeInMillis(new Date().getTime() - 1000 * 60 * 60 * 24);
    > > DateFormat sdf = new SimpleDateFormat("HH:mm");
    > > Date dateToBackup = null;
    > > try {
    > > dateToBackup = sdf.parse("18:32");
    > > } catch (ParseException e) {
    > > e.printStackTrace();
    > > }
    > > Calendar calendar = Calendar.getInstance();
    > > calendar.setTime(dateToBackup);
    > > date.set(Calendar.HOUR, calendar.get(Calendar.HOUR));
    > > date.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
    > > date.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
    > > date.set(Calendar.MILLISECOND, 0);

    >
    > Why not just roll onedayforward ?
    >
    >
    >
    > > //Scheduleto runeveryday.
    > > timer.schedule(
    > > new TestTimerTask(),
    > > date.getTime(),
    > > 1000 * 60 * 60 * 24
    > > );
    > > Thread.sleep(120000);
    > > }

    >
    > > }

    >
    > Arne


    I didnt want to roll one day forward because i didnt want to have
    special cases in my code like checking if the current time is later
    than scheduled time and in that case roll one day forward, but now
    that i see i have no choice, then i would need to check for that
    special case and roll one day forward.

    Thanks,

    Tomer
    Tomer, Jun 30, 2008
    #6
  7. Tomer

    Arne Vajhøj Guest

    Tomer wrote:
    > I didnt want to roll one day forward because i didnt want to have
    > special cases in my code like checking if the current time is later
    > than scheduled time and in that case roll one day forward, but now
    > that i see i have no choice, then i would need to check for that
    > special case and roll one day forward.


    I think your problem includes a condition, so it I don't think
    you can avoid a condition in your code.

    Arne
    Arne Vajhøj, Jul 4, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    2
    Views:
    303
    Scott M.
    Nov 1, 2003
  2. Replies:
    2
    Views:
    862
    Hal Rosser
    Jun 1, 2007
  3. ssylee
    Replies:
    4
    Views:
    499
    Thad Smith
    Jan 1, 2008
  4. Andy
    Replies:
    1
    Views:
    678
  5. Chris Gunnels
    Replies:
    4
    Views:
    151
    Robert Klemme
    Sep 16, 2010
Loading...

Share This Page