Formatting
int i = 2;
double r = Math.sqrt(i);
System.out.format("The square root of %d is %f.%n", i, r);
System.out.printf("%s%n", "hello");
System.err.format("Unable to open file '%s': %s%n", "filename.txt", "there was a problem");
System.err.printf("We got %s... I mean, %d more problem%n", "another", 1);
String s = String.format("%d", 123);
System.out.println(s);
Formatter formatter = new Formatter();
formatter.format("%s", "world");
System.out.println(formatter);
formatter.flush();
formatter.format("%d", 123);
System.out.println(formatter);
formatter.flush();
Conversions
Conversions are divided into the following categories:
- General (
%b %B %h %H %s %S
) - may be applied to any argument type
- Character (
%c %C
) - may be applied to basic types which represent Unicode characters: char
, Character
, byte
, Byte
, short
, and Short
. This conversion may also be applied to the types int
and Integer
when Character.isValidCodePoint(int)
returns true
-
Numeric
- Integral (
%d %o %x %X
) - may be applied to Java integral types: byte, Byte, short, Short, int and Integer, long, Long, and BigInteger (but not char or Character)
- Floating Point (
%e %E %f %g %G %a %A
) - may be applied to Java floating-point types: float, Float, double, Double, and BigDecimal
- Date/Time (
%t %T
) - may be applied to Java types which are capable of encoding a date or time: long, Long, Calendar, Date and TemporalAccessor
- Percent (
%%
) - produces a literal '%' ('\u0025')
- Line Separator (
%n
) - produces the platform-specific line separator
Note: (%B %H %S %C %X %E %G %A %T
) are the same as (%b %h %s %c %x %e %g %a %t
) except that the result is converted to upper case according to the rules of the prevailing Locale
.
General Conversions
Conversion |
Description |
%b %B |
If the argument arg is null , then the result is "false". If arg is a boolean or Boolean , then the result is the string returned by String.valueOf(arg) . Otherwise, the result is "true". |
%h %H |
If the argument arg is null , then the result is "null". Otherwise, the result is obtained by invoking Integer.toHexString(arg.hashCode()) . |
%s %S |
If the argument arg is null , then the result is "null". If arg implements Formattable , then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString() . |
System.out.format("%b%n", (Object) null);
System.out.format("%b%n", false);
System.out.format("%b%n", true);
System.out.format("%b%n", Integer.MIN_VALUE);
System.out.format("%b%n", -1);
System.out.format("%b%n", 0);
System.out.format("%b%n", 1);
System.out.format("%b%n", Integer.MAX_VALUE);
System.out.format("%B%n", (Object) null);
System.out.format("%9B%n", false);
System.out.format("%-9Bending%n", true);
System.out.format("%B%n", Integer.MIN_VALUE);
System.out.format("%B%n", -1);
System.out.format("%B%n", 0);
System.out.format("%B%n", 1);
System.out.format("%B%n", Integer.MAX_VALUE);
System.out.format("%h%n", (Object) null);
System.out.format("%h%n", "hello world");
System.out.format("%H%n", (Object) null);
System.out.format("%H%n", "hello world");
System.out.format("%s%n", "hello");
System.out.format("%S%n", "hello");
Character Conversions
Conversion |
Description |
%c %C |
The result is a Unicode character |
System.out.format("%c%n", null);
System.out.format("%c%n", 'a');
System.out.format("%C%n", 'a');
System.out.format("%c%n", 0x00E0);
System.out.format("%C%n", 0x00E0);
System.out.format("%c%n", '\u00E0');
System.out.format("%C%n", '\u00E0');
System.out.format("%c%n", 0x0450);
System.out.format("%C%n", 0x0450);
System.out.format("%c%n", '\u0450');
System.out.format("%C%n", '\u0450');
System.out.format("%c%c%c%n", '\uae40', '\ubc14', '\uc6b8');
int i = 9812;
int i2 = 1058388;
char c = '♔';
char ci = 9812;
char ci2 = (char) i;
char ci3 = (char) i2;
char ch = 0x2654;
char cu = '\u2654';
String hs = Integer.toHexString(c);
String hs2 = String.format("%h", c);
System.out.format("%d %n", (int) c);
System.out.format("%d %n", (int) ci);
System.out.format("%d %n", (int) ci2);
System.out.format("%d %n", (int) ci3);
System.out.format("%d %n", (int) ch);
System.out.format("%d %n", (int) cu);
System.out.format("%d %n", i);
System.out.format("%c %n", c);
System.out.format("%c %n", ci);
System.out.format("%c %n", ci2);
System.out.format("%c %n", ci3);
System.out.format("%c %n", ch);
System.out.format("%c %n", cu);
System.out.format("%c %n", i);
System.out.format("%h %n", c);
System.out.format("%h %n", ci);
System.out.format("%h %n", ci2);
System.out.format("%h %n", ci3);
System.out.format("%h %n", ch);
System.out.format("%h %n", cu);
System.out.format("%h %n", i);
System.out.format("%x %n", (int) c);
System.out.format("%x %n", (int) ci);
System.out.format("%x %n", (int) ci2);
System.out.format("%x %n", (int) ci3);
System.out.format("%x %n", (int) ch);
System.out.format("%x %n", (int) cu);
System.out.format("%x %n", i);
System.out.format("\\u%h %n", i);
System.out.format("\\u%x %n", i);
System.out.format("%#x %n", i);
System.out.format("%#X %n", i);
char[] dash1 = {8211, 8212};
char[] dash2 = {0x2013, 0x2014};
char[] dash3 = {'\u2013', '\u2014'};
System.out.println(Arrays.toString(dash1));
System.out.println(Arrays.toString(dash2));
System.out.println(Arrays.toString(dash3));
char[] whiteChessPieces1 = {9812, 9813, 9814, 9815, 9816, 9817};
char[] whiteChessPieces2 = {0x2654, 0x2655, 0x2656, 0x2657, 0x2658, 0x2659};
char[] whiteChessPieces3 = {'\u2654', '\u2655', '\u2656', '\u2657', '\u2658', '\u2659'};
System.out.println(Arrays.toString(whiteChessPieces1));
System.out.println(Arrays.toString(whiteChessPieces2));
System.out.println(Arrays.toString(whiteChessPieces3));
char[] blackChessPieces1 = {9818, 9819, 9820, 9821, 9822, 9823};
char[] blackChessPieces2 = {0x265a, 0x265b, 0x265c, 0x265d, 0x265e, 0x265f};
char[] blackChessPieces3 = {'\u265a', '\u265b', '\u265c', '\u265d', '\u265e', '\u265f'};
System.out.println(Arrays.toString(blackChessPieces1));
System.out.println(Arrays.toString(blackChessPieces2));
System.out.println(Arrays.toString(blackChessPieces3));
char[] blackSuits = {9824, 9827, 9829, 9830};
System.out.println(Arrays.toString(blackSuits));
char[] whiteSuits = {9828, 9831, 9825, 9826};
System.out.println(Arrays.toString(whiteSuits));
int[] range = IntStream.rangeClosed(1, 10).toArray();
System.out.println(Arrays.toString(range));
int[] range2 = IntStream.iterate(1, n -> n + 1).limit(10).toArray();
System.out.println(Arrays.toString(range2));
int[] arr = new int[10];
Arrays.setAll(arr, i -> i + 1);
System.out.println(Arrays.toString(arr));
int[] range3 = IntStream.rangeClosed(9812, 9817).toArray();
System.out.println(Arrays.toString(range3));
int[] range4 = IntStream.iterate(9812, n -> n + 1).limit(6).toArray();
System.out.println(Arrays.toString(range4));
List<Integer> list = IntStream.rangeClosed(9812, 9817)
.boxed()
.collect(Collectors.toList());
System.out.println(list);
List<Integer> list2 = IntStream.iterate(9812, n -> n + 1).limit(6)
.boxed()
.collect(Collectors.toList());
System.out.println(list2);
char[] range5 = IntStream.rangeClosed(9812, 9817)
.mapToObj(c -> Character.toString((char) c))
.collect(Collectors.joining())
.toCharArray();
System.out.println(Arrays.toString(range5));
char[] range6 = IntStream.rangeClosed(9812, 9817)
.mapToObj(c -> (char) c)
.map(String::valueOf)
.collect(Collectors.joining())
.toCharArray();
System.out.println(Arrays.toString(range6));
char[] range7 = IntStream.iterate(9812, n -> n + 1).limit(6)
.mapToObj(c -> Character.toString((char) c))
.collect(Collectors.joining())
.toCharArray();
System.out.println(Arrays.toString(range7));
List<Character> list4 = IntStream.rangeClosed(9812, 9817)
.mapToObj(c -> (char)c)
.collect(Collectors.toList());
System.out.println(list4);
List<Character> list3 = IntStream.iterate(9812, n -> n + 1).limit(6)
.mapToObj(c -> (char) c)
.collect(Collectors.toList());
System.out.println(list3);
Numeric Integral Conversions
Conversion |
Description |
%d |
The result is formatted as a decimal integer |
%o |
The result is formatted as an octal integer |
%x %X |
The result is formatted as a hexadecimal integer |
System.out.format("%,d%n", 461012);
System.out.format(Locale.US, "%,d%n", 461012);
System.out.format(Locale.FRANCE, "%,d%n", 461012);
System.out.format(Locale.GERMANY, "%,d%n", 461012);
Numeric Floating Point Conversions
Conversion |
Description |
%e %E |
The result is formatted as a decimal number in computerized scientific notation |
%f |
The result is formatted as a decimal number |
%g %G |
The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding. |
%a %A |
The result is formatted as a hexadecimal floating-point number with a significand and an exponent |
Date/Time Conversions
Conversion |
Description |
%t %T |
Prefix for date and time conversion characters. |
Calendar c = Calendar.getInstance();
System.out.format("%tH hour%n", c);
System.out.format("%tI hour%n", c);
System.out.format("%tk hour%n", c);
System.out.format("%tl hour%n", c);
System.out.format("%tM minutes%n", c);
System.out.format("%tS seconds%n", c);
System.out.format("%tL milliseconds%n", c);
System.out.format("%tN nanoseconds%n", c);
System.out.format("%tp%n", c);
System.out.format("%Tp%n", c);
System.out.format("%tz - RFC 822 style numeric time zone offset from GMT%n", c);
System.out.format("%tZ - time zone abbreviation%n", c);
System.out.format("%ts - seconds since the beginning of the epoch%n", c);
System.out.format("%tQ - milliseconds since the beginning of the epoch%n", c);
System.out.format("%tB%n", c);
System.out.format("%TB%n", c);
System.out.format("%tb%n", c);
System.out.format("%Tb%n", c);
System.out.format("%th%n", c);
System.out.format("%Th%n", c);
System.out.format("%tA%n", c);
System.out.format("%TA%n", c);
System.out.format("%ta%n", c);
System.out.format("%Ta%n", c);
System.out.format("%tC - year (first two digits) (00 - 99)%n", c);
System.out.format("%tY - year (four digits) (0000 - 9999)%n", c);
System.out.format("%ty - year (last two digits) (00 - 99)%n", c);
System.out.format("%tj - day of year (001 - 366)%n", c);
System.out.format("%tm - month (two digits)%n", c);
System.out.format("%td - day of month (01 - 31)%n", c);
System.out.format("%te - day of month (1 - 31)%n", c);
System.out.format("%tR - time formatted HH:MM%n", c);
System.out.format("%tT - time formatted HH:MM:SS%n", c);
System.out.format("%tr - time formatted HH:MM:SS AM/PM%n", c);
System.out.format("%tD - date formatted as MM/DD/YY%n", c);
System.out.format("%tF - date formatted as YYY-MM-DD%n", c);
System.out.format("%tc - dated formatted as e.g. Sun Jul 20 16:17:00 EDT 1969%n", c);
Percent
Conversion |
Description |
%% |
The result is a literal '%' ('\u0025') |
System.out.format("100%%");
Line Separator
Conversion |
Description |
%n |
The result is the platform-specific line separator |
System.out.format("hello%never");
Source