在 Java 中将 Long 转换为 Date 返回 1970 [英] Converting Long to Date in Java returns 1970

查看:54
本文介绍了在 Java 中将 Long 转换为 Date 返回 1970的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从网络服务下载的长值列表(例如:1220227200、1220832000、1221436800...).我必须将其转换为日期.不幸的是这种方式,例如:

I have list with long values (for example: 1220227200, 1220832000, 1221436800...) which I downloaded from web service. I must convert it to Dates. Unfortunately this way, for example:

Date d = new Date(1220227200);

返回 1 Jan 1970.有人知道正确转换它的另一种方法吗?

returns 1 Jan 1970. Anyone know another way to convert it correctly?

推荐答案

Date 构造函数(点击链接!)接受时间为 long,单位为 毫秒,不是秒.您需要将其乘以 1000,并确保将其提供为 long.

The Date constructor (click the link!) accepts the time as long in milliseconds, not seconds. You need to multiply it by 1000 and make sure that you supply it as long.

Date d = new Date(1220227200L * 1000);

这里显示

2008 年 8 月 31 日星期日 20:00:00 GMT-04:00

Sun Aug 31 20:00:00 GMT-04:00 2008

这篇关于在 Java 中将 Long 转换为 Date 返回 1970的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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