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:


Summary

An interpreter for printf-style format strings. This class provides support for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output.

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.
final var str1 = "%s : %s".formatted("abc", "XYZ");
System.out.println(str1); // "abc : XYZ"

final var str2 = "%s : %s".formatted(
        List.of(123, 456), Map.of("abc", 0.789));
System.out.println(str2); // "[123, 456] : {abc=0.789}"
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.
final var str1 = "%5s".formatted("abc");
System.out.println(str1); // "  abc" (two spaces)

final var str2 = "%2s".formatted("abc");
System.out.println(str2); // "abc"
precision The maximum number of characters in the result.
final var str1 = "%.3s".formatted("abcde");
System.out.println(str1); // "abc"

final var str2 = "%5.3s".formatted("abcde");
System.out.println(str2); // "  abc" (two spaces)
- Left justifies the result.
final var str = "%-5s".formatted("abc");
System.out.println(str); // "abc  " (two spaces)
%S The upper-case variant of 's'.
final var str1 = "%S : %S".formatted("abc", "XYZ");
System.out.println(str1); // "ABC : XYZ"

final var str2 = "%5S".formatted("abc");
System.out.println(str2); // "  ABC" (two spaces)

final var str3 = "%-5S".formatted("abc");
System.out.println(str3); // "ABC  " (two spaces)

Character

Conversion Option Description Code Example
%c Produces a character.
final var str1 = "%c : %c".formatted('a', 'X');
System.out.println(str1); // "a : X"

final var cp = Character.toCodePoint('\uD835', '\uDCBD');

final var str2 = "%x : %c".formatted(cp, cp);
System.out.println(str2); // "1d4bd : 𝒽"
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.
final var str = "%3c".formatted('a');
System.out.println(str); // "  a" (two spaces)
- Left justifies the result.
final var str = "%-3c".formatted('a');
System.out.println(str); // "a  " (two spaces)
%C The upper-case variant of 'c'.
final var str1 = "%C : %C".formatted('a', 'X');
System.out.println(str1); // "A : X"

final var str2 = "%3C".formatted('a');
System.out.println(str2); // "  A" (two spaces)

final var str3 = "%-3C".formatted('a');
System.out.println(str3); // "A  " (two spaces)

Integer Number

Conversion Option Description Code Example
%d Produces a decimal integer.
final var str1 = "%d : %d : %d".formatted(0, 123, -456);
System.out.println(str1); // "0 : 123 : -456"

final var str2 = "%d".formatted(0xff);
System.out.println(str2); // "255"

final var str3 = "%d".formatted(new BigInteger("123456789000"));
System.out.println(str3); // "123456789000"
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.
final var str = "%5d".formatted(123);
System.out.println(str); // "  123" (two spaces)
0 The result will be padded by zeros.
final var str = "%05d".formatted(123);
System.out.println(str); // "00123"
- Left justifies the result.
final var str = "%-5d".formatted(123);
System.out.println(str); // "123  " (two spaces)
+ Includes a positive sign for all positive numbers.
final var str = "%+d : %+d".formatted(123, -456);
System.out.println(str); // "+123 : -456"
space Includes a single space for non-negative values.
final var str1 = "% d".formatted(123);
System.out.println(str1); // " 123" (one space)

final var str2 = "% d".formatted(-456);
System.out.println(str2); // "-456"
, Includes digit group separators.
final var str1 = "%d".formatted(123000000);
System.out.println(str1); // "123000000"

final var str2 = "%,d".formatted(123000000);
System.out.println(str2); // "123,000,000"
( Prepends a '(' and appends a ')' for negative values.
final var str1 = "%d : %d".formatted(123, -456);
System.out.println(str1); // "123 : -456"

final var str2 = "%(d : %(d".formatted(123, -456);
System.out.println(str2); // "123 : (456)"
%x Produces a hexadecimal integer.
final var str1 = "%x : %x : %x".formatted(0x12ab, 255, -1);
System.out.println(str1); // "12ab : ff : ffffffff"

final var str2 = "%x".formatted(new BigInteger("305441741"));
System.out.println(str2); // "1234abcd"
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.
final var str = "%5x".formatted(0x123);
System.out.println(str); // "  123" (two spaces)
0 The result will be padded by zeros.
final var str = "%05x".formatted(0x123);
System.out.println(str); // "00123"
- Left justifies the result.
final var str = "%-5x".formatted(0x123);
System.out.println(str); // "123  " (two spaces)
# Prepends a "0x".
final var str = "%#x".formatted(0x123);
System.out.println(str); // "0x123"
%X The upper-case variant of 'x'.
final var str = "%X : %X : %X".formatted(0x12ab, 255, -1);
System.out.println(str); // "12AB : FF : FFFFFFFF"
%o Produces an octal integer.
final var str1 = "%o : %o : %o : %o".formatted(7, 8, 9, 10);
System.out.println(str1); // "7 : 10 : 11 : 12"

final var str2 = "%o : %o".formatted(64, -1);
System.out.println(str2); // "100 : 37777777777"

final var str3 = "%o".formatted(new BigInteger("342391"));
System.out.println(str3); // "1234567"
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.
final var str = "%5o".formatted(83);
System.out.println(str); // "  123" (two spaces)
0 The result will be padded by zeros.
final var str = "%05o".formatted(83);
System.out.println(str); // "00123"
- Left justifies the result.
final var str = "%-5o".formatted(83);
System.out.println(str); // "123  " (two spaces)
# Prepends a "0".
final var str = "%#o".formatted(83);
System.out.println(str); // "0123"

Real Number

Conversion Option Description Code Example
%f Produces a decimal number.
If a precision is not specified then the default value is 6.
final var str1 = "%f : %f : %f".formatted(0.0, 1.23, -456.0);
System.out.println(str1); // "0.000000 : 1.230000 : -456.000000"

final var str2 = "%f : %f".formatted(1.2e-5, 1.234567e+6);
System.out.println(str2); // "0.000012 : 1234567.000000"

final var str3 = "%f".formatted(new BigDecimal("1.25"));
System.out.println(str3); // "1.250000"

final var str4 = "%f".formatted(Double.POSITIVE_INFINITY);
System.out.println(str4); // "Infinity"
precision The number of digits after the decimal separator.
final var str1 = "%.3f".formatted(0.0);
System.out.println(str1); // "0.000"

final var str2 = "%.3f : %.3f".formatted(0.12345, -678.9);
System.out.println(str2); // "0.123 : -678.900"

final var str3 = "%.10f".formatted(0.123456789);
System.out.println(str3); // "0.1234567890"
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.
final var str1 = "%10f".formatted(0.123);
System.out.println(str1); // "  0.123000" (two spaces)

final var str2 = "%10.3f".formatted(0.123);
System.out.println(str2); // "     0.123" (five spaces)
0 The result will be padded by zeros.
final var str1 = "%010f".formatted(0.123);
System.out.println(str1); // "000.123000"

final var str2 = "%010.3f".formatted(0.123);
System.out.println(str2); // "000000.123"
- Left justifies the result.
final var str1 = "%-10f".formatted(0.123);
System.out.println(str1); // "0.123000  " (two spaces)

final var str2 = "%-10.3f".formatted(0.123);
System.out.println(str2); // "0.123     " (five spaces)
+ Includes a positive sign for all positive numbers.
final var str = "%+f : %+f".formatted(123.0, -456.0);
System.out.println(str); // "+123.000000 : -456.000000"
space Includes a single space for non-negative values.
final var str1 = "% f".formatted(123.0);
System.out.println(str1); // " 123.000000" (one space)

final var str2 = "% f".formatted(-456.0);
System.out.println(str2); // "-456.000000"
, Includes digit group separators.
final var str1 = "%f".formatted(123000.0);
System.out.println(str1); // "123000.000000"

final var str2 = "%,f".formatted(123000.0);
System.out.println(str2); // "123,000.000000"
( Prepends a '(' and appends a ')' for negative values.
final var str1 = "%(f".formatted(123.0);
System.out.println(str1); // "123.000000"

final var str2 = "%(f".formatted(-456.0);
System.out.println(str2); // "(456.000000)"
# A decimal separator will always be present.
final var str1 = "%.0f".formatted(123.0);
System.out.println(str1); // "123"

final var str2 = "%#.0f".formatted(123.0);
System.out.println(str2); // "123."
%e Produces a decimal number in computerized scientific notation.
If a precision is not specified then the default value is 6.
final var str1 = "%e".formatted(0.0);
System.out.println(str1); // "0.000000e+00"

final var str2 = "%e : %e".formatted(0.123456789, -123456789.0);
System.out.println(str2); // "1.234568e-01 : -1.234568e+08"

final var str3 = "%e : %e".formatted(1.23e-3, 0x1.0p+6);
System.out.println(str3); // "1.230000e-03 : 6.400000e+01"

final var str4 = "%e".formatted(new BigDecimal("1.25"));
System.out.println(str4); // "1.250000e+00"

final var str5 = "%e".formatted(Double.POSITIVE_INFINITY);
System.out.println(str5); // "Infinity"
precision The number of digits after the decimal separator.
final var str1 = "%.3e".formatted(0.0);
System.out.println(str1); // "0.000e+00"

final var str2 = "%.3e : %.3e".formatted(0.123, -123456789.0);
System.out.println(str2); // "1.230e-01 : -1.235e+08"
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.
final var str1 = "%14e".formatted(0.123);
System.out.println(str1); // "  1.230000e-01" (two spaces)

final var str2 = "%14.3e".formatted(0.123);
System.out.println(str2); // "     1.230e-01" (five spaces)
0 The result will be padded by zeros.
final var str1 = "%014e".formatted(0.123);
System.out.println(str1); // "001.230000e-01"

final var str2 = "%014.3e".formatted(0.123);
System.out.println(str2); // "000001.230e-01"
- Left justifies the result.
final var str1 = "%-14e".formatted(0.123);
System.out.println(str1); // "1.230000e-01  " (two spaces)

final var str2 = "%-14.3e".formatted(0.123);
System.out.println(str2); // "1.230e-01     " (five spaces)
+ Includes a positive sign for all positive numbers.
final var str = "%+e : %+e".formatted(123.0, -456.0);
System.out.println(str); // "+1.230000e+02 : -4.560000e+02"
space Includes a single space for non-negative values.
final var str1 = "% e".formatted(123.0);
System.out.println(str1); // " 1.230000e+02" (one space)

final var str2 = "% e".formatted(-456.0);
System.out.println(str2); // "-4.560000e+02"
( Prepends a '(' and appends a ')' for negative values.
final var str1 = "%(e".formatted(123.0);
System.out.println(str1); // "1.230000e+02"

final var str2 = "%(e".formatted(-456.0);
System.out.println(str2); // "(4.560000e+02)"
# A decimal separator will always be present.
final var str1 = "%.0e".formatted(0.0);
System.out.println(str1); // "0e+00"

final var str2 = "%#.0e".formatted(0.0);
System.out.println(str2); // "0.e+00"
%E The upper-case variant of 'e'.
final var str = "%E : %E".formatted(0.123, 0.456);
System.out.println(str); // "1.230000E-01 : 4.560000E-01"
%g The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding.
final var str1 = "%g : %g : %g".formatted(0.0, 1.23, -456.0);
System.out.println(str1); // "0.00000 : 1.23000 : -456.000"

final var str2 = "%g : %g".formatted(0.000123456, 0.0000123456);
System.out.println(str2); // "0.000123456 : 1.23456e-05"

final var str3 = "%g : %g".formatted(123456.0, 1234567.0);
System.out.println(str3); // "123456 : 1.23457e+06"

final var str4 = "%g".formatted(new BigDecimal("1.25"));
System.out.println(str4); // "1.25000"

final var str5 = "%g".formatted(Double.POSITIVE_INFINITY);
System.out.println(str5); // "Infinity"
precision The precision is the total number of significant digits in the resulting magnitude after rounding.
final var str1 = "%.3g".formatted(0.0);
System.out.println(str1); // "0.00"

final var str2 = "%.3g : %.3g".formatted(1.23, -456.0);
System.out.println(str2); // "1.23 : -456"

final var str3 = "%.3g".formatted(1.23456e+6);
System.out.println(str3); // "1.23e+06"
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.
final var str1 = "%9g".formatted(1.23);
System.out.println(str1); // "  1.23000" (two spaces)

final var str2 = "%9.3g".formatted(1.23);
System.out.println(str2); // "     1.23" (five spaces)

final var str3 = "%13g".formatted(1.23456e+6);
System.out.println(str3); // "  1.23456e+06" (two spaces)

final var str4 = "%13.3g".formatted(1.23456e+6);
System.out.println(str4); // "     1.23e+06" (five spaces)
0 The result will be padded by zeros.
final var str1 = "%09g".formatted(1.23);
System.out.println(str1); // "001.23000"

final var str2 = "%09.3g".formatted(1.23);
System.out.println(str2); // "000001.23"

final var str3 = "%013g".formatted(1.23456e+6);
System.out.println(str3); // "001.23456e+06"

final var str4 = "%013.3g".formatted(1.23456e+6);
System.out.println(str4); // "000001.23e+06"
- Left justifies the result.
final var str1 = "%-9g".formatted(1.23);
System.out.println(str1); // "1.23000  " (two spaces)

final var str2 = "%-9.3g".formatted(1.23);
System.out.println(str2); // "1.23     " (five spaces)
+ Includes a positive sign for all positive numbers.
final var str1 = "%+g : %+g".formatted(123.0, -456.0);
System.out.println(str1); // "+123.000 : -456.000"
space Includes a single space for non-negative values.
final var str1 = "% g".formatted(123.0);
System.out.println(str1); // " 123.000" (one space)

final var str2 = "% g".formatted(-456.0);
System.out.println(str2); // "-456.000"
, Includes digit group separators.
final var str1 = "%g".formatted(123000.0);
System.out.println(str1); // "123000"

final var str2 = "%,g".formatted(123000.0);
System.out.println(str2); // "123,000"
( Prepends a '(' and appends a ')' for negative values.
final var str1 = "%(g".formatted(123.0);
System.out.println(str1); // "123.000"

final var str2 = "%(g".formatted(-456.0);
System.out.println(str2); // "(456.000)"
%G The upper-case variant of 'g'.
final var str = "%G : %G".formatted(1.23, 1.23456e+6);
System.out.println(str); // "1.23000 : 1.23456E+06"
%a Produces a hexadecimal floating-point number with a significand and an exponent.
final var str1 = "%a : %a".formatted(0.0, -0x1.23p1);
System.out.println(str1); // "0x0.0p0 : -0x1.23p1"

final var str2 = "%a : %a : %a".formatted(2.0, 4.0, 8.0);
System.out.println(str2); // "0x1.0p1 : 0x1.0p2 : 0x1.0p3"

final var str3 = "%a".formatted(Double.POSITIVE_INFINITY);
System.out.println(str3); // "Infinity"
precision The number of digits after the decimal separator.
final var str1 = "%a : %.3a".formatted(0x1.2p1, 0x1.2p1);
System.out.println(str1); // "0x1.2p1 : 0x1.200p1"

final var str2 = "%a : %.3a".formatted(0x1.23456p1, 0x1.23456p1);
System.out.println(str2); // "0x1.23456p1 : 0x1.234p1"
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.
final var str1 = "%12a".formatted(0x1.2345p1);
System.out.println(str1); // "  0x1.2345p1" (two spaces)

final var str2 = "%12.2a".formatted(0x1.2345p1);
System.out.println(str2); // "    0x1.23p1" (four spaces)
0 The result will be padded by zeros.
final var str1 = "%012a".formatted(0x1.2345p1);
System.out.println(str1); // "0x001.2345p1"

final var str2 = "%012.2a".formatted(0x1.2345p1);
System.out.println(str2); // "0x00001.23p1"
- Left justifies the result.
final var str1 = "%-12a".formatted(0x1.2345p1);
System.out.println(str1); // "0x1.2345p1  " (two spaces)

final var str2 = "%-12.2a".formatted(0x1.2345p1);
System.out.println(str2); // "0x1.23p1    " (four spaces)
+ Includes a positive sign for all positive numbers.
final var str = "%+a : %+a".formatted(2.0, -4.0);
System.out.println(str); // "+0x1.0p1 : -0x1.0p2"
space Includes a single space for non-negative values.
final var str1 = "% a".formatted(2.0);
System.out.println(str1); // " 0x1.0p1" (one space)

final var str2 = "% a".formatted(-4.0);
System.out.println(str2); // "-0x1.0p2"
%A The upper-case variant of 'a'.
final var str = "%A : %A".formatted(0.0, -0x1.23p1);
System.out.println(str); // "0X0.0P0 : -0X1.23P1"

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.
final var time1 = LocalTime.of(6, 15);
System.out.println(time1); // 06:15

final var str1 = "%tR".formatted(time1);
System.out.println(str1); // "06:15"

final var time2 = LocalTime.of(14, 30);
final var str2 = "%tR".formatted(time2);
System.out.println(str2); // "14:30"

final var time3 = LocalTime.of(0, 0);
final var str3 = "%tR".formatted(time3);
System.out.println(str3); // "00:00"
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.
final var str = "%7tR".formatted(LocalTime.of(14, 30));
System.out.println(str); // "  14:30"  (two spaces)
- Left justifies the result.
final var str = "%-7tR".formatted(LocalTime.of(14, 30));
System.out.println(str); // "14:30  "  (two spaces)
%tT Time formatted for the 24-hour clock as "%tH:%tM:%tS".
The options are the same as for %tR.
final var time1 = LocalTime.of(6, 15, 30);
System.out.println(time1); // 06:15:30

final var str1 = "%tT".formatted(time1);
System.out.println(str1); // "06:15:30"

final var time2 = LocalTime.of(14, 30, 59);
final var str2 = "%tT".formatted(time2);
System.out.println(str2); // "14:30:59"

final var time3 = LocalTime.of(0, 0, 0);
final var str3 = "%tT".formatted(time3);
System.out.println(str3); // "00:00:00"
%tr Time formatted for the 12-hour clock as "%tI:%tM:%tS %Tp".
The options are the same as for %tR.
final var time1 = LocalTime.of(6, 15, 30);
System.out.println(time1); // 06:15:30

final var str1 = "%tr".formatted(time1);
System.out.println(str1); // "06:15:30 AM"

final var time2 = LocalTime.of(14, 30, 59);
final var str2 = "%tr".formatted(time2);
System.out.println(str2); // "02:30:59 PM"

final var time3 = LocalTime.of(0, 0, 0);
final var str3 = "%tr".formatted(time3);
System.out.println(str3); // "12:00:00 AM"
%tD Date formatted as "%tm/%td/%ty".
The options are the same as for %tR.
final var date = LocalDate.of(2030, 1, 15);
System.out.println(date); // 2030-01-15

final var str = "%tD".formatted(date);
System.out.println(str); // "01/15/30"
%tF Date formatted as "%tY-%tm-%td".
The options are the same as for %tR.
final var date = LocalDate.of(2030, 1, 15);
System.out.println(date); // 2030-01-15

final var str = "%tF".formatted(date);
System.out.println(str); // "2030-01-15"
%tc Date and time formatted as "%ta %tb %td %tT %tZ %tY".
The options are the same as for %tR.
final var zoneId = ZoneId.systemDefault();
System.out.println(zoneId); // America/Los_Angeles

final var dateTime = ZonedDateTime.of(
        LocalDate.of(2030, 1, 15),
        LocalTime.of(12, 30, 45),
        zoneId);
// 2030-01-15T12:30:45-08:00[America/Los_Angeles]
System.out.println(dateTime);

final var str = "%tc".formatted(dateTime);
System.out.println(str); // "Tue Jan 15 12:30:45 PST 2030"

Date

Conversion Option Description Code Example
%tB Locale-specific full month name, e.g. "January", "February".
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"
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.
final var str = "%10tB".formatted(LocalDate.of(2100, 12, 1));
System.out.println(str); // "  December" (two spaces)
- Left justifies the result.
final var str = "%-10tB".formatted(LocalDate.of(2100, 12, 1));
System.out.println(str); // "December  " (two spaces)
%tb
%th
Locale-specific abbreviated month name, e.g. "Jan", "Feb".
The "%th" is the same as "%tb".
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); // "Jan : JAN"
%tA Locale-specific full name of the day of the week, e.g. "Sunday", "Monday".
The options are the same as for %tB.
System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01
System.out.println(date.getDayOfWeek()); // FRIDAY

final var str = "%tA : %TA".formatted(date, date);
System.out.println(str); // "Friday : FRIDAY"
%ta Locale-specific short name of the day of the week, e.g. "Sun", "Mon"
The options are the same as for %tB.
System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01
System.out.println(date.getDayOfWeek()); // FRIDAY

final var str = "%ta : %Ta".formatted(date, date);
System.out.println(str); // "Fri : FRI"
%tC Year divided by 100, formatted as at least two digits.
The options are the same as for %tB.
final var date1 = LocalDate.of(900, 1, 1);
System.out.println(date1); // 0900-01-01

final var date2 = LocalDate.of(2150, 1, 1);
System.out.println(date2); // 2150-01-01

final var date3 = LocalDate.of(10000, 1, 1);
System.out.println(date3); // +10000-01-01

final var str = "%tC : %tC : %tC".formatted(date1, date2, date3);
System.out.println(str); // "09 : 21 : 100"
%tY Year, formatted as at least four digits.
The options are the same as for %tB.
final var date1 = LocalDate.of(90, 1, 1);
System.out.println(date1); // 0090-01-01

final var date2 = LocalDate.of(2100, 1, 1);
System.out.println(date2); // 2100-01-01

final var date3 = LocalDate.of(10000, 1, 1);
System.out.println(date3); // +10000-01-01

final var str = "%tY : %tY : %tY".formatted(date1, date2, date3);
System.out.println(str); // "0090 : 2100 : 10000"
%ty Last two digits of the year, i.e. 00-99.
The options are the same as for %tB.
final var date1 = LocalDate.of(1999, 1, 1);
System.out.println(date1); // 1999-01-01

final var date2 = LocalDate.of(2000, 1, 1);
System.out.println(date2); // 2000-01-01

final var date3 = LocalDate.of(2150, 1, 1);
System.out.println(date3); // 2150-01-01

final var str = "%ty : %ty : %ty".formatted(date1, date2, date3);
System.out.println(str); // "99 : 00 : 50"
%tj Day of year, formatted as three digits, e.g. 001 - 366.
The options are the same as for %tB.
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01

final var date2 = LocalDate.of(2100, 1, 31);
System.out.println(date2); // 2100-01-31

final var date3 = LocalDate.of(2100, 2, 1);
System.out.println(date3); // 2100-02-01

final var date4 = LocalDate.of(2100, 12, 31);
System.out.println(date4); // 2100-12-31

final var str = "%tj : %tj : %tj : %tj"
        .formatted(date1, date2, date3, date4);
System.out.println(str); // "001 : 031 : 032 : 365"
%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.
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01

final var date2 = LocalDate.of(2100, 3, 31);
System.out.println(date2); // 2100-03-31

final var date3 = LocalDate.of(2100, 12, 1);
System.out.println(date3); // 2100-12-01

final var str = "%tm : %tm : %tm".formatted(date1, date2, date3);
System.out.println(str); // "01 : 03 : 12"
%td Day of month, formatted as two digits, i.e. 01 - 31.
The options are the same as for %tB.
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01

final var date2 = LocalDate.of(2100, 1, 31);
System.out.println(date2); // 2100-01-31

final var date3 = LocalDate.of(2100, 2, 10);
System.out.println(date3); // 2100-02-10

final var str = "%td : %td : %td".formatted(date1, date2, date3);
System.out.println(str); // "01 : 31 : 10"
%te Day of month, i.e. 1-31.
The options are the same as for %tB.
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01

final var date2 = LocalDate.of(2100, 1, 31);
System.out.println(date2); // 2100-01-31

final var date3 = LocalDate.of(2100, 2, 10);
System.out.println(date3); // 2100-02-10

final var str = "%te : %te : %te".formatted(date1, date2, date3);
System.out.println(str); // "1 : 31 : 10"

Time

Conversion Option Description Code Example
%tH Hour of the day for the 24-hour clock, formatted as two digits, i.e. 00-23.
final var time1 = LocalTime.of(6, 15);
System.out.println(time1); // 06:15

final var str1 = "%tH : %tM".formatted(time1, time1);
System.out.println(str1); // "06 : 15"

final var time2 = LocalTime.of(14, 30);
final var str2 = "%tH : %tM".formatted(time2, time2);
System.out.println(str2); // "14 : 30"

final var time3 = LocalTime.of(0, 0);
final var str3 = "%tH : %tM".formatted(time3, time3);
System.out.println(str3); // "00 : 00"
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.
final var str = "%4tH".formatted(LocalTime.of(14, 30));
System.out.println(str); // "  14"  (two spaces)
- Left justifies the result.
final var str = "%-4tH".formatted(LocalTime.of(14, 30));
System.out.println(str); // "14  "  (two spaces)
%tI Hour for the 12-hour clock, formatted as two digits, i.e. 01 - 12.
The options are the same as for %tH.
final var time1 = LocalTime.of(6, 15);
System.out.println(time1); // 06:15

final var str1 = "%tI : %tM".formatted(time1, time1);
System.out.println(str1); // "06 : 15"

final var time2 = LocalTime.of(14, 30);
final var str2 = "%tI : %tM".formatted(time2, time2);
System.out.println(str2); // "02 : 30"

final var time3 = LocalTime.of(0, 0);
final var str3 = "%tI : %tM".formatted(time3, time3);
System.out.println(str3); // "12 : 00"
%tk Hour of the day for the 24-hour clock, i.e. 0 - 23.
The options are the same as for %tH.
final var time1 = LocalTime.of(6, 15);
System.out.println(time1); // 06:15

final var str1 = "%tk : %tM".formatted(time1, time1);
System.out.println(str1); // "6 : 15"

final var time2 = LocalTime.of(14, 30);
final var str2 = "%tk : %tM".formatted(time2, time2);
System.out.println(str2); // "14 : 30"

final var time3 = LocalTime.of(0, 0);
final var str3 = "%tk : %tM".formatted(time3, time3);
System.out.println(str3); // "0 : 00"
%tl Hour for the 12-hour clock, i.e. 1 - 12.
The options are the same as for %tH.
final var time1 = LocalTime.of(6, 15);
System.out.println(time1); // 06:15

final var str1 = "%tl : %tM".formatted(time1, time1);
System.out.println(str1); // "6 : 15"

final var time2 = LocalTime.of(14, 30);
final var str2 = "%tl : %tM".formatted(time2, time2);
System.out.println(str2); // "2 : 30"

final var time3 = LocalTime.of(0, 0);
final var str3 = "%tl : %tM".formatted(time3, time3);
System.out.println(str3); // "12 : 00"
%tM Minute within the hour formatted as two digits, i.e. 00 - 59.
The options are the same as for %tH.
final var time1 = LocalTime.of(12, 30);
System.out.println(time1); // 12:30

final var str1 = "%tH : %tM".formatted(time1, time1);
System.out.println(str1); // "12 : 30"

final var time2 = LocalTime.of(0, 0);
final var str2 = "%tH : %tM".formatted(time2, time2);
System.out.println(str2); // "00 : 00"
%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.
final var time1 = LocalTime.of(12, 30, 45);
System.out.println(time1); // 12:30:45

final var str1 = "%tH : %tM : %tS".formatted(time1, time1, time1);
System.out.println(str1); // "12 : 30 : 45"

final var time2 = LocalTime.of(0, 0, 0);
final var str2 = "%tH : %tM : %tS".formatted(time2, time2, time2);
System.out.println(str2); // "00 : 00 : 00"
%tL Millisecond within the second formatted as three digits, i.e. 000 - 999.
The options are the same as for %tH.
final var time = LocalTime.of(0, 0, 0, 123000000);
System.out.println(time); // 00:00:00.123

final var str = "%tL".formatted(time);
System.out.println(str); // "123"
%tN Nanosecond within the second, formatted as nine digits, i.e. 000000000 - 999999999.
The options are the same as for %tH.
final var time = LocalTime.of(0, 0, 0, 123456789);
System.out.println(time); // 00:00:00.123456789

final var str = "%tN".formatted(time);
System.out.println(str); // "123456789"
%tp Locale-specific morning or afternoon marker, e.g. "am" or "pm".
The options are the same as for %tH.
System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var time1 = LocalTime.of(10, 0);
System.out.println(time1); // 10:00

final var str1 = "%tp : %Tp".formatted(time1, time1);
System.out.println(str1); // "am : AM"

final var time2 = LocalTime.of(15, 0);
System.out.println(time2); // 15:00

final var str2 = "%tp : %Tp".formatted(time2, time2);
System.out.println(str2); // "pm : PM"
%tz Produces a time zone offset, e.g. "-0800".
The options are the same as for %tH.
final var zoneId = ZoneId.systemDefault();
System.out.println(zoneId); // America/Los_Angeles

final var dateTime = ZonedDateTime.of(
        LocalDate.of(2100, 1, 1),
        LocalTime.of(12, 0),
        zoneId);
// 2100-01-01T12:00-08:00[America/Los_Angeles]
System.out.println(dateTime);

final var str = "%tz".formatted(dateTime);
System.out.println(str); // "-0800"
%tZ Produces a time zone, e.g. "PST".
The options are the same as for %tH.
final var zoneId = ZoneId.systemDefault();
System.out.println(zoneId); // America/Los_Angeles

final var dateTime = ZonedDateTime.of(
        LocalDate.of(2100, 1, 1),
        LocalTime.of(12, 0),
        zoneId);
// 2100-01-01T12:00-08:00[America/Los_Angeles]
System.out.println(dateTime);

final var str = "%tZ".formatted(dateTime);
System.out.println(str); // "PST"
%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.
final var instant = Instant.ofEpochMilli(946684800000L);
System.out.println(instant); // 2000-01-01T00:00:00Z

final var str = "%ts".formatted(instant);
System.out.println(str); // "946684800"
%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.
final var instant = Instant.ofEpochMilli(946684800000L);
System.out.println(instant); // 2000-01-01T00:00:00Z

final var str = "%tQ".formatted(instant);
System.out.println(str); // "946684800000"

Boolean

Conversion Option Description Code Example
%b Produces either true or false.
final var str = "%b : %b : %b".formatted(true, false, null);
System.out.println(str); // "true : false : 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.
final var str = "%6b".formatted(true);
System.out.println(str); // "  true" (two spaces)
precision The maximum number of characters in the result.
final var str = "%.3b : %.3b".formatted(true, false);
System.out.println(str); // "tru : fal"
- Left justifies the result.
final var str = "%-6b".formatted(true);
System.out.println(str); // "true  " (two spaces)
%B The upper-case variant of 'b'.
final var str = "%B : %B : %B".formatted(true, false, null);
System.out.println(str); // "TRUE : FALSE : FALSE"

Hash Code

Conversion Option Description Code Example
%h Produces a hash code value of the object.
final var str = "%h : %h".formatted("xyz", 1234);
System.out.println(str); // "1d199 : 4d2"
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.
final var str = "%7h".formatted("xyz");
System.out.println(str); // "  1d199" (two spaces)
precision The maximum number of characters in the result.
final var str1 = "%h".formatted("xyz");
System.out.println(str1); // "1d199"

final var str2 = "%.3h".formatted("xyz");
System.out.println(str2); // "1d1"
- Left justifies the result.
final var str = "%-7h".formatted("xyz");
System.out.println(str); // "1d199  " (two spaces)
%H The upper-case variant of 'h'.
final var str = "%H : %H".formatted("xyz", 1234);
System.out.println(str); // "1D199 : 4D2"

Percent(%), Line Separator

Conversion Option Description Code Example
%% The result is a literal '%'.
final var str = "%d%%".formatted(75);
System.out.println(str); // "75%"
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.
final var str = "%d%3%".formatted(75);
System.out.println(str); // "75  %"  (two spaces)
- Left justifies the result.
final var str = "%d%-3%".formatted(75);
System.out.println(str); // "75%  "  (two spaces)
%n The platform-specific line separator as returned by
System.lineSeparator().
final var str = "%s%n%d".formatted("abc", 123);
System.out.println(str);

// Result
// ↓
//abc
//123

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!


Related posts

To top of page