2

I have string "Tue Nov 12 2010",I want to parse it in java.util.Date object. I write below code for this

DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date= format.parse("Tue Nov 12 2010");

It is giving exception like below:

java.text.ParseException: Unparseable date: "Sun Nov 21 2010"

Not getting what is wrong with it???

3
  • 2
    To the right of the text area where you typed your question, there's a box titled How to Format. Worth a read. I've fixed the code formatting for you, but you've asked five questions now, time to start flying solo. Commented Nov 22, 2010 at 13:20
  • 2
    Why can't you see that the "dd/MM/yyyy" format specified doesn't match the date "Tue Nov 12 2010"? Commented Nov 22, 2010 at 13:24
  • It's considered good form to accept an answer if it was helpful. @Alison had both a helpful and the quickest answer. Commented Nov 26, 2010 at 10:55

6 Answers 6

7

Your format is wrong - if you specify a format dd/MM/yyyy, then you need to supply the string to be formatted in the corresponding format (!) e.g. 21/11/2010.

Sign up to request clarification or add additional context in comments.

Comments

4

Ofcourse because it is not in format

format for Tue Nov 12 2010 should be EEE MMM dd yyyy

Have a look at docs

1 Comment

From the Docs linked, D for day in the year and d is for day in the month. Also there is no Y.
2

Learn to read code and use common sense.

DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date= format.parse("Tue Nov 12 2010");

This should be blatantly obvious that the format specified doesn't match the string being parsed. They're on adjacent lines, right next to each other. It doesn't get more straightforward than that.

You need to be able to see something like this if you are to be a successful programmer. If you can't see this, how are you ever going to find similar problems when the two lines causing problems aren't even in the same source code file?

My advice is to take some personal responsibility for learning how to read and debug code. Something like this should be a huge red flag right when you type it that the two lines of code don't match up.

Comments

1

The date format you have created
new SimpleDateFormat("dd/MM/yyyy");
Will only parse dates of that form. I.e. 05/10/1989 You'll need to change the format something more appropriate.

Comments

1

To parse the date you need to provide correct format. For the sample date give by you the format would be "EEE MMM dd yyyy"

Comments

0

You are using the wrong format for the date. To parse it according to your string format use "EEE MMM dd yyyy"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.