Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

The %n and %% Specifiers


The%nand%%format specifiers differ from the others in that they do not match an
argument. Instead, they are simply escape sequences that insert a character into the output
sequence. The%ninserts a newline. The%%inserts a percent sign. Neither of these characters
can be entered directly into the format string. Of course, you can also use the standard
escape sequence\nto embed a newline character.
Here is an example that demonstrates the%nand%%format specifiers:

// Demonstrate the %n and %% format specifiers.
import java.util.*;

class FormatDemo3 {
public static void main(String args[]) {
Formatter fmt = new Formatter();

fmt.format("Copying file%nTransfer is %d%% complete", 88);
System.out.println(fmt);
}
}

It displays the following output:

Copying file
Transfer is 88% complete

532 Part II: The Java Library


Suffix Replaced By
L Millisecond (000 to 999)
m Month as decimal (01 to 13)
M Minute as decimal (00 to 59)
N Nanosecond (000000000 to 999999999)
p Locale’s equivalent ofAMorPMin lowercase
Q Milliseconds from 1/1/1970
r hh:mm:ss(12-hour format)
R hh:mm(24-hour format)
S Seconds (00 to 60)
s Seconds from 1/1/1970 UTC
T hh:mm:ss(24-hour format)
y Year in decimal without centur y (00 to 99)
Y Year in decimal including centur y (0001 to 9999)
z Offset from UTC
Z Time zone name

TABLE 18-13

The Time and Date
Format Suffixes
(continued)
Free download pdf