tl;dr
LocalDate
.parse( "2022-05-12" )
.format(
DateTimeFormatter.ofPattern( "dd-MM-uuuu" )
)
12-05-2022
java.time
Use modern java.time classes. Never use the terrible Date, Calendar, SimpleDateFormat classes.
ISO 8601
Your input conforms to ISO 8601 standard format used by default in the java.time classes for parsing/generating text. So no need to specify a formatting pattern.
LocalDate
Parse your date-only input as a date-only object, a LocalDate.
String input = "2022-05-12" ;
LocalDate ld = LocalDate.parse( input ) ;
To generate text in your desired format, specify a formatting pattern.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) ;
String output = ld.format( f ) ;
Rather than hardcode a particular pattern, I suggest learning to automatically localize using DateTimeFormatter.ofLocalizedDate.
All this has been covered many many times already on Stack Overflow. Always search thoroughly before posting. Search to learn more.
Date, you need to format it again. You can have a look at the implementation of thetoStringmethod. If your are only interested in the days, LocalDate might be a good pick.Datehas only a one format when using itstoStringmethod - that format cannot be changed - a formatter must be used toformatit into a string. Please do not upload images of code/data/errors when asking a question.Daterepresents an instant in time, not a date. It's very unfortunately named. You should be usingjava.time.LocalDate, orjava.time.ZonedDateTime, or similar. None of those represent a format. To 'print' such an object with a specific format, pass it to a formatter which gives you a string.SimpleDateFormatorDate, they are out-of-date, use thejava.time.*APIs instead; 2. Dates (in general) are simply containers for the amount of time which has passed since a given point in time (ie the Unix epoch), they don't, by design, have a concept of "format", thetoStringimplementation is simply there to provide information, this is what formatters are used for, you format the date/time object to aString