• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Milliseconds to Date using Calendar [without using Epoch]

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.
I've already search trying to understand what's happening in the following snippet of code but I didn't find anything helpful yet.



I'm trying to output something like follows:
000:00:00:01,000

But, I'm getting:
031:09:00:01,000

I've been trying to use GMT and UTC Timezones when creating a calendar but it didn't solve this problem.
I think that there is something related to Epoch but I'm not getting it.

Notes: I know that an instance of Date would never have a date defined with day equals to 0 or month equals to 0.
But 1000 milliseconds is equal to 1 second and 1 second is far enough to complete 1 day. :P

Thanks in advance for some help.

 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not obvious to me but why are you doing this?
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd use HH, not hh. hh returns something between 01 and 12 inclusive. midnight in hh:mm:ss is 12:00:00 (AM, not PM).

About your issue. The date is probably 21:00:01 on December 31st 1969. That indicates there's indeed a timezone issue. If I run your code I get 001:01:00:01,000. Changing the time zone of the Calendar does not help. However, you can also change the time zone of your SimpleDateFormat. If I do that I get 001:12:00:01,000 (or 001:00:00:01,000 if I switch to HH).

On a side note, you can drop the Calendar completely. You create a Calendar, set its time, then retrieve that time again. You can create a Date directly:
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic