Android尝试解析日期格式时出现不可解析的日期异常。

huangapple go评论66阅读模式
英文:

Android Unparseable date exception when try to parse date format

问题

我尝试使用以下代码解析此日期 "Wed Jul 12 2023 23:58:20 GMT+0000 (Coordinated Universal Time)"

val sdf = SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss ", Locale.getDefault())
val time = try {
    val mDate = sdf.parse(input)
    mDate!!.time
} catch (e: ParseException) {
    -1
}

但我得到以下错误

无法解析的日期: "Wed Jul 12 2023 23:58:20 GMT+0000 (Coordinated Universal Time)"

英文:

I try to parse this date "Wed Jul 12 2023 23:58:20 GMT+0000 (Coordinated Universal Time)"
using this code

  val sdf = SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss ", Locale.getDefault())
    val time = try {
        val mDate = sdf.parse(input)
        mDate!!.time
    } catch (e: ParseException) {
        -1
    }

But I get this error

> Unparseable date: "Wed Jul 12 2023 23:58:20 GMT+0000 (Coordinated
> Universal Time)"

答案1

得分: 0

这只会发生在本地化更改时。
在我的情况下,该函数在英语环境下运行正常,但当应用切换到阿拉伯语时,我遇到了问题,

我将 Locale.getDefault() 更改为 Locale.ENGLISH,问题得以解决。

英文:

This only happens if the localization changed.
on my case the function works fine in English but when the app turns to Arabic here I faced the issue ,

> I Changed Locale.getDefault() to Locale.ENGLISH and the problem solved

huangapple
  • 本文由 发表于 2023年7月13日 09:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定