• 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

Date/Time display problem

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a problem displaying small time periods using the Date class. I'm quite new to Java (and this forum) so i'm hoping there is an easy way around this that i just haven't learned yet.
Here goes:
This code has been tested on Windows NT 2K and XP with the same results.
I am trying to display a time period stored in milliseconds as a long using code similar to this.
long timePeriod = 300000L;
System.out.println(new Date(timePeriod));
I would expect this to display...
Thu Jan 01 00:05:00 GMT 1970 (i am ignoring the date, its just the time i'm looking for)
... but instead it displays
Thu Jan 01 01:05:00 GMT 1970
I have found that if i un-tick the "Automatically adjust clock for daylight saving changes" option in time zone settings, the problem goes away - however i need to have this option on.
I would be grateful of any light you could shed on this as it has got me feeling quite
Thanks in advance,
Ste
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dates print by default in your local time zone. You show GMT prints out, but I bet it's really whatever time zone you're in, right? Anyway try this:

BTW, 5 hours in miliseconds in 18,000,000, not 300,000. It's a good idea to show things like that explicitly: 5L * 60 * 60 * 1000. It makes it clearer what you're doing, and you don't make computation errors. The compiler will do the math, so you don't even hurt runtime performance.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless I am mistaken, the output says 5 minutes, not 5 hours...
 
Steven Woodford
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your suggestions, but I don't think I have explained my problem very well.
I am using a timer object to schedule events. The timer has a user-defined interval anywhere between 1 minute and a couple of hours. I want to display the time until the next timer event triggers.
The way I am calculating the time out, which seems to be correct, is as follows (all values are in long):
timeOut = timerInterval - (currentTime - timeOfLastEvent)
The value of timeOut is what i am putting into a date formatter to display the time out, but when I have the "adjust clock for daylight saving changes" option ticked I get an extra hour added onto the time out I have calculated.
Thanks again.
 
Steven Woodford
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, not explained fully again. The extra hour seems to be added on by the date formatter (the value of timeOut is correct)
 
reply
    Bookmark Topic Watch Topic
  • New Topic