wood burning stoves 2.0*
The moose likes Java in General and the fly likes SimpleDateFormatter shows wrong hour? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "SimpleDateFormatter shows wrong hour?" Watch "SimpleDateFormatter shows wrong hour?" New topic
Author

SimpleDateFormatter shows wrong hour?

Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
Hi,
I want to get a string in the format of hhmm and uses SimpleDateFormatter for this purpose. My problem is that when the time is 1000 ms, it shows one hour ("01:00"). Have I missed something? The code that is below gives this result
>java TimeTest
01:00
01:00
01:59
02:00
what do I have to do to make it look like this instead:
00:00
00:00
00:59
01:00

Regards,
Andreas
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The problem is that SimpleDateFormatter is converting the Date objects to your local time zone (if I ran your code, I'd see different values printed. You clearly live in Europe, maybe Norway or Sweden, right? -- one hour ahead of Great Britain).
If you adjust the time zone, you can get the results you want. Add a line like this to main():

and you'll be all set.


[Jess in Action][AskingGoodQuestions]
Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
I have also tested to exchange the row
private static SimpleDateFormat hhmm_formatter = new SimpleDateFormat("HH:mm");
with this:
private static SimpleDateFormat hhmm_formatter = new SimpleDateFormat();
hhmm_formatter.applyPattern("HH:mm");
This is no difference.
Could it have something to do with that I'm in the +01 timezone (Sweden)? How can I avoid this localized time formatting?
// Andreas
Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
Thanks Ernest!
Didn't see your answer before my previous post. Your line solved my problem.
And yes, I live in beautiful Sweden
// Andreas
 
Consider Paul's rocket mass heater.
 
subject: SimpleDateFormatter shows wrong hour?