File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

mysterious problems with java.util.Calendar

Peter Franz

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() {
public static void main(String[] args) {
try {
} 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.setTime(tempDate); // This should be the elapsed time in GMT format!
System.out.println("Elapsed time:");
+ " 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!
[ 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:
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

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

Joined: Oct 25, 2000
Posts: 7292

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

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

Joined: Feb 07, 2002
Posts: 15
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!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: mysterious problems with java.util.Calendar
It's not a secret anymore!