如何在 Java 中将纪元时间转换为日期和时间? [英] How can I convert epoch time to date and time in Java?
本文介绍了如何在 Java 中将纪元时间转换为日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将纪元时间戳转换为日期和时间.我使用以下代码进行转换,但它转换为错误的日期、年份和时间.
I need to convert epoch timestamp to date and time. I have used the following code to convert but it converts to a wrong date, year and time.
String date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
.format(new java.util.Date (1319286637/1000));
预期输出是今天的某个时间,但我得到的结果是:
Expected output was today’s date at some time, but the result I got was:
01/01/1970 05:51:59
01/01/1970 05:51:59
推荐答案
Date(long)
构造函数需要毫秒.您应该乘以 1000,而不是除以您拥有的纪元时间.
The Date(long)
constructor takes milliseconds. You should be multiplying by 1000, not dividing the epoch time you have.
这篇关于如何在 Java 中将纪元时间转换为日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文