TL; dr
LocalDate.parse( "23/01/2017" , DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK ) ).format( DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK ) )
2017/01/23
Avoid obsolete time classes
Christopher Parker's answer is correct but outdated. Inconvenient old time classes, such as java.util.Date , java.util.Calendar and java.text.SimpleTextFormat , are now deprecated , being superseded by java.time classes.
Using java.time
Parse the input string as a date object, then generate a new String object in the desired format.
The LocalDate class represents a date value only without time and without a time zone.
DateTimeFormatter fIn = DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK ); // As a habit, specify the desired/expected locale, though in this case the locale is irrelevant. LocalDate ld = LocalDate.parse( "23/01/2017" , fIn );
Define a different formatter for output.
DateTimeFormatter fOut = DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK ); String output = ld.format( fOut );
2017/01/23
By the way, consider using standard ISO 8601 formats for strings representing date and time values.
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old obsolete time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , advises switching to the java.time classes.
To learn more, check out the Oracle tutorial . And search for qaru for many examples and explanations. The specification is JSR 310 .
Where can I get java.time classes?
- Java SE 8 , Java SE 9 and later
- Built in.
- Part of the standard Java API with integrated implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the functionality of java.time is included back in Java 6 and 7 in ThreeTen-Backport .
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) specifically for Android.
- See How to use ThreeTenABP ....
The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and others .
Joda time
UPDATE: The Joda-Time project is now in maintenance mode , and the team advises switching to the java.time classes. This section is left here for the sake of history.
For fun, here is his code adapted to use the Joda-Time library.
Reset Console ...
System.out.println( "localDate: " + localDate ); System.out.println( "newDateString: " + newDateString );
At startup ...
localDate: 2010-08-12 newDateString: 2010/08/12