I have a string containing the UNIX Epoch time, and I need to convert it to a Java Date object.
我有一个包含UNIX纪元时间的字符串,我需要将它转换为Java日期对象。
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()。
7 个解决方案
#1
91
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()到1000的长乘以1000,将其转换为毫秒,这是Java的日期构造函数所使用的,因此:
Date expiry = new Date(Long.parseLong(date) * 1000);
#2
34
Epoch is the number of seconds since Jan 1, 1970..
纪元是自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/
更多信息:http://www.epochconverter.com/
#3
13
java.time
Using the java.time
framework built into Java 8 and later.
使用java。在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 UTC time zone because Epoch is defined in UTC in Unix time used by Java.
在这种情况下,最好使用ZonedDateTime来标记UTC时区中的日期,因为在Java使用的Unix时间中,Epoch是用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" );
#4
5
long timestamp = Long.parseLong(date)
Date expiry = new Date(timestamp * 1000)
#5
0
Better yet, use JodaTime. Much easier to parse strings and into strings. Is thread safe as well. Worth the time it will take you to implement it.
更好的是,使用JodaTime。更容易解析字符串和字符串。线程安全吗?值得你花时间去实现它。
#6
0
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)
长milliSecondTimeStamp =毫秒。转换(secondsTimeStamp秒)
#7
-1
Hum.... if I am not mistaken, the UNIX Epoch time is actually the same thing as
哼....如果我没弄错的话,UNIX纪元时间实际上是一样的
System.currentTimeMillis()
So writing
所以写
try {
Date expiry = new Date(Long.parseLong(date));
}
catch(NumberFormatException e) {
// ...
}
should work (and be much faster that date parsing)
应该可以工作(并且要比日期解析快得多)
#1
91
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()到1000的长乘以1000,将其转换为毫秒,这是Java的日期构造函数所使用的,因此:
Date expiry = new Date(Long.parseLong(date) * 1000);
#2
34
Epoch is the number of seconds since Jan 1, 1970..
纪元是自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/
更多信息:http://www.epochconverter.com/
#3
13
java.time
Using the java.time
framework built into Java 8 and later.
使用java。在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 UTC time zone because Epoch is defined in UTC in Unix time used by Java.
在这种情况下,最好使用ZonedDateTime来标记UTC时区中的日期,因为在Java使用的Unix时间中,Epoch是用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" );
#4
5
long timestamp = Long.parseLong(date)
Date expiry = new Date(timestamp * 1000)
#5
0
Better yet, use JodaTime. Much easier to parse strings and into strings. Is thread safe as well. Worth the time it will take you to implement it.
更好的是,使用JodaTime。更容易解析字符串和字符串。线程安全吗?值得你花时间去实现它。
#6
0
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)
长milliSecondTimeStamp =毫秒。转换(secondsTimeStamp秒)
#7
-1
Hum.... if I am not mistaken, the UNIX Epoch time is actually the same thing as
哼....如果我没弄错的话,UNIX纪元时间实际上是一样的
System.currentTimeMillis()
So writing
所以写
try {
Date expiry = new Date(Long.parseLong(date));
}
catch(NumberFormatException e) {
// ...
}
should work (and be much faster that date parsing)
应该可以工作(并且要比日期解析快得多)