• 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

mysterious problems with java.util.Calendar

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
uhon -- please review JavaRanch's naming policy, and change your registered name accordingly.
Thanks,
 
Peter Franz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic