Search code examples
scaladatedate-formatsimpledateformat

Convert String "22/08/2013" to date format 2013/08/22


This is my code in scala

 var s:String ="22/08/2013"
 var  simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
 var  date:Date = simpleDateFormat.parse(s);
 println(date)

the date is not change. the format of date same as 22/08/2013 there is no change to 2013/08/22

How to change the format dd/mm/yyyy to yyyy/mm/dd in scala


Solution

  • You need to define a SimpleDateFormat which first parses your string and get Date from it. And then convert it to whatever format.

     var s:String ="22/08/2013"
     var  simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy");
     var  date:Date = simpleDateFormat.parse(s);
     val ans = new SimpleDateFormat("yyyy/mm/dd").format(date) 
     println(ans)