Android无法解析的日期异常 [英] Android Unparseable date Exception
问题描述
当尝试解析发送到我的Android客户端的Date字符串时,我得到了不可解析的日期。
I get a Unparseable date when trying to parse a Date string that is sent to my android client.
这是一个例外:
java.text.ParseException:无法解析的日期:java.text.DateFormat处的 2018-09-18T00:00:00Z(在
偏移量19处) .parse(DateFormat.java:571)
java.text.ParseException: Unparseable date: "2018-09-18T00:00:00Z" (at offset 19) at java.text.DateFormat.parse(DateFormat.java:571)
基于C#的后端发送的日期格式(C#对象的属性为 DateTime ):
The date format my C# based backend sends (The C# object Property is DateTime):
2018-09-18T00:00:00Z
2018-09-18T00:00:00Z
我的Java代码失败了:
My Java Code where it fails:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
String targetDate = "2018-09-18T00:00:00Z";
Date date = dateFormat.parse(targetDate));
如何更改代码以解析后端发送的确切格式?
How can I change my code to parse the exact format sent by the backend?
推荐答案
您的后端发送的日期格式遵循ISO-8601即时格式。
The date format sent by your backend follows ISO-8601 instant format.
您可以使用 LocalDate.parse(targetDate,DateTimeFormatter.ISO_INSTANT)
进行解析。
这篇关于Android无法解析的日期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!