How to get a Date object from String

前端 未结 5 1322
被撕碎了的回忆
被撕碎了的回忆 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:52

    Need the output in date format and not as string so as to store the output in datetime field in mysql db
    

    After the statement

    Date d = (Date)formatter.parse(dateTime);
    java.sql.Date sqldate = new java.sql.Date(d.getTime())
    

    you have got a java.util.Date object and you can store it as it is in mysql DB (column type : datetime).

    However, when you are printing d, it defaults to the .toString() implementation. I think you expected some output like Date@ but the toString printed in user readable format thats why the confusion.

提交回复
热议问题