aspose file tools*
The moose likes Java in General and the fly likes Java Timer... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Timer..." Watch "Java Timer..." New topic
Author

Java Timer...

Montano Mazvik
Greenhorn

Joined: Feb 07, 2007
Posts: 23
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
Marshal

Joined: Mar 22, 2005
Posts: 41529
    
  53
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?


Ping & DNS - my free Android networking tools app
Montano Mazvik
Greenhorn

Joined: Feb 07, 2007
Posts: 23
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
Marshal

Joined: Mar 22, 2005
Posts: 41529
    
  53
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

Joined: Aug 16, 2005
Posts: 14111
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Montano Mazvik
Greenhorn

Joined: Feb 07, 2007
Posts: 23
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
Sheriff

Joined: Sep 28, 2004
Posts: 18720
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Timer...