I am new to java and I need a LocalDate variable to obtain it in String format but only the day of the month and the month. Ex:
LocalDate fecha = LocalDate.of(2020, Month.SEPTEMBER, 1);
I need to get 0109
. I have tried to do the following:
System.out.println("Código ->" + fecha.getDayOfMonth()
+ "" + fecha.getMonthValue());
But it gives me 19
, I suppose that it will be necessary to use some format
, thanks in advance.
One way to do it is using
DateTimeFormatter
with the patternddMM
, wheredd
are the days andMM
is the month in number.I leave you an example:
I have to emphasize that you
Month.SEPTEMBER
are equal to the 9 that you place, forgive the redundancy.To use
DateTimeFormatter
you must importjava.time.format.DateTimeFormatter
You can use this to format dates. Where format is the name you want to give it. And inside the parentheses you can change the format structure.
To use the format, you can do it as follows: