scala – Joda时间:将UTC转换为本地
发布时间:2020-12-16 18:54:13 所属栏目:安全 来源:网络整理
导读:我想将Joda Time UTC DateTime对象转换为本地时间. 这是一种艰苦的方法,它似乎有效.但必须有更好的方法. 这是没有周围声明的代码(在Scala中): val dtUTC = new DateTime("2010-10-28T04:00") println("dtUTC = " + dtUTC) val dtLocal = timestampLocal(dtU
我想将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 解决方法
这是我在当前项目中使用的内容.
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |