• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Timer...

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to schedule a task for everyday at 6.30 AM, I am trying to use Java.Util.Timer and TimerTask to do so, however I am facing a problem in using following API

schedule(TimerTask task, Date time)
Schedules the specified task for execution at the specified time.

scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
Schedules the specified task for repeated fixed-rate execution, beginning
at the specified time.

My Problem is I am not able to set the Timing which is 6.30 Am.

Please provide your valuable solutions.

Thanks in advance.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd need to create a Date object that points to 6.30 AM tomorrow, and a period of 24 hours (in milliseconds).

How did you create the Date object that didn't work?
 
Montano Mazvik
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Ulf.

I shouldn't be hesitating to tell you that I am getting stuck in setting up this time using date object.

I create date object
Date dt = new Date();

This will return me current date & time then I need to set this timing for every day 6.40 am.but don't know how to do it.

I tried dt.setHours() which is depricated & also dt.setTime(in miliseonds);
but not getting the correct way to do it.

Can you please tell me How to create a Date object that points to 6.30 AM
tomorrow and 24hrs period. I would apriciate that.

Thanks again !!
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like this should do the trick. Don't be put off by the fact that most methods of the Date class are deprecated - they work fine. Just be aware that Date has no concept of timezones (which is just fine in this case).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed, the methods in class Date are deprecated, and that means you should not use them, even though they do work. To do this the "right" way, use a Calendar object to set the date and time, and then call getTime() on the Calendar object to get the Date object:
 
Montano Mazvik
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a Lot Jasper and ULF I think its working, now I will try to implement this in my application.

Thank you very much guys apriciate it.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf, Jesper,

Hate to point out a minor bug. But the original request was for 6:30am everyday (not starting the next day). Hence, you should set the time to 6:30am first, and then set the day to the next day, only if the time has past for the current day.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic