2016-04-08 60 views
0

我得到这个错误:SimpleDate格式解析错误

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)"); 
+1

注:'IST'也是'爱尔兰标准Time','伊朗标准Time'和'以色列标准时间'我建议你最后放弃'(IST)'。 –

+0

@Ravi:你检查我的答案吗?如果它为你工作,那么接受+ upvote它。 – Unknown

+0

不!老兄它不适合我这只是给演员解析错误 –

回答

1

如果您的饲喂日期为“2016年4月8日00:00:00 GMT + 0530(IST)”。那就错了。请删除GMT。所有时区仅根据GMT计算。

尝试通过日期为“周五2016年4月08日00:00:00 +0530(IST)”。它会工作。

+0

如果它已经工作,那么请接受答案。谢谢。 –

+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)

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)")); 

Output (in my system) : 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[Asia/Kolkata]

+0

嗯,我无法重现您的输出“Fri Apr 08 00:00:00 IST 2016”。相反,我在欧洲/柏林时区的“时间2016年4月8日00:00:00 CEST 2016”中获得了这个时间,这个时间距离印度还有3个半小时。所以结果是错误的。你真的测试过你的代码吗? –

+0

嗨。 OP问他无法解析日期(Fri Apr 08 2016 00:00:00 GMT + 0530(IST))。所以我的答案只是解析给定的字符串。我已经把输出结果告诉了他我能够解析日期。 – Unknown

+0

@Meno Hochschild:请参阅我的更新回答 – Unknown