Android org.threeten.bp.format.DateTimeParseException
当我尝试像这样解析日期时,会出现以下错误:Android org.threeten.bp.format.DateTimeParseException,android,date,kotlin,date-parsing,threetenabp,Android,Date,Kotlin,Date Parsing,Threetenabp,当我尝试像这样解析日期时,会出现以下错误: 无法解析文本“2019-11-04”:无法从TemporalAccessor获取LocalDateTime:DateTimeBuilder[,ISO,null,2019-11-04,null],键入org.threeten.bp.format.DateTimeBuilder使用LocalDate代替LocalDateTime,如下所示: val dateFormatter= DateTimeFormatterBuilder() .app
无法解析文本“2019-11-04”:无法从TemporalAccessor获取LocalDateTime:DateTimeBuilder[,ISO,null,2019-11-04,null],键入org.threeten.bp.format.DateTimeBuilder使用
LocalDate
代替LocalDateTime
,如下所示:
val dateFormatter= DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd")
.toFormatter()
val begin = (LocalDateTime.parse("2019-11-04", dateFormatter).atOffset(ZoneOffset.UTC)
.toInstant()).atZone(ZoneId.of(timeZoneIdentifier))
但是这个调用需要minapi26。对于旧版API,请参见,因为
2019-11-04
是ISO 8601格式和LocalDate
,而其他java.time类将ISO 8601格式解析为其默认格式,因此不需要任何显式格式化程序。仅此而已:
val begin = (LocalDate.parse("2019-11-04", dateFormatter).atOffset(ZoneOffset.UTC)
.toInstant()).atZone(ZoneId.of(timeZoneIdentifier))
假设timeZoneIdentifier
为Europe/Bu加勒斯特
,则结果为2019-11-04T02:00+02:00[欧洲/布加勒斯特]
的ZoneDateTime
无法将字符串解析为LocalDateTime
。这不仅需要日期,还需要一天中的某个时间,正如您所知,您的字符串只包含前者
链接:可能是我无法编译的副本。我发现类型LocalDate的方法atOffset(ZoneOffset)是未定义的。
val begin = LocalDate.parse("2019-11-04")
.atStartOfDay(ZoneOffset.UTC)
.withZoneSameInstant(ZoneId.of(timeZoneIdentifier))