How to get a Date object from String

前端 未结 5 1324
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 06:03
DateFormat formatter = new SimpleDateFormat(\"MM/dd/yyyy\' \'HH:mm:ss\");
Date d = (Date)formatter.parse(dateTime);
System.out.println(\"date in controller \"+d);
         


        
5条回答
  •  一整个雨季
    2021-01-13 06:55

    You are using d an object of Date class, so its toString method is called that gives the output as Mon Dec 31 16:04:57 IST 2012.

    If you want to display it in the format that you have specified in your SimpleDateFormat then try using this :

    DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
    Date d = (Date)formatter.parse(dateTime);
    System.out.println("date in controller "+ formatter.format(d));
    

提交回复
热议问题