我在 Date 对象中有一些日期/时间;如何将其格式化为字符串,以便在 中表示适当的日期/时间UTC 时区?
最佳答案
我在中欧TZ。这是我的 joda-time 代码:
scala> val myTz = DateTimeZone.getDefault()
myTz: org.joda.time.DateTimeZone = Europe/Warsaw
scala> val now = new Date()
now: java.util.Date = Tue Sep 23 12:06:05 CEST 2014
scala> val utcString = new DateTime(now).withZone(DateTimeZone.UTC).toString()
utcString: String = 2014-09-23T10:06:05.302Z
java 8:
欢迎使用 Scala 版本 2.11.2(Java HotSpot(TM) 64 位服务器 VM,Java 1.8.0_20)。
scala> import java.time._
import java.time._
scala> val utcZoneId = ZoneId.of("UTC")
utcZoneId: java.time.ZoneId = UTC
scala> val zonedDateTime = ZonedDateTime.now
zonedDateTime: java.time.ZonedDateTime = 2014-09-24T09:45:31.165+02:00[Europe/Warsaw]
scala> val utcDateTime = zonedDateTime.withZoneSameInstant(utcZoneId)
utcDateTime: java.time.ZonedDateTime = 2014-09-24T07:45:31.165Z[UTC]
关于scala - 如何将时间格式化为 UTC 时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25991892/