我想将 DateTime 字符串(如“2018-04-13T20:00:00.0400”)转换为“2018 年 4 月 13 日”。我已经使用了代码

val sdf = SimpleDateFormat("yyyy/mm/dd", Locale.getDefault())
val startDate = sdf.parse(data.start_time)

但是使用此代码,我的应用程序崩溃了。关于此的任何帮助

日志显示这个
2018-10-11 16:44:56.948 20482-21021/? E/NowController: Failed to access data from EntryProvider. ExecutionException.
java.util.concurrent.ExecutionException: com.google.android.apps.gsa.sidekick.main.h.n: Could not complete scheduled request to refresh entries. ClientErrorCode: 3
    at com.google.common.util.concurrent.d.eA(SourceFile:85)
    at com.google.common.util.concurrent.d.get(SourceFile:23)
    at com.google.common.util.concurrent.l.get(SourceFile:2)
    at com.google.android.apps.gsa.staticplugins.nowstream.b.a.be.cbB(SourceFile:49)
    at com.google.android.apps.gsa.staticplugins.nowstream.b.a.be.cbA(SourceFile:181)
    at com.google.android.apps.gsa.staticplugins.nowstream.b.a.bh.run(Unknown Source:2)
    at com.google.android.apps.gsa.shared.util.concurrent.at.run(SourceFile:4)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at com.google.android.apps.gsa.shared.util.concurrent.b.g.run(Unknown Source:4)
    at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
    at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)
    at com.google.android.apps.gsa.shared.util.concurrent.b.i.run(SourceFile:6)
 Caused by: com.google.android.apps.gsa.sidekick.main.h.n: Could not complete scheduled request to refresh entries. ClientErrorCode: 3
    at com.google.android.apps.gsa.staticplugins.nowstream.b.a.ar.az(Unknown Source:4)
    at com.google.common.util.concurrent.q.ap(SourceFile:7)
    at com.google.common.util.concurrent.p.run(SourceFile:32)
    at com.google.common.util.concurrent.bt.execute(SourceFile:3)
    at com.google.common.util.concurrent.d.b(SourceFile:275)
    at com.google.common.util.concurrent.d.addListener(SourceFile:135)
    at com.google.common.util.concurrent.p.b(SourceFile:3)
    at com.google.android.apps.gsa.shared.util.concurrent.h.a(SourceFile:16)
    at com.google.android.apps.gsa.shared.util.concurrent.h.a(SourceFile:13)
    at com.google.android.apps.gsa.staticplugins.nowstream.b.a.be.cbB(SourceFile:47)

最佳答案

tl;博士

使用现代 java.time 类,而不是可怕的遗留类。

这是 Java 语法(我还没有学过 Kotlin)。

LocalDateTime                             // Represent a date with time-of-day but lacking offset-from-UTC or time zone. As such, this does *not* represent a moment, is *not* a point on the timeline.
.parse( "2018-04-13T20:00:00.0400" )      // Parse an input string in standard ISO 8601 format. Returns a `LocalDateTime` object.
.toLocalDate()                            // Extract the date-only portion without the time-of-day. Still no time zone or offset-from-UTC. Returns a `LocalDate` object.
.format(                                  // Generate text representing the value of that `LocalDate` object.
    DateTimeFormatter                     // Define a pattern to use in generating text.
    .ofLocalizedDate( FormatStyle.LONG )  // Automatically localize, specifying how long/abbreviated…
    .withLocale( Locale.US )              // … and what human language and cultural norms to use in localizing.
)                                         // Return a `String`.



对于早期的 Android,请参阅下方底部的项目符号。

使用 java.time



您正在使用多年前被 java.time 类取代的糟糕的旧日期时间类。

首先,解析您的输入字符串。但是最后那个 .0400 是什么?也许是几分之一秒?但通常毫秒、微秒或纳秒以 3 位数字为一组显示。所以你这里的四位数是奇数。也许是UTC的偏移量?四位数字是正确的,小时和分钟前导零。但是没有加号或减号来表示早于或晚于 UTC。所以我会选择小数秒。

您的输入缺少与 UTC 或时区偏移的任何指标。所以解析为 LocalDateTime 。您的字符串采用标准 ISO 8601 格式。 java.time 类中默认使用标准格式。
String input = "2018-04-13T20:00:00.0400";
LocalDateTime ldt = LocalDateTime.parse( input );



提取日期部分。
LocalDate ld = ldt.toLocalDate() :



生成表示该日期值的文本。让 java.time 自动本地化。要本地化,请指定:
  • FormatStyle 确定字符串的长度或缩写。
  • Locale 来确定:
  • 用于翻译日名、月名等的人类语言。
  • 决定缩写、大小写、标点符号、分隔符等问题的文化规范。

  • 代码:
    DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG ).withLocale( Locale.US );
    String output = ld.format( f );
    



    注意: LocalDateTime 对象不是片刻,也不是时间线上的一个点。缺少与 UTC 或时区的任何偏移意味着它代表了一系列潜在的时刻,大约在 26-27 小时的范围内(全局当前的时区范围)。如果输入字符串隐式地表示某个区域的挂钟时间中的某个时刻,则应在提取 ZoneId 之前应用 ZonedDateTime 以获取 LocalDate 对象。这在 Stack Overflow 上已经多次展示过。

    关于 java.time

    java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date Calendar SimpleDateFormat

    Joda-Time 项目,现在在 maintenance mode 中,建议迁移到 java.time 类。

    要了解更多信息,请参阅 Oracle Tutorial 。并在 Stack Overflow 上搜索许多示例和解释。规范是 JSR 310

    您可以直接与您的数据库交换 java.time 对象。使用符合 JDBC driver 或更高版本的 JDBC 4.2。不需要字符串,不需要 java.sql.* 类。

    从哪里获得 java.time 类?
  • Java SE 8Java SE 9Java SE 10Java SE 11和更高版本-具有 bundle 实现的标准Java API的一部分。
  • Java 9 添加了一些小功能和修复。
  • Java SE 6Java SE 7
  • 大多数 java.time 功能在 ThreeTen-Backport 中被反向移植到 Java 6 和 7。
  • Android
  • 更高版本的 Android bundle 实现 java.time 类。
  • 对于早期的Android(ThreeTenABP 项目适配了ThreeTen-Backport(上面提到过)。见 How to use ThreeTenABP…

  • ThreeTen-Extra 项目使用附加类扩展了 java.time。该项目是 future 可能添加到 java.time 的试验场。您可能会在这里找到一些有用的类,例如 Interval YearWeek YearQuarter more

    10-08 07:51
    查看更多