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

前端 未结 6 843
死守一世寂寞
死守一世寂寞 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:32

    try this way

    String mydate = "2011-11-29 12:34:25"
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date date = null;
    
    try{
       date = sdf.parse(mydate);
       SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
       String mydate = dateFormat.format(date);
    }catch(Exception ex){
       // handle exception
    }
    

提交回复
热议问题