File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calendar class and getting the correct date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar class and getting the correct date" Watch "Calendar class and getting the correct date" New topic
Author

Calendar class and getting the correct date

Jax Laakso
Greenhorn

Joined: Dec 07, 2001
Posts: 23
I'm a bit confused and hoping someone can help me. I need my program to grab the current time from the OS and then compare that to the time I gave it. The problem I'm having is that it's only grabbing the time once. For example, if I start the program at 1410 then the time will ALWAYS be 1410 and not 1530 or whatever time it is. How do I get Java to continually get a NEW time when it's needed?
Thanks a bunches, I'd rather not have to rewrite this with a timer function, since I don't understand those.
Jax
PS--This worked fine on the Macintosh I orginally wrote it on.
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
hi,
each time you want the current time, do this
long currentTime = new Date().getTime();
kawaii
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'd use either
Date dateNow = new Date();
or
long timeNow = System.currentTimeMillis();
depending on what data type I wanted. Why create a Date if you don't need it?


"I'm not back." - Bill Harding, Twister
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

As to "why create a Date when you don't need it?"

A trick I saw in Sun's source code for a asynchronous service method was to instantiate a single Date statically, and then use the setTime() method each time you wanted the 'current' date.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Which works as long as no other parts of the code are retaining a reference to the same Date(), without realizing that it can change. I really think they should've made Date immutable like the other standard wrapper classes. Oh well.
Anyway - this is an example where they evidently did need a Date, but they made sure they only created it once. "Why create a Date if you don't need it" still holds.
Jax Laakso
Greenhorn

Joined: Dec 07, 2001
Posts: 23
Actually I'm trying to get the current time. My program needs a timer so that it can run everyday at 5:15am. But the Calendar class can't be instatiated becuase it's abstract and most of the Date class has be depreciated. So I'm wondering how to do this. Am I going to have to use a Timer? And if yes, could someone explain how those work? I tried the Java API, but that just confused me.
Jax
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Jax, that question has been answered.

System.currentTimeMillis();

If you're using that, but it's not working, post your code.
Jax Laakso
Greenhorn

Joined: Dec 07, 2001
Posts: 23
All right. I have System.currentTimeMillis() running but how do I get my program to understand all those milliseconds are 5:15am? Can I use a dateformat to format the milliseconds into something readable? Is there another way to do this?
I don't want it to run just 24 hours after it starts, because if the server should go down, I don't want to have to come in at 5:15am to start it up again. Which from my understanding is how the Timer class would work. It would simply go to sleep for a set amount of time and then wake up and run.
I want to be able to start it when I come in at 8am and trust that it will run at 5:15am.
Thank you for your help,
Jax
[ March 04, 2002: Message edited by: Jax Laakso ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Aack! Tab characters are evil! Look how far everything is strewn across the page. Once the window width is forced to a certain width, the same width must be used for the whole page. Which is why we must all now use a scroll bar here. Ugh. Space characters are your friends. Please re-edit your post to get rid of the tab characters - thanks.
If you want to schedule something at regular intervals, java.util.Timer is really the easiest way to go. It's really not difficult to use:

[ February 27, 2002: Message edited by: Jim Yingst ]
Jax Laakso
Greenhorn

Joined: Dec 07, 2001
Posts: 23
But it looks like that only works for a set period of time. So if I started the program up at 8am, it would always run at 8am and not at 5:15am. And if I set the timer up to run at 5:15am I would have to restart it from 8am. So if it would crash at 2pm, I couldn't restart it until 8 the next morning, right?
Jax
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calendar class and getting the correct date