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 SimpleDateFormatter shows wrong hour? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "SimpleDateFormatter shows wrong hour?" Watch "SimpleDateFormatter shows wrong hour?" New topic

SimpleDateFormatter shows wrong hour?

Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
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
what do I have to do to make it look like this instead:

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24166

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();
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
I agree. Here's the link:
subject: SimpleDateFormatter shows wrong hour?
Similar Threads
Stop Watch
Adding 'time' to a timestamp
Doubt in Data
How to compare (Hour) with actual new Date() ?