Parsing Exception: "java.text.ParseException: Unparseable Date" with SimpleDateFormat
When attempting to parse a date from the string "Sat Jun 01 12:53:10 IST 2013" using SimpleDateFormat, you may encounter the error "java.text.ParseException: Unparseable date." This error occurs because the pattern used for parsing, "MMM d, yyyy HH:mm:ss," does not match the input string format.
Solution:
To resolve this issue and successfully parse the given date, you should adjust the pattern in SimpleDateFormat to align with the input string's specific format. For the provided string, a more appropriate pattern would be:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
where:
Output Formatting:
Once the date has been successfully parsed, you can use a second SimpleDateFormat to format the output in your desired format, "MMM d, yyyy HH:mm:ss":
Date parsedDate = sdf.parse(date); SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss"); System.out.println(print.format(parsedDate));
Additional Considerations:
The above is the detailed content of How to Parse \'java.text.ParseException: Unparseable Date\' for \'Sat Jun 01 12:53:10 IST 2013\' using SimpleDateFormat?. For more information, please follow other related articles on the PHP Chinese website!