Java Timer Task

archinpalli
archinpalli used Ask the Experts™
on
Hi,

My requirement is to schedule the timer at 11 AM and repeat it after every minute.

Here is my code.

         Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
         cal.set(Calendar.DAY_OF_WEEK, cal.get(Calendar.DAY_OF_WEEK));
         cal.set(Calendar.HOUR_OF_DAY, 11);
         cal.set(Calendar.MINUTE, 0);
         cal.set(Calendar.SECOND, 0);
         cal.set(Calendar.MILLISECOND, 0);
         
         Date stTime = cal.getTime();
         
         if (!stTime.before(new Date()))
             new Timer(true).scheduleAtFixedRate(new ImportTimerUtil(), stTime, 60000);

The timer is scheduled when I click a button.

The problem is the timer is started immediately after clicking a button(clicked the button at 10:30 AM). It is starting after evey 1 minute delay. It is not supposed to start immediately. It is supposed to start at 11:00 AM and after every minute from then.

Thanks...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Why are you setting that time zone on the Calendar?
Top Expert 2016

Commented:
... and what does the following print?
Top Expert 2016

Commented:
:
System.out.println(System.getProperty("user.timezone"));

Open in new window

why not set it in scheduled tasks @ 11am every morning with the current code you have ?

Author

Commented:
System.out.println(System.getProperty("user.timezone"));
 
The above statement is printing 'America/New_York'

Author

Commented:
Hi plusone3055,

Sorry. I did not get your solution.

thanks...
Top Expert 2016

Commented:
If you specifically are interested in not starting before 11 am NY time, you should use


Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));

Open in new window

Top Expert 2016

Commented:
What OS are you using btw?

Author

Commented:
I am using Windows XP.

What happens if I use this

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));

just wanted to know.

Thanks....

Author

Commented:
Why the timer is not starting at 11:00 AM? Why is it starting immediately when I click the button?
Top Expert 2016

Commented:
Well, the 3-letter zone codes are discouraged by Sun in favour of city names.

>>My requirement is to schedule the timer at 11 AM and repeat it after every minute.

If you're doing that on a daily basis using Windows, you'll be well advised to have it running as a Windows, service, for which you can use the Tanukisoft wrapper, and/or  use a scheduling framework such as Quartz

Author

Commented:
I am using the timer to start the import process that will read data from a file and import data into database at the scheduled time.

I am testing on my local machine which is Windows XP. I will deploy it on the server.

Will there be any problem if I use timer and schedule? Your reply is greatly appreciated.

Thanks...
Top Expert 2016

Commented:
>>Why the timer is not starting at 11:00 AM?

Study the attached carefully. The pane on the left shows EST and the right shows America/New_York
tzs.png
Top Expert 2016

Commented:
Sorry, there's important info missing from the right pane. I've folded out unimportant stuff here:
tzs2.png

Author

Commented:
Hi CEHJ,

The timer is starting at the mentioned time. I do  not know why it started immediately earlier. Now it is working. But I got to know the below information which is useful to me.

Well, the 3-letter zone codes are discouraged by Sun in favour of city names.

Will there be any problem if I use timer and schedule instead of Quartz for scheduling?

Can I use
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(System.getProperty("user.timezone")));

instead of EST?




Top Expert 2010

Commented:
My recommendation would be to use CronTrigger from the Quartz (org.quartz) library and use Cron syntax to define firing times

Author

Commented:
My only concern regarding this is I already spent time on this and implemented using timer. Just wanted to know if I will face any problem in future with timer.
Top Expert 2010

Commented:
Quartz/CronTrigger is well established Timer library used in many products that have proven successful.
Top Expert 2016

Commented:
>>Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(System.getProperty("user.timezone")));

is redundant.
Top Expert 2016

Commented:
The important thing, whatever you use, is to ensure that the program is run as a service, or it can be easily interrupted
Mick BarryJava Developer
Top Expert 2010

Commented:
> Well, the 3-letter zone codes are discouraged by Sun in favour of city names.

they still work fine

> Will there be any problem if I use timer and schedule instead of Quartz for scheduling?

quartz wouldn't make any difference
Top Expert 2016

Commented:
>>they still work fine

They don't work fine. You obviously either haven't read or haven't understood the previous posts
Mick BarryJava Developer
Top Expert 2010

Commented:
we work on many legacy application that use them without problem.
Top Expert 2016
Commented:
Fortunately, archinpalli the comments above should remove any confusion on this issue:

a. If you hadn't set a timezone of any kind, you would have been OK
b. If you do want to set one for your location, you must use America/New_York, not EST, since, despite what you might think, the latter is *not* your time zone, as it doesn't do daylight savings time, and you do
Top Expert 2016

Commented:
:-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial