【问题标题】:SimpleDate Format parsing errorSimpleDateFormat 解析错误
【发布时间】:2016-04-08 11:32:21
【问题描述】:

我收到此错误:

java.text.ParseException: Unparseable date: "Fri Apr 08 2016 00:00:00 GMT+0530 (IST)"

我用过这个SimpleDateFormat 谁能给我推荐一个正确的?

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z (Z)");

【问题讨论】:

  • 注意:IST 也是Irish Standard TimeIran Standard TimeIsraeli Standard Time 我建议你把(IST)放在最后。
  • @Ravi:你检查我的答案了吗?如果它对您有用,请接受+upvote。
  • 不!老兄,它对我不起作用,它只会给出转换解析错误
  • 你试过同样的代码吗?你的java版本?

标签: java ionic-framework simpledateformat datetime-format


【解决方案1】:

如果您将喂食日期设为“Fri Apr 08 2016 00:00:00 GMT+0530 (IST)”。那么就错了。请删除 GMT。所有时区均仅从格林威治标准时间计算。

尝试将日期传递为“Fri Apr 08 2016 00:00:00 +0530 (IST)”。它会起作用的。

【讨论】:

  • 如果有效,请接受答案。谢谢。
  • 建议更改输入通常不会奏效。解决方案通常是适应模式,而不是可能来自用户无权更改的外部来源的输入。
【解决方案2】:

正确的可解析日期字符串应该是:

Fri Apr 08 2016 00:00:00 IST (+0530)

这个小sn-p应该可以消除混乱。这与您正在做的相反:

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z (Z)");

String strDate = format.format(new Date());
System.out.println(strDate);

输出为:Fri Apr 08 2016 17:26:34 IST (+0530)

【讨论】:

    【解决方案3】:

    你可以试试EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)的模式

    1) 使用 Java 1.6:

    System.out.println(fromStringToDate("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)", "EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)"));
    

    输出(在我的系统中):Fri Apr 08 00:00:00 IST 2016

    请参阅此链接了解时区值Java TimeZone List

    public static Date fromStringToDate(String myPotentialDate,String pattern) throws Exception{
            // DateFormat myDateFormat = new SimpleDateFormat(pattern);
            String countryCode = "US";
            String languageCode = "en";
            String timeZone = "Asia/Kolkata";
            DateFormat myDateFormat = getDateFormat(pattern,countryCode,languageCode,timeZone);
            // We set the Leniant to false
            myDateFormat.setLenient(false);
            try {
                return myDateFormat.parse(myPotentialDate);
            }
            catch (ParseException e) {
                // Unparsable date
                throw new Exception("Unparsable date '"+myPotentialDate+"' with pattern '"+pattern+"'. Due to '"+e+"'",e);
            }
    
        }
    
        private static DateFormat getDateFormat(String pattern,String countryCode,String languageCode,String timeZoneId){
            // We build the Local
            Locale myLocale = new Locale(languageCode,countryCode);
            // We build the DateFormat with the Local and the pattern
            DateFormat myDateFormat = new SimpleDateFormat(pattern,myLocale);
            // We set the TimeZone to the correct one
            myDateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneId));
            // We set the Leniant to false
            myDateFormat.setLenient(false);
            return myDateFormat;
        }
    

    2)使用Java 1.8 Java8 Date time API

            String countryCode = "US";
            String languageCode = "en";
            String timeZoneId = "Asia/Kolkata";
    
            LocalDateTime dt = LocalDateTime.parse("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)",
                    DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)").withLocale(new Locale(languageCode,countryCode)));
            ZoneId zoneId= ZoneId.of(timeZoneId); 
            ZonedDateTime zdt= ZonedDateTime.of(dt, zoneId);
            System.out.println(zdt);
    

    输出:

    2016-04-08T00:00+05:30[亚洲/加尔各答]

    【讨论】:

    • 嗯,我无法重现您的输出“Fri Apr 08 00:00:00 IST 2016”。相反,我在我的欧洲/柏林时区得到“Fri Apr 08 00:00:00 CEST 2016”,这比印度晚了三个半小时。所以结果是错误的。你真的测试过你的代码吗?
    • 嗨。 OP 询问他无法解析日期(Fri Apr 08 2016 00:00:00 GMT+0530 (IST))。所以我的答案只是解析给定的字符串。此外,我将输出只是为了告诉他我能够解析日期。
    • 我仍然得到“Thu Apr 07 22:00:00 UTC 2016”,在网站“compilejava.net”上进行了测试
    • @Meno Hochschild 现在获得“2016 年 4 月 7 日星期四 18:30:00 UTC”
    • 因为我关于SimpleDateFormat 的声明(OP 主要要求的)。其余的答案还可以,但只有补充的特征。我对SimpleDateFormat-code 的新测试不同!!!工作场所表明,结果似乎取决于具体的 Java 版本或相关的 tzdb 版本。我对SimpleDateFormat 的第一次测试使用了 v1.7.0_51。我的第二个测试使用 Java-8-platform 确认了您的测试结果。结论:SimpleDateFormat 在某种程度上是不可靠的,但如果 OP 只是更加注意格式模式,它可能适用于最新的 java 平台上的 OP。
    猜你喜欢
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    相关资源
    最近更新 更多