tl;dr
LocalDateTime.parse(
"2011-11-29 12:34:25".replace( " " , "T " )
).format (
DateTimeFormatter.ofPattern( "dd-MM-uuuu" )
)
29-11-2011
Using java.time
The modern approach uses the java.time classes rather than the troublesome legacy classes.
ISO 8601
Your input string of “2011-11-29 12:34:25” has a format nearly that of the ISO 8601 standard. To fully comply, replace the SPACE in the middle with a T
.
String input = "2011-11-29 12:34:25".replace( " " , "T " );
Without any indication of time zone or offset-from-UTC, we parse as a LocalDateTime
.
LocalDateTime ldt = LocalDateTime.parse( input ) ;
You want the date-only value, so extract a LocalDate
. The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate ld = ldt.toLocalDate() ;
To generate a string in your desired format, you specify a custom formatting pattern.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" );
String output = ld.format( f );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.