• 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

Calendar class and getting the correct date

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
each time you want the current time, do this
long currentTime = new Date().getTime();
kawaii
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jax, that question has been answered.

System.currentTimeMillis();

If you're using that, but it's not working, post your code.
 
Jax Laakso
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic