Changing String date format

The first thing you need to do is parse the original value to a Date object

String startTime = "08/11/2008 00:00";
// This could be MM/dd/yyyy, you original value is ambiguous 
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date dateValue = input.parse(startTime);

Once you have that done, you can format the dateValue any way you want…

SimpleDateFormat output = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println("" + output.format(dateValue) + " real date " + startTime);

which outputs:

2008/11/08 00:00 real date 08/11/2008 00:00

The reason you’re getting 0014/05/01 00:00 is SimpleDateFormat (when using yyyy/MM/dd HH:mm) is using 08 for the year, 11 for the month and 2008 for the day, it’s doing an internal rolling of the values to correct the values to a valid date

Leave a Comment