java 无法将字符串转换为 ZonedDateTime:DateTimeParseException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44487065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:11:26  来源:igfitidea点击:

Can't convert string to ZonedDateTime: DateTimeParseException

javajsondatetime-parsingzoneddatetime

提问by Zi-yan Tseng

I try to convert string from JSON to ZonedDateTime just like

我尝试将字符串从 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;

}

and always get the error

并且总是得到错误

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)

I want to get the difference between these two date. I've tried SimpleDateFormatbut it will get the error result, for the mills.

我想知道这两个日期之间的差异。我试过,SimpleDateFormat但它会得到错误结果,对于工厂。

回答by Ole V.V.

I think it's already all in the comments, so this is just to sum up.

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

(1) Your format pattern string is correct. You just need to delete .ISO_DATEfrom the following line, so it becomes:

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

          DateTimeFormatter Parser = DateTimeFormatter.ofPattern(pattern);

(ISO_DATEaccepts for example '2011-12-03+01:00' or '2011-12-03', a date without time with or without an offset from UTC; you've got nothing to use it for here as far as I can tell.)

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

(2) Since your string seems to have neither time zone nor offset, use LocalDateTime:

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

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

If you need to take summer time (DST) or the like into account when calculating the difference, convert the time after parsing:

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

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

Please think twice about the time zone to use for conversion so you are sure to get the expected result.

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

(3) A ChronoUnitof nullwon't work. I don't know which one you intended, so this option is picked rather at random:

(3)ChronoUnitnull将无法工作。我不知道你想要哪个,所以这个选项是随机选择的:

          ChronoUnit unit = ChronoUnit.DAYS;

With these three changes your method executes nicely on my computer. In one run it printed:

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

2016-06-28 22:29:44.700228
365

In the same run it returned a string of 365.

在同一次运行中,它返回了一串365.

回答by Zi-yan Tseng

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

我得到了 Andreas 的答案(在评论中)我最终使用此代码实现了我的目标

    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

我没有设置时区,所以不能将输入作为字符串转换为 ZonedDateTime。我需要获得微秒,因此我使用ChronoUnit.MICROS.between()感谢您的回答