In the documentation of String.format
there is a link to the format string syntax. And there is said the following:
The format specifiers for types which are used to represent Dates and times have the following syntax:
%[argument_index$][flags][width]conversion
About argument_index
, we’ll talk further down.
flags
and width
are those options for alignment, fill with zeros, number of columns, etc. As you did not use, this is not the case. What matters to this case is conversion
.
Still in the same section about dates and times, it is said that:
The required Conversion is a two Character Sequence. The first Character is’t' or’T'. The Second Character indicates the format to be used.
That is, if you have a "t" or "T", it is because we are formatting a date/time, and therefore the value to be passed should be a long
, Long
, java.util.Calendar
, java.util.Date
or java.time.temporal.TemporalAccessor
(interface implemented by all date and time types of the java.time
).
That’s why when using the format %td
, he makes a mistake when you pass one int
. The t
indicates that the parameter should be one of the types mentioned above (and d
follows the specific table of "Date/Time conversions", which in this case indicates the day of the month).
That is, if you pass one LocalDateTime
(implementing TemporalAccessor
), that would work:
// imprime o dia correspondente à data atual
System.out.println(String.format("%td", LocalDateTime.now()));
For the day is taken from the informed date.
To print more than one field of the same date, it’s kind of boring. So it doesn’t work:
System.out.println(String.format("%td/%tm/%ty %tH:%tM:%tS", LocalDateTime.now())); // exception!
For the %td
corresponds to the LocalDateTime
informed, but %tm
refers to the next argument (which has not been informed). In order for all fields to refer to the same date, you must use the argument_index
, whose syntax is N$
, whereas N
corresponds to the umpteenth date reported. So it should actually be this "beautiful thing":
System.out.println(String.format("%1$td/%1$tm/%1$ty %1$tH:%1$tM:%1$tS", LocalDateTime.now()));
But how you yourself have answered, the best way to format dates and times is to use the date API itself:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss");
System.out.println(formatter.format(LocalDateTime.now()));
About the use of uuuu
instead of yyyy
next year, see here.
Quick game, in the documentation speaks of using
<
in the format string, you know if it would have any use in this case or has nothing to do? When I looked over I thought it was something about repeating the previous parameter, something like that. If you can’t answer, no problem (I know, my laziness, but they say the important thing is not to know, is to have the phone of who knows kkk).– Piovezan
@Piovezan This I never used, but from what I understood, it would also work, because it serves to indicate that it is to use the same argument of the previous - ie,
%1$td/%<tm/%<ty
would be the same as%1$td/%1$tm/%1$ty
– hkotsubo