Java:来自unix时间戳的日期 [英] Java: Date from unix timestamp

查看:668
本文介绍了Java:来自unix时间戳的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将unix时间戳转换为日期对象。

我试过这个:

I need to convert a unix timestamp to a date object.
I tried this:

java.util.Date time = new java.util.Date(timeStamp);

时间戳值为: 1280512800

日期应该是2010/07/30 - 22:30:00(因为我通过PHP得到)但是我得到了 1月15日星期四23:11:56 IRST 1970

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.

应如何做?

推荐答案

对于 1280512800 ,乘以1000,因为java需要毫秒:

For 1280512800, multiply by 1000, since java is expecting milliseconds:

java.util.Date time=new java.util.Date((long)timeStamp*1000);

如果你已经有毫秒,那么只需 new java.util.Date( (长)时间戳);

If you already had milliseconds, then just new java.util.Date((long)timeStamp);

来自文档


分配Date对象和
初始化它代表
指定的毫秒数,因为
标准基准时间称为
epoch,即1970年1月1日,
00:00:00 GMT。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆