How to convert String to Date in java [duplicate]

2019-03-07 09:36发布

问题:

This question already has an answer here:

  • How can I change the date format in Java? 10 answers

I have a string something like date month year(31 08 2012) . I want to convert it to date format.

String string = "31082012";
Date date = new SimpleDateFormat("DDMMYYYY").parse(string);
System.out.println(date);

回答1:

Firstly, Your format is wrong, it should be ddMMyyyy

Date date = new SimpleDateFormat("ddMMyyyy").parse(string);