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 ]