Nouvel Travay Nouvel Travay - 2 months ago 12
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
1460469600
.

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

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.

http://currentmillis.com/

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.

Comments