我尝试将字符串从 JSON 转换为 ZonedDateTime 就像
static String getWatchTime(JSONObject aJson, JSONObject bJson) {
long difference = 0 ;
try {
String aTime = aJson.getString("time_utc_8");
String bTime = bJson.getString("time_utc_8");
String pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS";
DateTimeFormatter Parser = DateTimeFormatter.ofPattern(pattern).ISO_DATE;
System.out.println(aTime);
ZonedDateTime a = ZonedDateTime.parse(aTime, Parser);
ZonedDateTime b = ZonedDateTime.parse(bTime, Parser);
ChronoUnit unit = null;
difference = unit.between(a, b);
System.out.println(difference);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String t = difference +"";
return t;
}
总是得到错误
Exception in thread "main" java.time.format.DateTimeParseException: Text '2016-06-28 22:29:44.700228' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2016-06-28T22:29:44.700228 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.OffsetDateTime.parse(Unknown Source)
at Q2.getWatchTime(Q2.java:321)
at Q2.WatchTime(Q2.java:265)
at Q2.main(Q2.java:31)
我想知道这两个日期之间的区别。我已经尝试过SimpleDateFormat
,但对于工厂来说,它会得到错误结果。