I am getting a parsing exception while I am trying the following code:
String date="Sat Jun 01 12:53:10 IST 2013"; SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss"); Date currentdate; currentdate=sdf.parse(date); System.out.println(currentdate);
Exception:
Exception in thread "main" java.text.ParseException: Unparseable date: "Sat Jun 01 12:53:10 IST 2013" at com.ibm.icu.text.DateFormat.parse(DateFormat.java:510)
Input: Sat Jun 01 12:53:10 IST 2013
Expected output: Jun 01,2013 12:53:10
How to solve this?
Try this: SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy"); in. setTimeZone(TimeZone. getTimeZone("Asia/Calcutta")); //or Asia/Jerusalem String s2 = "Fri Oct 23 11:07:08 IST 2015"; Date date = in.
parseexception: unparseable date” error message, when you try to parse the date string into another desired format. Don't worry it's a very common error message that users generally faced while parsing a date in java using SimpleDateFormat class.
Java SimpleDateFormat ExampleString pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);
public class ParseException extends Exception. Signals that an error has been reached unexpectedly while parsing. See Also: Exception , Format , FieldPosition , Serialized Form.
Your pattern does not correspond to the input string at all... It is not surprising that it does not work. This would probably work better:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Then to print with your required format you need a second SimpleDateFormat:
Date parsedDate = sdf.parse(date); SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss"); System.out.println(print.format(parsedDate));
Notes:
String date="Sat Jun 01 12:53:10 IST 2013"; SimpleDateFormat sdf=new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy"); Date currentdate=sdf.parse(date); SimpleDateFormat sdf2=new SimpleDateFormat("MMM dd,yyyy HH:mm:ss"); System.out.println(sdf2.format(currentdate));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With