Unix timestamp keeps returning Jan 17 1970 in DateTime

不羁岁月 提交于 2019-12-24 16:33:59

问题


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

回答1:


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.




回答2:


Query to get timestamp in milliseconds :

select  UNIX_TIMESTAMP(yourtimestamp) *1000  from tablename.

this gives time stamp in milliseconds in mysql



来源:https://stackoverflow.com/questions/36556875/unix-timestamp-keeps-returning-jan-17-1970-in-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!