Java:来自 unix 时间戳的日期 [英] Java: Date from unix timestamp
问题描述
我需要将 unix 时间戳转换为日期对象.
我试过这个:
java.util.Date time = new java.util.Date(timeStamp);
时间戳值为:1280512800
日期应该是2010/07/30 - 22:30:00"(因为我是通过 PHP 得到的)但我得到的是 Thu Jan 15 23:11:56 IRST 1970
.
应该怎么做?
对于 1280512800
,乘以 1000,因为 java 需要毫秒:
java.util.Date time=new java.util.Date((long)timeStamp*1000);
如果你已经有毫秒,那么只需 new java.util.Date((long)timeStamp);
来自文档:><块引用>
分配一个 Date 对象并初始化它以表示指定的毫秒数标准基准时间称为纪元",即 1970 年 1 月 1 日,格林威治标准时间 00:00:00.
I need to convert a unix timestamp to a date object.
I tried this:
java.util.Date time = new java.util.Date(timeStamp);
Timestamp value is: 1280512800
The Date should be "2010/07/30 - 22:30:00" (as I get it by PHP) but instead I get Thu Jan 15 23:11:56 IRST 1970
.
How should it be done?
For 1280512800
, multiply by 1000, since java is expecting milliseconds:
java.util.Date time=new java.util.Date((long)timeStamp*1000);
If you already had milliseconds, then just new java.util.Date((long)timeStamp);
From the documentation:
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.
这篇关于Java:来自 unix 时间戳的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!