scala - 如何将时间格式化为 UTC 时区?

标签 scala

我在 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/

相关文章:

scala - 玩!斯卡拉 : How to disable actor modules loaded at the start of the application when testing?

scala - 如何在scala中将数字拆分为数字

java - Gradle scala 类已通过组合 Java 编译定义

scala - 从 Future[ List[ A ] ] 获取 head Future 的首选方式

java - 在Java代码中,如何正确处理Future[Try[Option[Map[String, Any]]]]格式的scala future

java - 如何在 Akka Actor 之间传递远程引用?

memory - Actor 邮箱溢出。斯卡拉

scala - 如何将值元组与函数元组结合起来?

java - 如何打包成.jar java main + jruby编译类+ ruby​​脚本?

scala - 从 Scala 控制台获取函数类型