Home > Java > javaTutorial > How to Parse \'java.text.ParseException: Unparseable Date\' for \'Sat Jun 01 12:53:10 IST 2013\' using SimpleDateFormat?

How to Parse \'java.text.ParseException: Unparseable Date\' for \'Sat Jun 01 12:53:10 IST 2013\' using SimpleDateFormat?

Barbara Streisand
Release: 2024-11-17 02:29:03
Original
854 people have browsed it

How to Parse

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);
Copy after login

where:

  • "EE MMM dd HH:mm:ss z yyyy" represents the input string's date and time format (e.g., Saturday, June 01, 2013, 12:53:10 IST).
  • Locale.ENGLISH ensures the parser understands the English day name format.

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));
Copy after login

Additional Considerations:

  • It's recommended to use the precise time zone name in the input string rather than the ambiguous "IST" to avoid potential parsing problems.
  • Ensure the locale specified in SimpleDateFormat matches the locale of the expected input to appropriately handle day names and other locale-specific format elements.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template