my dog learned polymorphism*
The moose likes Java in General and the fly likes mysterious problems with java.util.Calendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "mysterious problems with java.util.Calendar" Watch "mysterious problems with java.util.Calendar" New topic
Author

mysterious problems with java.util.Calendar

Peter Franz
Greenhorn

Joined: Feb 07, 2002
Posts: 15
Hi I've really mysterious problems with the class java.util.Calendar.
I tried to write an Application that stores it's starttime in a Calendar, and after a while compare it to the current Time, and write the time that has passed to the console (h:m:s).
But it is really curious. I wrote the code with Visual Age for Java (jdk 1.2.2)
When I run this code from VisualAge, there is no problem, and the right elapsed Time is displayed on the console.
But when i run this code from console (with jdk1.3.1), there is something wrong with the hours passed.
The hours are ONE Hour fast!
Probably it has something to do with GMT-Zone, because I live (and my system works) in Switzerland (GMT+1:00). But I thougt that i've handled this problem with the TimeZones (see code)!
So I'm verry confinced if you can help me with this problem, because I've allready debuged this code for several hours and also friends from school/work couldn't help me!

Here is the Code:
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
public class ElapsedTime {
private final static Calendar startTime = Calendar.getInstance();
public ElapsedTime() {
super();
}
public static void main(String[] args) {
try {
Thread.sleep(3500);
} catch (Exception e) {
}
Calendar now = Calendar.getInstance();
// I know this is horrible code, but it seams that there is no other way to do this!!!
long milli = now.getTime().getTime() - startTime.getTime().getTime();
Date tempDate = new Date(milli);
Calendar elapsed = Calendar.getInstance();
elapsed.setTimeZone(TimeZone.getTimeZone("GMT"));

elapsed.setTime(tempDate); // This should be the elapsed time in GMT format!
elapsed.setTimeZone(TimeZone.getDefault());
System.out.println("Elapsed time:");
System.out.println(
String.valueOf(elapsed.get(Calendar.HOUR))
+ " hours\n"
+ String.valueOf(elapsed.get(Calendar.MINUTE))
+ " minutes\n"
+ String.valueOf(elapsed.get(Calendar.SECOND))
+ " seconds");
}
}
// End of code
Thank you very much for your help!
uhon
[ February 07, 2002: Message edited by: uhon ]
[ February 07, 2002: Message edited by: uhon ]
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Simply remove the line:
elapsed.setTimeZone(TimeZone.getDefault());
this increments/decrements the elapsed time according to your time zone.
Solution works in both JDK1.2.2 and JDK1.3
By the way, what are snow conditions like in Switzerland? I have seen reports that the French and Italian Alps have very little snow this year.
[ February 07, 2002: Message edited by: Graeme Brown ]
Peter Franz
Greenhorn

Joined: Feb 07, 2002
Posts: 15
Wow it works!
I never thought that it is so simple!
I thought that a TimeZone is something permanent.
I didn't know that it just increments the Calendar *smile*
Thank you verry much for your help!
There is just something misterious -> the curious results in visual age!
By the way. The snow conditions arn't very good, that's true, but yesterday evening we had some snowflakes
I'll try out the ski tracks next week.
And I think some holidays are good fo me after this exhausting problem
sincirely uhon
[ February 07, 2002: Message edited by: uhon ]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

uhon -- please review JavaRanch's naming policy, and change your registered name accordingly.
Thanks,


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Peter Franz
Greenhorn

Joined: Feb 07, 2002
Posts: 15
Done,
although I don't understand why. :roll:
But I think it's a good Forum, and because of this I'll obey the naming conventions!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mysterious problems with java.util.Calendar