Converting Long to Date in Java returns 1970

mmmiki picture mmmiki · Sep 20, 2011 · Viewed 334.7k times · Source

I have list with long values (for example: 1220227200, 1220832000, 1221436800...) which I downloaded from web service. I must convert it to Dates. Unfortunately this way, for example:

Date d = new Date(1220227200);

returns 1 Jan 1970. Anyone know another way to convert it correctly?

Answer

BalusC picture BalusC · Sep 20, 2011

The Date constructor (click the link!) accepts the time as long in milliseconds, not seconds. You need to multiply it by 1000 and make sure that you supply it as long.

Date d = new Date(1220227200L * 1000);

This shows here

Sun Aug 31 20:00:00 GMT-04:00 2008