【发布时间】:2011-05-01 16:35:09
【问题描述】:
我想将 Joda Time UTC DateTime 对象转换为本地时间。
这是一种费力的方法,似乎可行。但一定有更好的办法。
这是代码(在 Scala 中),没有周围的声明:
val dtUTC = new DateTime("2010-10-28T04:00")
println("dtUTC = " + dtUTC)
val dtLocal = timestampLocal(dtUTC)
println("local = " + dtLocal)
def timestampLocal(dtUTC: DateTime): String = {
// This is a laborious way to convert from UTC to local. There must be a better way.
val instantUTC = dtUTC.getMillis
val localDateTimeZone = DateTimeZone.getDefault
val instantLocal = localDateTimeZone.convertUTCToLocal(instantUTC)
val dtLocal = new DateTime(instantLocal)
dtLocal.toString
}
这是输出:
dtUTC = 2010-10-28T04:00:00.000+11:00 本地 = 2010-10-28T15:00:00.000+11:00
【问题讨论】:
标签: scala time local utc jodatime