Formatting a String date into a different format in Java

前端 未结 5 811
旧巷少年郎
旧巷少年郎 2020-12-20 05:50

I am trying to format a JSON which has a date key in the format:

date: \"2017-05-23T06:25:50\"

My current attempt i

5条回答
  •  [愿得一人]
    2020-12-20 06:52

    Use this function :

     private String convertDate(String dt) {
    
                //String date="2017-05-23T06:25:50";
    
                try {
                    SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); //date format in which your current string is
                    Date newDate = null;
                    newDate = spf.parse(dt);
                    spf = new SimpleDateFormat("MM/DD/yyyy"); //date format in which you want to convert
                    dt = spf.format(newDate);
                    System.out.println(dt);
    
                    Log.e("FRM_DT", dt);
    
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return dt;
    
            }
    

提交回复
热议问题