Reputation: 6600
I need to format a date to following format: 10.12.2014
and I am using the following code but it returns following error
Messages:
Unparseable date: "2014-12-10"
Code
SimpleDateFormat formatter = new SimpleDateFormat("dd.mm.yyyy");
Date date = formatter.parse(param.getFromDate());
String formattedDate = formatter.format(date);
Upvotes: 0
Views: 11070
Reputation: 4360
Change your date format, you are parsing yyyy-MM-dd
and providing format as yyyy.MMMM.dd
:-
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter.parse(param.getFromDate());
String formattedDate = formatter.format(date);
Edit:- you updated format input to dd.mm.yyyy
, which again is wrong, and above solution is valid in this case too.
Also you want the date output as 10.12.2014
, which you can do by creating new formatter and parsing the date to string using it:-
formatter = new SimpleDateFormat("dd.MM.yyyy");
String formattedDate2 = formatter.format(date); //
Upvotes: 3
Reputation: 9884
Unparseable date
means your input dateString value is not in the same format as expected.
For example, if your dateString is 2014-12-10 (yyyy-MM-dd
) , if you try to format it to dd-MM-yyyy
, then this exception will occur.
The following code will help you.!
// Existing date is in this format : "2014-12-10"
SimpleDateFormat formatFrom = new SimpleDateFormat("yyyy-MM-dd");
// Required date is in this format : 10.12.2014
SimpleDateFormat formatTo = new SimpleDateFormat("dd.MM.yyyy");
// Convert the String param.getFromDate()==>2014-12-10 to a Date
Date date = formatFrom .parse(param.getFromDate());
// Convert Date to String 10.12.2014
String formattedDate = formatTo .format(date);
Upvotes: 5
Reputation: 312
check this out: Java Docs for SimpleDateFormat
contains all the pattern letters for date and time, helped me a lot when i was looking onto how to implement date and time in my app.
Upvotes: 0