How to convert string “2011-11-29 12:34:25” to date in “dd-MM-yyyy” format in JAVA

前端 未结 6 849
死守一世寂寞
死守一世寂寞 2020-12-21 15:30

I am trying to convert date which is in string and got format of \"yyyy-MM-dd HH:mm:ss\" to \"dd-MM-yyyy\".

I have implmented following code but its giving : java.

6条回答
  •  醉酒成梦
    2020-12-21 16:30

    First you have to parse the string representation of your date-time into a Date object.

    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = (Date)formatter.parse("2011-11-29 12:34:25");
    

    Then you format the Date object back into a String in your preferred format.

    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    String mydate = dateFormat.format(date);
    

提交回复
热议问题