Reputation: 35
I have a string variable, showing date value as ddmmyyyy(04112013).I want to format this string value to yyyy.mm.dd (2013.11.04).which is the best way to do this in java?
Upvotes: 1
Views: 113
Reputation: 2615
You can use SimpleDateFormat#parse() to parse a String in a certain pattern into a Date.
String oldstring = "04112013";
Date date = new SimpleDateFormat("ddMMyyyy").parse(oldstring);
Use SimpleDateFormat#format() to format a Date into a String in a certain pattern.
String newstring = new SimpleDateFormat("yyyy.MM.dd").format(date);
System.out.println(newstring);
I hope it helps.
Upvotes: 3