java - LocalDateTime 的长时间戳

标签 java java-8 timestamp java-time

我有一个长时间戳 1499070300(相当于 2017 年 7 月 3 日星期一 16:25:00 +0800),但是当我将其转换为 LocalDateTime 时,我得到 1970-01-18T16:24:30.300

这是我的代码

long test_timestamp = 1499070300;

LocalDateTime triggerTime =
                LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
                        .getDefault().toZoneId());

最佳答案

您需要以毫秒为单位传递时间戳:

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

System.out.println(triggerTime);

结果:

2017-07-03T10:25

或者使用 ofEpochSecond 代替:

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

System.out.println(triggerTime);

结果:

2017-07-03T10:25

关于java - LocalDateTime 的长时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44883432/

相关文章:

Javascript正则表达式从字符串中提取年份

MySQL 时间戳问题

java - Spring data neo4j无法连接neo4j 2.2.1

java - 在 java 中我们可以使用 list.remove(int index) 来删除该索引中的项目,如果列表很大而我们只能使用 long 来存储索引怎么办?

使用 JDK 8 在 Mac OS X El Capitan beta 3 上的 Eclipse 进程 sleep

javaws.exe 与 java8 中的 java.exe 相比性能较差

java - 如何将 WatchService 更改为流 api

java - 启动时收到工作管理器

java - 删除所有文件和子目录,但在 Java 中将当前目录保持为空

sql - postgres 批量数据类型更改