java text ParseException Unparseable date

0 votes

I have constantly been 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 could I solve this?

Feb 23, 2022 in Java by Soham
• 9,710 points
19,748 views

1 answer to this question.

0 votes

Your pattern does not align to the input string at all, which is why it is not surprising that it does not work. This would probably work better by using:

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));

Also keep in mind that you should include the locale as if your locale is not English, the day name might not be recognised. 

Hope this helps!

Check out Java certification course here.

Thanks!

answered Feb 23, 2022 by Aditya
• 7,680 points

edited Jun 22, 2023 by Khan Sarfaraz

Related Questions In Java

0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,230 points
2,940 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
2,496 views
0 votes
2 answers

Date format conversion in Java

Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 1); Date date = ...READ MORE

answered Aug 13, 2018 in Java by Sushmita
• 6,920 points
1,078 views
0 votes
1 answer

How can I read a large text file line by line using Java?

// Open the file FileInputStream file = new ...READ MORE

answered May 2, 2018 in Java by Parth
• 4,640 points
900 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,243 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,364 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,443 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,882 views
0 votes
1 answer

Can not find the tag library descriptor for “http://java.sun.com/jsp/jstl/core”

If you have a jstl.jar file while ...READ MORE

answered Feb 17, 2022 in Java by Aditya
• 7,680 points

edited Jun 22, 2023 by Khan Sarfaraz 27,623 views
0 votes
1 answer

How do I get the current date in JavaScript?

To ensure that you get the current ...READ MORE

answered Feb 18, 2022 in Java by Aditya
• 7,680 points
588 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP