How to convert String date to Date?

走远了吗. 提交于 2019-12-02 14:15:18

You are using the wrong format. MM is for month, and mm is for minutes, and for hours use HH. See SimpleDateFormat for various other formats.

Change your format to: -

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

The following code should work well:

try{
String str = "2013-01-17 11:26";
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm");
Date date = (Date)dateFormat.parse(str);
System.out.println("str :"+str);
System.out.println("Date :"+date);
}
catch(Exception e){
   e.getStackTrace();
}

Just Using of DateFormat Class:

ateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!