This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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():
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
Joined: Mar 05, 2003
Thanks Ernest! Didn't see your answer before my previous post. Your line solved my problem. And yes, I live in beautiful Sweden // Andreas