What does %-8.8s mean when formatting? And can you explain this date formatting %1$tY%1$tm%1$td?

1.5k views Asked by At

I get the '-' is the left align but what is the period('.') in between?

I'm also seeing a lot of #impliedDecimal7SignLastTwoZero. I read that "Implied decimal simply means there is a decimal point implied at a specified location in a field, but not actually present in the file", but I dont get the "7SignLastTwoZero" portion.

1

There are 1 answers

0
Anonymous On BEST ANSWER

%-8.8s

To get a first understanding let’s try it out:

    System.out.format(Locale.ENGLISH, "<%-8.8s>%n", "12345678");
    System.out.format(Locale.ENGLISH, "<%-8.8s>%n", "12345");
    System.out.format(Locale.ENGLISH, "<%-8.8s>%n", "123456789012");

Output is:

<12345678>
<12345   >
<12345678>

As you said %-8s would print output left-justified in 8 positions. Except: if the output is longer than 8 chars, it is printed in as many positions as needed. This is where the extra .8 makes a difference: it truncates the output to at most 8 positions as you see in the third line above. It’s in the documentation:

The format specifiers for general, character, and numeric types have the following syntax:

  %[argument_index$][flags][width][.precision]conversion

The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.

Precision

For general argument types, the precision is the maximum number of characters to be written to the output.

You added:

The requirements are: Unique 8 digit school ID for reporting the student's information. Alphanumeric, Length: 8

In this case I’d warn against %-8.8s. If by some mistake there is a 9 or 10 digit ID, no one will discover because the formatting tacitly truncates to 8 digits. Better to either print all the digits so we can see them, or report an error in some way.

%1$tY%1$tm%1$td

    System.out.format(Locale.ENGLISH, "%1$tY%1$tm%1$td%n",
                        LocalDate.now(ZoneId.of("Europe/Kirov")));

20200129

This format string prints year, month and day of month from argument 1 to the format method. 1$ means the first argument after the format string. t means date and/or time, and Y, m and d the different parts of a date. So the output meant year 2020, January 29. All of this too is documented:

The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.

Conversion  Argument Category  Description
------------------------------------------------------------------------------
't', 'T'    date/time          Prefix for date and time conversion characters.
                               See Date/Time Conversions. 

The following conversion characters are used for formatting dates:

    'Y'   Year, formatted as at least four digits with leading zeros as necessary, e.g. 0092 equals 92 CE for the Gregorian calendar.
    'm'   Month, formatted as two digits with leading zeros as necessary, i.e. 01 - 13.
    'd'   Day of month, formatted as two digits with leading zeros as necessary, i.e. 01 - 31

I have quoted only the 3 relevant entries from the last table, there are many more letters.

Edit: BTW it may be clearer to obtain the latter result in this way:

    System.out.println(LocalDate.now(ZoneId.of("Europe/Kirov"))
            .format(DateTimeFormatter.BASIC_ISO_DATE));

Midnight has passed in Kirov since I first posted this answer, so now the output us:

20200130

Documentation link

Documentation of Java format strings