Unix时代到Java Date对象 [英] Unix epoch time to Java Date object
问题描述
我有一个包含 UNIX时代时间的字符串,我需要将其转换为Java Date对象。
I have a string containing the UNIX Epoch time, and I need to convert it to a Java Date object.
String date = "1081157732";
DateFormat df = new SimpleDateFormat(""); // This line
try {
Date expiry = df.parse(date);
} catch (ParseException ex) {
ex.getStackTrace();
}
标记行是我遇到麻烦的地方。我无法弄清楚SimpleDateFormat()应该是什么,或者即使我应该使用SimpleDateFormat()。
The marked line is where I'm having trouble. I can't work out what the argument to SimpleDateFormat() should be, or even if I should be using SimpleDateFormat().
推荐答案
p>如何:
Date expiry = new Date(Long.parseLong(date));
编辑:根据 rde6173 的答案,并仔细观察问题中指定的输入,1081157732似乎是一个基于秒的时期值,所以你想要将来自parseLong()的long乘以1000转换为毫秒,这是Java的Date构造函数使用的,所以:
as per rde6173's answer and taking a closer look at the input specified in the question , "1081157732" appears to be a seconds-based epoch value so you'd want to multiply the long from parseLong() by 1000 to convert to milliseconds, which is what Java's Date constructor uses, so:
Date expiry = new Date(Long.parseLong(date) * 1000);
这篇关于Unix时代到Java Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!