java - Scala - 如何将 LocalDateTime 转换为无 GMT 后缀格式的 ZonedDateTime?
问题描述
我想在 GMT 中获取 LocalDateTime,所以用 ZonedDateTime 包装它。
但是gmtZoneTime
以以下格式返回:2019-10-29T00:00Z[GMT]
虽然我需要它:2019-10-29T00:00:00.000+0000
我应该如何正确转换localDateTime
为 GMT ZonedDateTime?
val currentDate:LocalDate = java.time.LocalDate.now
val localDateTime: LocalDateTime = currentDate.atStartOfDay
val gmtZoneTime: ZonedDateTime = localDateTime.atZone(ZoneId.systemDefault()).withZoneSameInstant(ZoneId.of("GMT"))
解决方案
您需要将ZonedDateTime
.
第一种方法是使用预定义的格式化程序,例如:java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME
,但是因为GMT
它显示“Z”而不是“+0000”(默认行为,其他偏移量显示为“+0100”等)
所以第二个是创建自己的格式化程序,例如:
java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")
然后用它来格式化ZonedDateTime
,这样gmtZoneTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"))
你就会得到如下结果:
2019-10-28T23:00:00+0000
推荐阅读
- mysql - 我不明白两个查询之间 EXPLAIN 输出的区别。唯一的区别是 MYSQL 中的 LEFT JOIN 与 INNER JOIN
- drupal - Drupal 升级后 Drupal 模块丢失
- eclipse - 在 STS 中安装 Mac os Lombok
- javascript - 从地图文件中获取上下文
- java - 将本地时间转换为 EST 时区
- image-processing - 如何检查图像是否为损坏的图像?
- unity3d - 为什么 Input.MouseButtonDown 和 OnPointerClicked 不在同一帧中触发?
- javascript - 如何使动画完全可见html
- jekyll-paginator - Paginator 不在 github 页面上工作,但它在 localhost 上工作
- docker - Docker 无法解析 Teradata 数据库源 url