Ad

Timestamp To Date Not Correct In Java

- 1 answer

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);                     
    } 
Ad

Answer

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
Ad
source: stackoverflow.com
Ad