File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Date/Time display problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date/Time display problem" Watch "Date/Time display problem" New topic

Date/Time display problem

Steven Woodford

Joined: Nov 11, 2002
Posts: 12
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,
Greg Charles

Joined: Oct 01, 2001
Posts: 2969

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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Unless I am mistaken, the output says 5 minutes, not 5 hours...

Java API Documentation
The Java Tutorial
Steven Woodford

Joined: Nov 11, 2002
Posts: 12
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

Joined: Nov 11, 2002
Posts: 12
Sorry, not explained fully again. The extra hour seems to be added on by the date formatter (the value of timeOut is correct)
I agree. Here's the link:
subject: Date/Time display problem
It's not a secret anymore!