Nouvel Travay
Nouvel Travay

Reputation: 6472

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

Upvotes: 4

Views: 7464

Answers (2)

Abhinav Mente
Abhinav Mente

Reputation: 11

Query to get timestamp in milliseconds :

select  UNIX_TIMESTAMP(yourtimestamp) *1000  from tablename.

this gives time stamp in milliseconds in mysql

Upvotes: 1

Ragesh Ramesh
Ragesh Ramesh

Reputation: 3520

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.

Upvotes: 14

Related Questions