Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Silly Date Problem

Chris Parr
Greenhorn
Posts: 2
Ok, a little shameful of me asking this as it should be simple (it's late; perhaps I need to go to bed).

I have a long (1327759217) representing a time after epoch; roughly 2pm this afternoon UK time. I want to take that number and print out the date as a string. Here is my code:

My problem is that this prints a date sometime in 1970, 16/01/1970 09:49 AM to be precise.

This shouldn't be. Going to this site here (click) and inserting 1327759217 gives the right answer, but not the above code even though the javadoc suggests I'm doing things right.

Anyone care to point out my schoolboy error? Please?

Cheers

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65278
95
I don't care what that site says, there are way too few digits in that value to represent a date in 2012.

In fact, I just computed 1327784196526 for a few minutes ago.

That's 13 digits, not 10.

Stephan van Hulst
Bartender
Posts: 6486
83
1300000000 milliseconds is roughly equivalent to 15 days. I'm pretty sure the site is wrong.

Stephan van Hulst
Bartender
Posts: 6486
83
• 1
Actually, if you read the text below the converter, it says that it uses seconds, not milliseconds. That's where the discrepancy arises.

Joanne Neal
Rancher
Posts: 3742
16
Stephan van Hulst wrote:1300000000 milliseconds is roughly equivalent to 15 days. I'm pretty sure the site is wrong.

getTime returns the number of milliseconds elapsed, in your computer's timezone, since 1/1/1970 GMT. Because epoch is measured in seconds, you then want to divide the return value by 1000 to change milliseconds into seconds.

Chris Parr
Greenhorn
Posts: 2
Thanks all.

Of course you're right. Like I said, I should have called it a day.

Live and learn etc....

Jeff Verdegan
Bartender
Posts: 6109
6
Chris Parr wrote:Like I said, I should have called it a day.

No, you should have just called it a second, instead of a millisecond.