Nouvel Travay Nouvel Travay - 1 year ago 82
Java Question

Unix timestamp keeps returning Jan 17 1970 in DateTime

I am using the following method to return a formatted date as say

07:00AM, Apr 12 2016
. But I keep getting
01:41PM, Sat, Jan 17 1970
. Say for example my timestamp is

Here is my method.

public static String formattedDate(long timestamp) {
DateTime date = new DateTime(timestamp);
String formatted= date.toString("hh:mma, EEE, MMM dd yyyy");
return formatted;

Answer Source

Your timeStamp is wrong. It doesnt represent the correct time in millis. YOur timeStamp refers to 01:41PM, Sat, Jan 17 1970.

You can check what time date the timeinmillis (TimeStamp) refers to from this site.

To get the correct time from unix time stamp just change your DateTime date = new DateTime(timestamp); into

    DateTime date = new DateTime(timestamp*1000);

Because unix time gives timpestamp in seconds and we need millis here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download