java8時間api:LocalDateTime

通過給定的秒級時間戳,來算出n天后的時間戳,或者是字符串
java8之前的時間api存在閏秒問題,所以如果要獲取到準確的時間戳,最好用java8api,不要用當前時間戳加上每天的秒數來算.

private String getDelayDaysString(Long stratTime, Integer n) {
    LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(stratTime), getChinaZone());

    LocalDateTime time = localDateTime.plusDays(n);
    return time.toString();
}

private static ZoneOffset getChinaZone() {
        return ZoneOffset.of("+8");
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章