SimpleDate格式解析错误

时间:2016-04-08 11:32:21

标签: java ionic-framework simpledateformat datetime-format

我收到了这个错误:

Sub Clean_PDF_Para()
'crude macro to fix paragraph markers (invisible)( so text copied from pdf is formatted to fill lines
'currently based on selected range


   With Selection.Find
        .Text = "^p"
        .Replacement.Text = "  "
        .Wrap = wdFindStop ' think this is required to stop it fixing (breaking) the whole selction
    End With

    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Style = ActiveDocument.Styles("Normal") 'added to fix the paragraph style so it doesn't take the form of a heading.
End Sub

我用过这个java.text.ParseException: Unparseable date: "Fri Apr 08 2016 00:00:00 GMT+0530 (IST)" 任何人都可以建议我一个正确的吗?

SimpleDateFormat

3 个答案:

答案 0 :(得分:1)

如果您要提供日期为" Fri Apr 08 2016 00:00:00 GMT + 0530(IST)"。那就错了。请删除GMT。所有时区仅从GMT计算。

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

答案 1 :(得分:1)

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

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

这个小片段应该清除混乱。这与你正在做的事情相反:

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)

答案 2 :(得分:1)

您可以尝试模式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)"));
  

输出(在我的系统中):2016年4月8日星期五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 [亚/加尔各答]