javadateunix-timestampjava.util.date

Java Date strange behaviour


I have very strange behaviour of Java Date class:

    System.out.println(new Date().toGMTString());
    long l = 1332452310L;
    Date d = new Date(l);
    System.out.println(d.toGMTString());

Unix time 1 332 452 310 is very recent, so I expect the two outputs to be close to each other in time.

Gives me

22 Mar 2012 22:00:42 GMT
16 Jan 1970 10:07:32 GMT

Why this happens? Why am I getting a date in 1970 rather than 2012?


Solution

  • your long l is the time in seconds, you need to make it the time in milliseconds:

    long l = 1332452310L * 1000L;