java - 将毫秒转换为 ZonedDateTime 不起作用
问题描述
我有很长的这个毫秒:
1570046362841
使用版本 1转换时:
var myDate = ZonedDateTime.ofInstant(Instant.ofEpochSecond(1570046362841), ZoneId.of("America/New_York"));
我会得到这个结果(这是错误的!):
+51722-10-16T03:58:54-04:00[America/New_York]
但是,使用版本 2进行转换时:
final String dateFormat = "yyyy-MM-dd HH:mm:ss SSS";
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
var dateObj = new Date(1570046362841);
var myDate = formatter.format(dateObj);
我得到正确的结果:
2019-10-02 15:59:59 934
为什么版本 1 错误?版本 1 有什么问题?
解决方案
而不是Instant.ofEpochSecond()
你需要使用Instant.ofEpochMilli()
,因为你有millis。
推荐阅读
- css - 是否有一个 Bootstrap 类可以像我的示例一样在悬停时在我的图像下创建颜色?
- html - HTML5 如何在文章标签中添加背景颜色
- entity-framework - 带有连接的 EF 查询未运行
- python - 在 keras 训练期间使用 fit_generator 的问题
- free-jqgrid - 当字段在表单编辑时更改时,想要在 ColModel 的 editoptions 中运行规则
- javascript - 此错误消息(业力打字稿)是否有解决方案?
- sql-server - 尝试在 Azure SQL Server 上创建新的 Azure AD 服务器主体(登录名)时,为什么我会在“提供者”附近收到错误的语法?
- php - 为什么 pdo 什么也没发生?
- python-3.x - 从打印中删除内括号
- flutter - 如何根据 Flutter 中的大小决定要显示的小部件