【问题标题】:How to convert this "Wed, 10 Jun 2020 10:16:24 GMT" to this "2020-05-10T12:30:45" datetimestr format如何将此“2020 年 6 月 10 日星期三 10:16:24 GMT”转换为“2020-05-10T12:30:45”datetimestr 格式
【发布时间】:2020-09-30 08:58:52
【问题描述】:

我正在尝试这个,但我在解析时遇到异常

{
  val format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss")
  val format_input = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss Z")
    format.format(
      format_input.parse(
        "Wed, 10 Jun 2020 10:16:24 GMT"
      )
    )
}

【问题讨论】:

  • 您使用旧的和过时的SimpleDate 库有什么原因吗?
  • 不,我只是一个使用 scala 和 java 库的新手,有更好的方法吗?

标签: scala date time format


【解决方案1】:

我强烈建议使用较新的 java.time.LocalDateTime 实用程序来代替破旧的 SimpleDate 库。

因为您的输入格式是预定义的DateTimeFormatter 格式之一,并且您想要的输出格式与LocalDateTime 默认格式相同,所以解决方案变得非常简单。

import java.time.format.DateTimeFormatter._
import java.time.LocalDateTime

LocalDateTime.parse("Wed, 10 Jun 2020 10:16:24 GMT"
                   ,RFC_1123_DATE_TIME).toString

//res0: String = 2020-06-10T10:16:24

【讨论】:

    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多