Converting Unix Timestamp to Date in Java

The following code can be used to convert a Unix timestamp. There are two solutions you can use. Solution 1 involves doing something like this. Solution 2 is another option. The output is the number of milliseconds since the epoch (1 517 439 600 000), which represents a point in time regardless of time zone or offset. In Java 8, the Instant class from the new date and time API can be used to obtain a Unix timestamp.

Solution 1:

You’re multiplying a timestamp which is already given in milliseconds since the Unix epoch by 1000. You simply need:

Date date = new Date(timestamp);

Currently, the date is 46886 when considering both the date and time.

Solution 2:

Are You certain that it needs to be multiplied by 1000? I attempted to pass it without multiplying
Date date = new Date(timestamp);
and it displayed 6:07 PM.

Solution 3:

Remove multiply with 1000 in

Create a new Date object with the given timestamp, multiplied by 1000 to convert it to milliseconds.

than it works.

Frequently Asked Questions