Unix紀(jì)錄時(shí)間到Java Date對象我有一個(gè)包含UNIX Epoch時(shí)間的字符串,我需要將其轉(zhuǎn)換為Java Date對象。String date = "1081157732";DateFormat df = new SimpleDateFormat(""); // This linetry {
Date expiry = df.parse(date);
} catch (ParseException ex) {
ex.getStackTrace();}標(biāo)記的線是我遇到麻煩的地方。我無法弄清楚SimpleDateFormat()的參數(shù)應(yīng)該是什么,或者即使我應(yīng)該使用SimpleDateFormat()。
3 回答

繁星淼淼
TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
java.time
使用java.time
Java 8及更高版本中內(nèi)置的框架。
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]
在這種情況下,您最好將ZonedDateTime
其標(biāo)記為UTC時(shí)區(qū)中的日期,因?yàn)镋poch是在Java使用的Unix時(shí)間內(nèi)以UTC定義的。
ZoneOffset
包含UTC時(shí)區(qū)的便捷常量,如上面的最后一行所示。它的超類,ZoneId
可用于調(diào)整到其他時(shí)區(qū)。
ZoneId zoneId = ZoneId.of( "America/Montreal" );
添加回答
舉報(bào)
0/150
提交
取消