4

我尝试将字符串从 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,但对于工厂来说,它会得到错误结果。

4

2 回答 2

6

我想这一切都已经在评论中了,所以这只是总结一下。

(1)您的格式模式字符串是正确的。您只需.ISO_DATE要从以下行中删除,因此它变为:

          DateTimeFormatter Parser = DateTimeFormatter.ofPattern(pattern);

ISO_DATE例如接受 '2011-12-03+01:00' 或 '2011-12-03',一个没有时间的日期,有或没有与 UTC 的偏移量;就我而言,你在这里没有什么可使用的可以告诉。)

(2)由于您的字符串似乎既没有时区也没有偏移量,请使用LocalDateTime

          LocalDateTime a = LocalDateTime.parse(aTime, Parser);
          LocalDateTime b = LocalDateTime.parse(bTime, Parser);

如果计算时需要考虑夏令时(DST)等,解析后转换时间:

          ZoneId timeZone = ZoneId.systemDefault();
          ZonedDateTime a = LocalDateTime.parse(aTime, Parser).atZone(timeZone);
          ZonedDateTime b = LocalDateTime.parse(bTime, Parser).atZone(timeZone);

请仔细考虑用于转换的时区,以确保获得预期的结果。

(3) A行不通ChronoUnitnull我不知道你想要哪个,所以这个选项是随机选择的:

          ChronoUnit unit = ChronoUnit.DAYS;

通过这三个更改,您的方法可以在我的计算机上很好地执行。在一次运行中,它打印了:

2016-06-28 22:29:44.700228
365

在同一次运行中,它返回了一个字符串365

于 2017-06-11T19:20:16.373 回答
4

i get the answer by Andreas(in comment) i used this code to achieve my goal finally

    static String getWatchTime(JSONObject aJson, JSONObject bJson) {
    double 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).withZone(ZoneId.systemDefault());

          System.out.println(aTime);


          ZonedDateTime a = ZonedDateTime.parse(aTime,Parser);
          ZonedDateTime b = ZonedDateTime.parse(bTime,Parser);

          System.out.println(a);
          System.out.println(b);
          //ChronoUnit unit = null ;
          difference = ChronoUnit.MICROS.between(a, b);


    } catch (JSONException e) {
        e.printStackTrace();
    } /*catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/

    String t = difference +"";
    return t;

}

I didn't set the TimeZone, so can't convert the input as string to ZonedDateTime. And I need to get the microssecond , hence i use ChronoUnit.MICROS.between() Thanks for answers

于 2017-06-11T19:28:55.467 回答