How to convert String date to Date?

后端 未结 3 1857
北恋
北恋 2021-01-29 10:02

I have done following code:

String str = \"2013-01-17 11:26\";
DateFormat dateFormat = new SimpleDateFormat(\"yyyy-mm-dd hh:MM\");
Date date = dateFormat.parse(s         


        
相关标签:
3条回答
  • 2021-01-29 10:38

    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();
    }
    
    0 讨论(0)
  • 2021-01-29 10:44

    Just Using of DateFormat Class:

    ateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
    
    0 讨论(0)
  • 2021-01-29 10:57

    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");
    
    0 讨论(0)
提交回复
热议问题