获取错误 java.text.ParseException: Unparseable date: (at offset 0) 即使简单日期格式和字符串值相同 [英] Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical

查看:63
本文介绍了获取错误 java.text.ParseException: Unparseable date: (at offset 0) 即使简单日期格式和字符串值相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使要检查的格式和字符串值相同,我也总是收到解析异常.代码如下:

I'm always getting the parse exception even if the format to check and the string value are same. Here is the code:

String format = "EEE MMM dd HH:mm:ss z yyyy";
String value = "Mon Sep 18 10:30:06 MST 2017";
public static boolean isValidFormat(String format, String value) {
    Date date = null;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        date = sdf.parse(value); // here it breaks
        if (!value.equals(sdf.format(date))) {
            date = null;
        }
    } catch (ParseException ex) {
        ex.printStackTrace(); //java.text.ParseException: Unparseable date: 
                                "Mon Sep 18 10:30:06 MST 2017" (at offset 0)
    }
    return date != null;
}

推荐答案

它说你的日期时间字符串在索引 0 处是不可解析的.索引 0 是它说 Mon 的地方,所以三个字母时区缩写并不是第一个疑点.语言环境是.Mon"在英语中用作 Monday 的缩写,但在许多其他语言中则不然.因此,如果您的设备具有非英语语言设置(甚至可能最近已更改),这将完全解释您的观察结果.

It says that your date-time string is unparseable at index 0. Index 0 is where it says Mon, so the three letter time zone abbreviation is not the first suspect. The locale is. "Mon" works as abbreviation for Monday in English, but not in very many other languages. So if your device has a non-English language setting — maybe it has even been changed recently — this will fully explain your observation.

短视的解决方案是

        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ROOT);

我使用 Locale.ROOT 表示不应进行特定于语言的处理.如果您的字符串是英语,因为英语通常是全球计算中使用的语言,我会认为这个选择是合适的.另一方面,如果它是英语,因为它来自讲英语的语言环境,那么该语言环境将是正确的使用.

I use Locale.ROOT to mean that no language specific processing should be done. If your string is in English because English is generally the language used in computing around the globe, I would consider this choice appropriate. If on the other hand it is in English because it comes from an English speaking locale, that locale will be the right one to use.

通过此更改,在我的计算机上,您的代码将日期格式化为 Mon Sep 18 11:30:06 MDT 2017,正如您所看到的,这与我们开始时的值不同from,所以你的方法返回false.我的 JVM 将 MST 理解为山地标准时间,然后假定为 9 月的夏令时 (DST) 并相应地格式化字符串.

With this change, on my computer your code formats your date into Mon Sep 18 11:30:06 MDT 2017, which, as you can see is not the same as the value we started out from, so your method returns false. My JVM understood MST as Mountain Standard Time, and then assumed summer time (DST) in September and formatted the string accordingly.

也就是说,DateSimpleDateFormat 是早已过时的类.您应该考虑摆脱它们并改用现代 Java 日期和时间 API.在 Android 上,您可以在 ThreeTenABP 中获得它,请参阅这个问题:How to use ThreeTenABP in安卓项目.现在你可以这样做:

That said, Date and SimpleDateFormat are long outdated classes. You should give it a thought to get rid of them and use the modern Java date and time API instead. On Android you get it in the ThreeTenABP, see this question: How to use ThreeTenABP in Android Project. Now you may do:

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format, Locale.ROOT);
    try {
        return ZonedDateTime.parse(value, dtf).format(dtf).equals(value); 
    } catch (DateTimeParseException dtpe) {
        dtpe.printStackTrace();
        return false;
    }

这与上面的行为相同.

您应该尽可能避免使用三个和四个字母的时区缩写.它们不是标准化的,而且通常是模棱两可的.例如,MST 可能表示马来西亚标准时间或山区标准时间.后者甚至不是一个完整的时区,因为 MDT 用于一年中的大部分时间,这导致了我上面所说的问题.

You should avoid the three and four letter time zone abbreviations where you can. They are not standardized and generally ambiguous. MST, for example, may mean Malaysia Standard Time or Mountain Standard Time. The latter isn’t even a full time zone, since MDT is used for the greater part of the year, which caused the trouble I observed as I said above.

相反,看看您是否可以获得 ISO 8601 格式的字符串,例如 2017-09-18T10:30:06+08:00.其次,只要得到一些明确的东西.一种方法是包含与 UTC 的偏移量而不是时区 ID(或两者).

Instead, see if you can get a string in ISO 8601 format, like 2017-09-18T10:30:06+08:00. Second best, just get something unambiguous. One way is to include an offset from UTC rather than a time zone ID (or both).

这篇关于获取错误 java.text.ParseException: Unparseable date: (at offset 0) 即使简单日期格式和字符串值相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆