Java : An exhaustive list of format strings
Java has a convenient way to generate a string using format strings like "%d" and "%s". This article provides an exhaustive list of format strings with code examples.
Note:
- This article may use translation software for your convenience. Please also check the original Japanese version.
Summary
The format strings are a mechanism for conveniently including numbers, other strings, etc. in one string.
An example of the format string.
final var str = "num = %d : str = %s".formatted(1234, "abc");
System.out.println(str); // "num = 1234 : str = abc"
The format string is the following part :
num = %d : str = %s
- Insert the number 1234 in place of %d
- Insert the string "abc" in place of %s
If you have experience with the C language, you may have benefited from the printf function. A similar syntax can be used in the Java language.
The basis of format strings in Java is the '%' character. '%' is followed by the conversion characters such as 'd' for numbers and 's' for strings.
This article will omit further description of the format string syntax. Please check the above Formatter API specification for details.
Format string syntax
There are several ways to use the format strings in Java.
etc.
This article mainly uses String.formatted added in Java 15. You use String.format for Java 14 or earlier.
// Java 15 or later.
final var str1 = "num = %d : str = %s".formatted(1234, "abc");
System.out.println(str1); // "num = 1234 : str = abc"
// Java 14 or earlier.
final var str2 = String.format("num = %d : str = %s", 1234, "abc");
System.out.println(str2); // "num = 1234 : str = abc"
Argument Index
If a format string has no argument indices, an argument list is converted in the order specified.
final var str = "%s : %s : %s".formatted("a", "b", "c");
System.out.println(str); // "a : b : c"
You can use '$' to specify the argument index. For example, "1$" specifies the first argument, and "2$" specifies the second argument.
final var str1 = "%1$s : %2$s : %3$s".formatted("a", "b", "c");
System.out.println(str1); // "a : b : c"
final var str2 = "%3$s : %2$s : %1$s".formatted("a", "b", "c");
System.out.println(str2); // "c : b : a"
It's OK to use the same index more than once. You can also refer to the previous index with '<'.
final var str1 = "%1$s : %1$s : %1$s".formatted("abc");
System.out.println(str1); // "abc : abc : abc"
final var str2 = "%1$s : %<s : %<s".formatted("abc");
System.out.println(str2); // "abc : abc : abc"
String
Conversion | Option | Description | Code Example |
---|---|---|---|
%s | Produces a string. The result is obtained by the Object.toString method. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
precision | The maximum number of characters in the result. |
|
|
- | Left justifies the result. |
|
|
%S | The upper-case variant of 's'. |
|
Character
Conversion | Option | Description | Code Example |
---|---|---|---|
%c | Produces a character. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
- | Left justifies the result. |
|
|
%C | The upper-case variant of 'c'. |
|
Integer Number
Conversion | Option | Description | Code Example |
---|---|---|---|
%d | Produces a decimal integer. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
0 | The result will be padded by zeros. |
|
|
- | Left justifies the result. |
|
|
+ | Includes a positive sign for all positive numbers. |
|
|
space | Includes a single space for non-negative values. |
|
|
, | Includes digit group separators. |
|
|
( | Prepends a '(' and appends a ')' for negative values. |
|
|
%x | Produces a hexadecimal integer. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
0 | The result will be padded by zeros. |
|
|
- | Left justifies the result. |
|
|
# | Prepends a "0x". |
|
|
%X | The upper-case variant of 'x'. |
|
|
%o | Produces an octal integer. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
0 | The result will be padded by zeros. |
|
|
- | Left justifies the result. |
|
|
# | Prepends a "0". |
|
Real Number
Conversion | Option | Description | Code Example |
---|---|---|---|
%f | Produces a decimal number. If a precision is not specified then the default value is 6. |
|
|
precision | The number of digits after the decimal separator. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
0 | The result will be padded by zeros. |
|
|
- | Left justifies the result. |
|
|
+ | Includes a positive sign for all positive numbers. |
|
|
space | Includes a single space for non-negative values. |
|
|
, | Includes digit group separators. |
|
|
( | Prepends a '(' and appends a ')' for negative values. |
|
|
# | A decimal separator will always be present. |
|
|
%e | Produces a decimal number in computerized scientific notation. If a precision is not specified then the default value is 6. |
|
|
precision | The number of digits after the decimal separator. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
0 | The result will be padded by zeros. |
|
|
- | Left justifies the result. |
|
|
+ | Includes a positive sign for all positive numbers. |
|
|
space | Includes a single space for non-negative values. |
|
|
( | Prepends a '(' and appends a ')' for negative values. |
|
|
# | A decimal separator will always be present. |
|
|
%E | The upper-case variant of 'e'. |
|
|
%g | The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding. |
|
|
precision | The precision is the total number of significant digits in the resulting magnitude after rounding. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
0 | The result will be padded by zeros. |
|
|
- | Left justifies the result. |
|
|
+ | Includes a positive sign for all positive numbers. |
|
|
space | Includes a single space for non-negative values. |
|
|
, | Includes digit group separators. |
|
|
( | Prepends a '(' and appends a ')' for negative values. |
|
|
%G | The upper-case variant of 'g'. |
|
|
%a | Produces a hexadecimal floating-point number with a significand and an exponent. |
|
|
precision | The number of digits after the decimal separator. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
0 | The result will be padded by zeros. |
|
|
- | Left justifies the result. |
|
|
+ | Includes a positive sign for all positive numbers. |
|
|
space | Includes a single space for non-negative values. |
|
|
%A | The upper-case variant of 'a'. |
|
Date/Time
You can use "%t" and "%T" for date/time conversions. 'T' is the upper-case variant of 't'.
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01
final var str = "%tB : %TB".formatted(date, date);
System.out.println(str); // "January : JANUARY"
Please see also : DateTimeFormatter
General
Conversion | Option | Description | Code Example |
---|---|---|---|
%tR | Time formatted for the 24-hour clock as %tH:%tM. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
- | Left justifies the result. |
|
|
%tT | Time formatted for the 24-hour clock as "%tH:%tM:%tS". The options are the same as for %tR. |
|
|
%tr | Time formatted for the 12-hour clock as "%tI:%tM:%tS %Tp". The options are the same as for %tR. |
|
|
%tD | Date formatted as "%tm/%td/%ty". The options are the same as for %tR. |
|
|
%tF | Date formatted as "%tY-%tm-%td". The options are the same as for %tR. |
|
|
%tc | Date and time formatted as "%ta %tb %td %tT %tZ %tY". The options are the same as for %tR. |
|
Date
Conversion | Option | Description | Code Example |
---|---|---|---|
%tB | Locale-specific full month name, e.g. "January", "February". |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
- | Left justifies the result. |
|
|
%tb %th |
Locale-specific abbreviated month name, e.g. "Jan", "Feb". The "%th" is the same as "%tb". |
|
|
%tA | Locale-specific full name of the day of the week, e.g. "Sunday", "Monday". The options are the same as for %tB. |
|
|
%ta | Locale-specific short name of the day of the week, e.g. "Sun", "Mon" The options are the same as for %tB. |
|
|
%tC | Year divided by 100, formatted as at least two digits. The options are the same as for %tB. |
|
|
%tY | Year, formatted as at least four digits. The options are the same as for %tB. |
|
|
%ty | Last two digits of the year, i.e. 00-99. The options are the same as for %tB. |
|
|
%tj | Day of year, formatted as three digits, e.g. 001 - 366. The options are the same as for %tB. |
|
|
%tm | Month, formatted as two digits, i.e. 01 - 13 ("13" is a special value required to support lunar calendars). The options are the same as for %tB. |
|
|
%td | Day of month, formatted as two digits, i.e. 01 - 31. The options are the same as for %tB. |
|
|
%te | Day of month, i.e. 1-31. The options are the same as for %tB. |
|
Time
Conversion | Option | Description | Code Example |
---|---|---|---|
%tH | Hour of the day for the 24-hour clock, formatted as two digits, i.e. 00-23. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
- | Left justifies the result. |
|
|
%tI | Hour for the 12-hour clock, formatted as two digits, i.e. 01 - 12. The options are the same as for %tH. |
|
|
%tk | Hour of the day for the 24-hour clock, i.e. 0 - 23. The options are the same as for %tH. |
|
|
%tl | Hour for the 12-hour clock, i.e. 1 - 12. The options are the same as for %tH. |
|
|
%tM | Minute within the hour formatted as two digits, i.e. 00 - 59. The options are the same as for %tH. |
|
|
%tS | Seconds within the minute, formatted as two digits, i.e. 00 - 60 ("60" is a special value required to support leap seconds). The options are the same as for %tH. |
|
|
%tL | Millisecond within the second formatted as three digits, i.e. 000 - 999. The options are the same as for %tH. |
|
|
%tN | Nanosecond within the second, formatted as nine digits, i.e. 000000000 - 999999999. The options are the same as for %tH. |
|
|
%tp | Locale-specific morning or afternoon marker, e.g. "am" or "pm". The options are the same as for %tH. |
|
|
%tz | Produces a time zone offset, e.g. "-0800". The options are the same as for %tH. |
|
|
%tZ | Produces a time zone, e.g. "PST". The options are the same as for %tH. |
|
|
%ts | Seconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC. The options are the same as for %tH. |
|
|
%tQ | Milliseconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC. The options are the same as for %tH. |
|
Boolean
Conversion | Option | Description | Code Example |
---|---|---|---|
%b | Produces either true or false. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
precision | The maximum number of characters in the result. |
|
|
- | Left justifies the result. |
|
|
%B | The upper-case variant of 'b'. |
|
Hash Code
Conversion | Option | Description | Code Example |
---|---|---|---|
%h | Produces a hash code value of the object. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
precision | The maximum number of characters in the result. |
|
|
- | Left justifies the result. |
|
|
%H | The upper-case variant of 'h'. |
|
Percent(%), Line Separator
Conversion | Option | Description | Code Example |
---|---|---|---|
%% | The result is a literal '%'. |
|
|
width | The minimum number of characters in the result. If the length of the converted value is less than the width then the result will be padded by spaces. |
|
|
- | Left justifies the result. |
|
|
%n | The platform-specific line separator as returned by System.lineSeparator(). |
|
Conclusion
The format strings are a mechanism for conveniently including numbers, other strings, etc. in one string.
Main format strings are :
- %s : String
- %d : Decimal integer
- %f : Decimal number
- %e : Decimal number in computerized scientific notation.
If you use it well, it will improve the readability of your code. Let's make good use of it!