how to change format of date from string date

前端 未结 4 1134
天命终不由人
天命终不由人 2021-01-29 09:27

I have date as a string like this

String date = \"11-12-2018\"

I want to change it to \"2018-12-11\"

with the same var

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-29 10:26

    Try code below that will work for your case:

    First parse your input format from string,

    String date = "11-12-2018";
    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    

    Then convert it to desired format,

    Date dateTobeParse = null;
    try {
        dateTobeParse = df.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    if (dateTobeParse != null) {
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
        String outputDate = outFormat.format(dateTobeParse);
    }
    

提交回复
热议问题