long timestamp to LocalDateTime

后端 未结 5 1167
情深已故
情深已故 2020-12-05 06:13

I have a long timestamp 1499070300 (equivalent to Mon, 03 Jul 2017 16:25:00 +0800) but when I convert it to LocalDateTime I get 1970-01-18T16:24:30.300

Here\'s my co

5条回答
  •  心在旅途
    2020-12-05 06:43

    You need to pass timestamp in milliseconds:

    long test_timestamp = 1499070300000L;
    LocalDateTime triggerTime =
            LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), 
                                    TimeZone.getDefault().toZoneId());  
    
    System.out.println(triggerTime);
    

    Result:

    2017-07-03T10:25
    

    Or use ofEpochSecond instead:

    long test_timestamp = 1499070300L;
    LocalDateTime triggerTime =
           LocalDateTime.ofInstant(Instant.ofEpochSecond(test_timestamp),
                                   TimeZone.getDefault().toZoneId());   
    
    System.out.println(triggerTime);
    

    Result:

    2017-07-03T10:25
    

提交回复
热议问题