0

My date string is like this dd.MM.yyyy-HH.mm.ss . I am doing following:

String s_date= "13.06.2012-12.12.12"
Date d_date = new SimpleDateFormat("dd.MM.YYYY-HH.mm.ss", Locale.ENGLISH).parse(s_date);    

But it is throwing Unparseable date: "13.06.2012-12.12.12" Exception.

How can I make it work for the given date-time format ?

2
  • 1
    That actually doesn't throw an exception for me - which surprises me, given that you haven't provided any time parts in your format string... Commented Jun 13, 2013 at 11:48
  • it works fine for me.. no exception Commented Jun 13, 2013 at 12:01

5 Answers 5

2

You are using capital Y.
Try:

Date d_date = new SimpleDateFormat("dd.MM.yyyy-HH.mm.ss", Locale.ENGLISH).parse(s_date);

Reference

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

Comments

2

You should add time as well:

 new SimpleDateFormat("dd.MM.YYYY-HH.mm.ss", Locale.ENGLISH)

1 Comment

That was a typo .. now I edited but still its throwing the same exception. I am doing this: Date d_date = new SimpleDateFormat("dd.MM.YYYY-HH.mm.ss", Locale.ENGLISH).parse(s_date);
1

String s_date= "13.06.2012-12.12.12" doesn't fit your pattern dd.MM.YYYY. You should remove the part after the - if you want date without hours:

s_date = s_date.substring(0, s_date.indexOf('-'));

or change your pattern as Michał said.

1 Comment

Using what you suggested eliminates the exception, but the date object d_date gets a date which is not right.. ie for any date string it gives this : Sun Dec 30 00:00:00 IST 2012.
0

String s_date= ""13.06.2012-12.12.12";

this is wrong

use String s_date= "13.06.2012-12.12.12";

1 Comment

that was a typo.. Edited it. Thanks.
0

do like this.

String d=new SimpleDateFormat("dd.MM.yyyy").format(date);
System.out.println(d);

3 Comments

I dont want to just print it, rather change it into Date type. So this error comes when I do what you suggested Type mismatch: cannot convert from String to Date
boss its like you want spoon feeding.:)
No, you just didn't got the question.. format gives the string from date object, not vice-versa. :) I was encountering an issue while trying something, its not called spoon feeding. :)

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.