Unix 纪元时间到 Java 日期对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/535004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unix epoch time to Java Date object
提问by Xenph Yan
I have a string containing the UNIX Epoch time, and I need to convert it to a Java Date object.
我有一个包含UNIX Epoch time的字符串,我需要将它转换为 Java Date 对象。
String date = "1081157732";
DateFormat df = new SimpleDateFormat(""); // This line
try {
Date expiry = df.parse(date);
} catch (ParseException ex) {
ex.getStackTrace();
}
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().
标记的线是我遇到麻烦的地方。我无法弄清楚 SimpleDateFormat() 的参数应该是什么,或者即使我应该使用 SimpleDateFormat()。
采纳答案by Marc Novakowski
How about just:
怎么样:
Date expiry = new Date(Long.parseLong(date));
EDIT: 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:
编辑:根据rde6173的回答并仔细查看问题中指定的输入,“1081157732”似乎是基于秒的纪元值,因此您希望将来自 parseLong() 的 long 乘以 1000 进行转换到毫秒,这是 Java 的 Date 构造函数使用的,所以:
Date expiry = new Date(Long.parseLong(date) * 1000);
回答by tcurdt
long timestamp = Long.parseLong(date)
Date expiry = new Date(timestamp * 1000)
回答by Varkhan
Hum.... if I am not mistaken, the UNIX Epoch time is actually the same thing as
嗯.... 如果我没记错的话,UNIX Epoch 时间实际上和
System.currentTimeMillis()
So writing
所以写作
try {
Date expiry = new Date(Long.parseLong(date));
}
catch(NumberFormatException e) {
// ...
}
should work (and be much faster that date parsing)
应该可以工作(并且日期解析要快得多)
回答by Ryan Emerle
Epoch is the number of secondssince Jan 1, 1970..
Epoch 是自 1970 年 1 月 1 日以来的秒数。
So:
所以:
String epochString = "1081157732";
long epoch = Long.parseLong( epochString );
Date expiry = new Date( epoch * 1000 );
For more information: http://www.epochconverter.com/
回答by WolfmanDragon
回答by Dinesh Jayabalan
To convert seconds time stamp to millisecond time stamp. You could use the TimeUnit API and neat like this.
将秒时间戳转换为毫秒时间戳。您可以像这样使用 TimeUnit API 和整洁。
long milliSecondTimeStamp = MILLISECONDS.convert(secondsTimeStamp, SECONDS)
long milliSecondTimeStamp = MILLISECONDS.convert(secondsTimeStamp, SECONDS)
回答by Przemek
java.time
时间
Using the java.time
framework built into Java 8 and later.
使用java.time
内置于 Java 8 及更高版本中的框架。
import java.time.LocalDateTime;
import java.time.Instant;
import java.time.ZoneId;
long epoch = Long.parseLong("1081157732");
Instant instant = Instant.ofEpochSecond(epoch);
ZonedDateTime.ofInstant(instant, ZoneOffset.UTC); # ZonedDateTime = 2004-04-05T09:35:32Z[UTC]
In this case you should better use ZonedDateTime
to mark it as date in UTCtime zone because Epoch is defined in UTC in Unix timeused by Java.
在这种情况下,您最好使用UTC时区ZonedDateTime
将其标记为日期,因为 Epoch 是在Java 使用的Unix 时间的UTC 中定义的。
ZoneOffset
contains a handy constant for the UTC time zone, as seen in last line above. Its superclass, ZoneId
can be used to adjust into other time zones.
ZoneOffset
包含一个方便的 UTC 时区常量,如上面最后一行所示。它的超类,ZoneId
可用于调整到其他时区。
ZoneId zoneId = ZoneId.of( "America/Montreal" );