简单日期格式提供了错误的时间信息 [英] Simple Date format gives wrong info from epoch timestamp
问题描述
我发现这给出了错误的日期。但是我怎么解决不了。请有人帮我。
我是android开发的新手。
预先感谢;
I found that this gives a wrong date. but how i can not solve it. please someone help me. I am new in android Development. Thanks in advance;
字符串时间戳= 1538970640;
String timestamp = "1538970640";
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM 'at' hh:mm a z" );
String dateString = formatter.format(new Date(Long.parseLong(timestamp)));
返回值:
19 Jan at 01:29 AM GMT+06:oo
是:
8 Oct at 9:50 AM GMT+06:00
推荐答案
java.util.Date
构造函数接受毫秒自纪元以来,不是秒:
分配一个Date对象并将其初始化以表示指定的日期自标准基准时间(即纪元)以来的毫秒数,即1970年1月1日,格林尼治标准时间。
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
以下使用ms的代码正在运行:
The following code which uses ms is working:
String timestamp = "1538970640000"; // use ms NOT s
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM 'at' hh:mm a z" );
String dateString = formatter.format(new Date(Long.parseLong(timestamp)));
08 Oct at 05:50 AM CEST
您遇到的部分问题面临的问题是您的日期格式省略了年份部分,而实际上是1970年。
Part of the problem you were facing is that your date format omitted the year component, which was actually coming up as 1970.
这篇关于简单日期格式提供了错误的时间信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!