How to format a date to following format day.month.year?

Jac*_*ack 0 java calendar simpledateformat

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"
Run Code Online (Sandbox Code Playgroud)

Code

   SimpleDateFormat formatter = new SimpleDateFormat("dd.mm.yyyy");
   Date date = formatter.parse(param.getFromDate());
   String formattedDate = formatter.format(date);
Run Code Online (Sandbox Code Playgroud)

Nis*_*hia 5

Unparseable date意味着您输入的 dateString 值与预期的格式不同。 例如,如果您的 dateString 是 2014-12-10( yyyy-MM-dd) ,如果您尝试将其格式化为dd-MM-yyyy,则会发生此异常。

下面的代码会帮助你。!

// 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);
Run Code Online (Sandbox Code Playgroud)