Home  >  Article  >  Java  >  How to Solve the \'java.text.ParseException: Unparseable date\' Exception in Java?

How to Solve the \'java.text.ParseException: Unparseable date\' Exception in Java?

DDD
DDDOriginal
2024-11-19 12:40:03550browse

How to Solve the

Parsing "java.text.ParseException: Unparseable date" Issue in Java

While attempting to parse a date string into a Date object, you may encounter the "java.text.ParseException: Unparseable date" exception. To resolve this issue and accurately convert the date string to the desired format, follow these steps:

1. Define an Appropriate Date Format Pattern

The pattern specified in the SimpleDateFormat object does not match the input date string format. To address this, use a pattern that corresponds to the input date's format:

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);

2. Parse the Input Date

Using the defined Date Format, parse the input date string into a Date object:

Date parsedDate = sdf.parse(date);

3. Define an Output Date Format (Optional)

If the target output format differs from the input date format, create a new SimpleDateFormat object to format the parsed Date object:

SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");

4. Print the Formatted Date

Finally, use the output Date Format to print the parsed date in the desired format:

System.out.println(print.format(parsedDate));

Additional Notes

  • Specify the locale to avoid issues with date parsing if the locale is not English.
  • Avoid using ambiguous time zone specifiers like "IST" and opt for more specific time zone names.

The above is the detailed content of How to Solve the \'java.text.ParseException: Unparseable date\' Exception in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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