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
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!