代码之家  ›  专栏  ›  技术社区  ›  anako

乔达约会时间。如何将时区设置为解析的日期时间?

  •  1
  • anako  · 技术社区  · 6 年前

    我正在使用Scala和play框架。我想用简单的日期来解析字符串,并说它是UTC格式的。所以如果我有 我想得到 2018-10-04T00:00:00.000Z

    有了这个:

    DateTime.parse("2018-10-04", DateTimeFormat.forPattern("yyyy-mm-dd")).withZone(DateTimeZone.UTC)
    

    我不停地

    1 回复  |  直到 6 年前
        1
  •  3
  •   Andy Hayden bigbug    6 年前

    一种方法是使用 LocalDatetime

    scala> LocalDateTime.parse("2018-10-04", DateTimeFormat.forPattern("yyyy-MM-dd"))
    res11: org.joda.time.LocalDateTime = 2018-10-04T00:00:00.000
    

    那你可以用 toDateTime

    scala> LocalDateTime.parse("2018-10-04", DateTimeFormat.forPattern("yyyy-MM-dd")).toDateTime(DateTimeZone.UTC)
    res12: org.joda.time.DateTime = 2018-10-04T00:00:00.000Z
    

    另外:应该使用MM(月)而不是MM(分钟)。

        2
  •  0
  •   ForeverLearner    3 年前

    首先是进口。

    import org.joda.time.{DateTime, DateTimeZone}
    import org.joda.time.format.DateTimeFormat
    

    val timeString = "T00:00:00.000Z"
    

    我们使用字符串插值将其附加到传入日期。

    DateTime.parse(s"2018-10-04$timeString", DateTimeFormat.forPattern("yyyy-mm-dd'T'HH:mm:ss.SSSZ")).withZone(DateTimeZone.UTC)