I have a string in the form MMM/dd/yyyy, ie. May/21/2010. Now I want to convert it to yyyyMMdd, ie. 20100521.
My code is:
public static void main(String[
You will need to apply the locale on SimpleDateFormat.
Here's a more shorter version:-
public String transformPrevDate(String date) {
DateFormat oldFormat = new SimpleDateFormat("MMM/dd/yyyy", Locale.ENGLISH);
DateFormat newFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
String formattedDate = "";
try {
formattedDate = newFormat.format(oldFormat.parse(date));
}
catch (ParseException e) {
e.printStackTrace();
}
return formattedDate;
}
@Test
public void testTransformPrevDate() {
assertEquals("20110113", transformPrevDate("Jan/13/2011"));
assertEquals("20010203", transformPrevDate("Feb/03/2001"));
}