I need to format a date(yyyyMMDD) into YYYY-MM-DD. Can I create a date format of the latter ?
Yes. Use SimpleDateFormat.
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
String sourceDateStr = "20101012";
try {
Date sourceDate = sourceFormat.parse(sourceDateStr);
String targetDateStr = targetFormat.format(sourceDate);
System.out.println(targetDateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Gives the output 2010-01-12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With