• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

SimpleDateFormatter shows wrong hour?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest!
Didn't see your answer before my previous post. Your line solved my problem.
And yes, I live in beautiful Sweden
// Andreas
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic