android - SimpleDateFormat 解析不适用于数据库中 UTC 格式的日期
问题描述
我使用 Parse.com,每个 ParseObject 都有日期类型为 UTC 格式的日期列。
我想按日期对从查询中获得的那些对象进行排序。所以我使用 SimpleDateFormat 和 Comparator 来比较日期,但它抛出了这个异常:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)
Caused by: java.text.ParseException: Unparseable date: "Thu Dec 06 16:20:00 GMT+01:00 2018" (at offset 0)
代码:
private fun formatQueryData(unsortedList: ArrayList<ParseObject>){
unsortedList.sortWith(object : Comparator<ParseObject>{
var f = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'")
override fun compare(o1: ParseObject, o2: ParseObject): Int {
try {
return f.parse(o1.getDate("date").toString()).compareTo(f.parse(o2.getDate("date).toString()))
} catch (e: ParseException) {
throw IllegalArgumentException(e)
}
}
})
}
解决方案
推荐阅读
- python-3.x - 网址在 chrome 中启动,但在 Firefox 中我收到错误
- apache - 只有全球 IP 地址的网站应该是 ssl 吗?
- javascript - 单击按钮使用 Angular 导航 Bootstrap 选项卡
- linux - 如何打印两个/多个匹配行?
- javascript - RXJS 中的条件连接?
- swift - 动画变换仅适用于超级视图
- javascript - Gridstack 动态创建的小部件不拖动
- oauth - 当我的移动应用程序有号码和 otp 登录时,如何在 Kong 中实施 oauth?
- javascript - 如何获取浏览器 jquery 的语言显示
- mysql - Hiberante Inner join 给出了“连接的预期路径”异常