Home >Java >javaTutorial >How to Resolve \'java.text.ParseException: Unparseable Date\' Exception?

How to Resolve \'java.text.ParseException: Unparseable Date\' Exception?

DDD
DDDOriginal
2024-11-19 05:29:02672browse

How to Resolve

Resolving "java.text.ParseException: Unparseable Date" Exception

When attempting to parse a date using SimpleDateFormat, an "Unparseable Date" exception may arise due to mismatched input string formats. To address this, the input string and SimpleDateFormat pattern must be aligned.

In this specific case, the input string "Sat Jun 01 12:53:10 IST 2013" does not correspond to the pattern "MMM d, yyyy HH:mm:ss" used by the SimpleDateFormat instance. To correctly parse the input, a different pattern is required.

The revised pattern below accommodates the specific elements in the input string:

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

Additionally, to print the parsed date in the desired format, another SimpleDateFormat instance is required:

Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));

Notes:

  • Including the appropriate locale in the SimpleDateFormat constructor ensures accurate date parsing, as day names can vary across different locales.
  • Using the correct time zone name in the input string eliminates potential ambiguities and improves parse accuracy.

The above is the detailed content of How to Resolve \'java.text.ParseException: Unparseable Date\' Exception?. 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