mani
mani

Reputation: 155

Exception :java.text.ParseException: Unparseable date: "02/15/2012"

try {
    String str_date=date;
    DateFormat formatter ; 
    Date date1 ; 
    formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:SSSS");
    date1 = (Date)formatter.parse(str_date);  
    System.out.println("Today is " +date1 );
}
catch (ParseException e)
{
    System.out.println("Exception :"+e); 
}  

i got Exception :java.text.ParseException: Unparseable date: "02/15/2012"

any one help me to solve the issue

Upvotes: 0

Views: 37334

Answers (5)

Ilya
Ilya

Reputation: 29703

remake

formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:SSSS"); 

to

formatter = new SimpleDateFormat("MM/dd/yyyy"); 

Upvotes: 3

giorgiline
giorgiline

Reputation: 1371

I like more doing it like this:

String dateString = "22/11/1982";
DateFormat dF = DateFormat.getDateInstance(DateFormat.SHORT);
Date date = dF.parse(dateString);

Upvotes: 3

subodh
subodh

Reputation: 6158

You are passing date in this format 02/15/2012 so you need to do this

formatter = new SimpleDateFormat("MM/dd/yyyy");

if you are passing date in this format 02-15-2012 then you have to do this

formatter = new SimpleDateFormat("MM-dd-yyyy"); 

above will not gives you any error.

Upvotes: 4

mas-designs
mas-designs

Reputation: 7546

formatter = new SimpleDateFormat("MM/dd/yyyy");

That is the code for you. You are defining the wrong "pattern" in your code. How should the parser parse the date 22/05/2012 if you tell him "Hey the date I will give you has the pattern of yyyy-MM-dd HH:mm:SSSS" ?

Upvotes: 3

Buhake Sindi
Buhake Sindi

Reputation: 89189

The formatting is wrong, even the answers here are wrong.

Your date starts with MM/dd/yyyy pattern so change your format to:

formatter = new SimpleDateFormat("MM/dd/yyyy");

The reason why it can't be dd/MM/yyyy is because you can't have a month of 15 (there's only 12 months in a year).

Upvotes: 1

Related Questions