Java經典例項:紀元秒和本地日期時間互換

FrankYou發表於2016-11-28

Java版本:1.8開始

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * Created by Frank
 */
public class CurrentDatetime {
    public static void main(String[] args) {
        // 紀元秒和本地日期時間互換
        Instant epochSec = Instant.ofEpochSecond(1000000000L);
        ZoneId zId = ZoneId.systemDefault();
        ZonedDateTime then = ZonedDateTime.ofInstant(epochSec, zId);
        System.out.println("The epoch was a billion seconds old on " + then);
    }
}

執行輸出:

The epoch was a billion seconds old on 2001-09-09T09:46:40+08:00[Asia/Shanghai]

 

相關文章