Ali*_*han 7 datetime android runtime-error exception kotlin
我想将 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)
Run Code Online (Sandbox Code Playgroud)
但是使用此代码,我的应用程序崩溃了。关于此的任何帮助
日志显示这个
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)
Run Code Online (Sandbox Code Playgroud)
小智 5
上述内容的固定版本,因为您的日期输入中没有 Z。而且他们的输出格式与您的不匹配。我建议阅读文档https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html ,这样您就可以根据需要修改响应。
fun convertISOTimeToDate(isoTime: String): String? {
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
var convertedDate: Date? = null
var formattedDate: String? = null
try {
convertedDate = sdf.parse(isoTime)
formattedDate = SimpleDateFormat("MMMMM dd,yyyy").format(convertedDate)
} catch (e: ParseException) {
e.printStackTrace()
}
return formattedDate
}
Run Code Online (Sandbox Code Playgroud)
使用现代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`.
Run Code Online (Sandbox Code Playgroud)
2018 年 4 月 13 日
对于早期的 Android,请参阅下方底部的项目符号。
将 DateTime 字符串(如“2018-04-13T20:00:00.0400”)转换为“2018 年 4 月 13 日”。
您正在使用多年前被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 );
Run Code Online (Sandbox Code Playgroud)
ldt.toString(): 2018-04-13T20:00:00.040
提取日期部分。
LocalDate ld = ldt.toLocalDate() :
Run Code Online (Sandbox Code Playgroud)
ld.toString(): 2018-04-13
生成表示该日期值的文本。让java.time自动本地化。要本地化,请指定:
FormatStyle 确定字符串的长度或缩写。Locale 确定:
代码:
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG ).withLocale( Locale.US );
String output = ld.format( f );
Run Code Online (Sandbox Code Playgroud)
2018 年 4 月 13 日
请注意:一个LocalDateTime对象是不是一个时刻,是不是在时间轴上的一个点。缺少与 UTC 或时区的任何偏移意味着它代表了一系列潜在的时刻,大约在 26-27 小时的范围内(全球当前的时区范围)。如果输入字符串被暗指代表在一些地区的挂钟时间了一下,你应该申请一个ZoneId获得ZonedDateTime提取物之前LocalDate。这在 Stack Overflow 上已经多次展示过。
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
从哪里获得 java.time 类?
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval,YearWeek,YearQuarter,和更多。
| 归档时间: |
|
| 查看次数: |
10966 次 |
| 最近记录: |