我得到这个错误: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)");
我得到这个错误: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)");
如果您的饲喂日期为“2016年4月8日00:00:00 GMT + 0530(IST)”。那就错了。请删除GMT。所有时区仅根据GMT计算。
尝试通过日期为“周五2016年4月08日00:00:00 +0530(IST)”。它会工作。
如果它已经工作,那么请接受答案。谢谢。 –
建议更改输入通常不起作用。解决方案通常是调整模式,而不是可能来自用户无权更改的外部来源的输入。 –
正确的语法分析日期字符串应该是:
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)
你可以尝试模式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]
注:'IST'也是'爱尔兰标准Time','伊朗标准Time'和'以色列标准时间'我建议你最后放弃'(IST)'。 –
@Ravi:你检查我的答案吗?如果它为你工作,那么接受+ upvote它。 – Unknown
不!老兄它不适合我这只是给演员解析错误 –