我需要将 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);
来自 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时间戳的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3371326/