Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Timer...

 
Montano Mazvik
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42967
73
  • Mark post as helpful
  • send pies
  • 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).
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15276
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic