Java Formatter specifiers

Introduction

The Java Formatter's format() method accepts a wide variety of format specifiers.

Many specifiers have both upper- and lowercase forms.

When an uppercase specifier is used, then letters are shown in uppercase.

Otherwise, the upper- and lowercase specifiers do the same conversion.

Format Specifier
Conversion Applied
%a
%A
Floating-point hexadecimal

%b
%B
Boolean

%c
Character
%d
Decimal integer
%h
%H
Hash code of the argument

%e
%E
Scientific notation

%f
Decimal floating-point
%g
%G
Uses %e or %f, based on the value being formatted
and the precision
%o
Octal integer
%n
Inserts a newline character
%s
%S
String

%t
%T
Time and date

%x
%X
Integer hexadecimal

%%
Inserts a % sign

Java type-checks each format specifier against its corresponding argument.

If the argument doesn't match, an IllegalFormatException is thrown.

To get the formatted string, call toString().

String str = fmt.toString(); 

To get a reference to the underlying output buffer, call out().

It returns a reference to an Appendable object.




PreviousNext

Related