Scala Joda时间:将UTC转换为本地时间
我想将Joda Time UTC DateTime对象转换为本地时间 这里有一个很费劲的方法,看起来很有效。但一定有更好的办法 下面是代码(在Scala中),没有周围的声明:Scala Joda时间:将UTC转换为本地时间,scala,time,local,utc,jodatime,Scala,Time,Local,Utc,Jodatime,我想将Joda Time UTC DateTime对象转换为本地时间 这里有一个很费劲的方法,看起来很有效。但一定有更好的办法 下面是代码(在Scala中),没有周围的声明: val dtUTC = new DateTime("2010-10-28T04:00") println("dtUTC = " + dtUTC) val dtLocal = timestampLocal(dtUTC) println("local = " + dtLocal) def t
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
local=2010-10-28815:00:00.000+11:00以下是我在当前项目中使用的内容
val marketCentreTime = timeInAnotherTimezone.withZone(DateTimeZone.forID("Australia/Melbourne"))
这有用吗
编辑:
这里有一些东西在当前TZ中需要一段时间,并转换为布里斯班时间。你可以使用同样的原理
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_21).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import org.joda.time._
import org.joda.time._
scala> def timestampBrisbane(date: DateTime): String = {
| date.withZone(DateTimeZone.forID("Australia/Brisbane")).toString
| }
timestampBrisbane: (date: org.joda.time.DateTime)String
scala> val date = new DateTime
date: org.joda.time.DateTime = 2010-10-28T16:22:03.481+11:00
scala> val dateBrisbane = timestampBrisbane(date)
dateBrisbane: String = 2010-10-28T15:22:03.481+10:00
完美的我把它改在更南边的墨尔本。