Parsing of string to Date Object
I have string "Tue Nov 12 2010",I开发者_StackOverflow社区 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???
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
.
Ofcourse because it is not in format
format for Tue Nov 12 2010
should be EEE MMM dd yyyy
Have a look at docs
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.
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.
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"
You are using the wrong format for the date. To parse it according to your string format use "EEE MMM dd yyyy"
精彩评论