Java - Date Format converter? [duplicate]

2019-03-07 05:20发布

问题:

This question already has an answer here:

  • java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy 6 answers

Has java any function where I have a String with a date in this form : ddMMyy or dd.MM.yy
and I want to convert it in another format like: dd:MM:yy

回答1:

I would suggest using two SimpleDateFormat objects for this.

Date date = new SimpleDateFormat("ddMMyy").parse( dateString);
String result = new SimpleDateFormat("dd:MM:yy").format( date );

For the format, see the documentation: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html