0

I am trying to convert the timestamp to date in java but I got an old date

Result Tue Jan 20 06:13:12 IST 1970

Expected: enter image description here

Here is my code

   public static void main(String args[]){    
            Date date=new Date(1644192000);  
            System.out.println(date);                     
    } 
yali
  • 847
  • 2
  • 11
  • 25

2 Answers2

6

The Date class is mostly obsolete except for backwards compatibility. Instead, use the modern java.time classes:

Instant timestamp = Instant.ofEpochSecond(1644192000);
// note the second/millis difference Rob Spoor mentioned
chrylis -cautiouslyoptimistic-
  • 72,004
  • 20
  • 117
  • 147
5

In Java, timestamps are in milliseconds. On Unix, they are in seconds. Try adding 3 zeroes to your Java timestamp (and let it end with L to indicate it's a long, not an int) and you should get the same result.

Rob Spoor
  • 2,775
  • 1
  • 15
  • 16