Format of string in Java

Time:2021-5-8

introduction

The format of string is equivalent to toString () according to the specified format. Generally, there are two forms:

//Returns a formatted string using the specified format string and parameters.
 public static String format(String format, Object... args) {
        return new Formatter().format(format, args).toString();
    }
//Returns a formatted string using the specified locale, format string, and parameters.
public static String format(Locale l, String format, Object... args) {
        return new Formatter(l).format(format, args).toString();
    }

Data conversion operator

data type explain Transformation form
%s String type “string”
%c Character type ‘A’
%b Boolean type true/false
%o Integer type (octal) 111
%d Integer type (decimal) 17
%x Integer type (hexadecimal) 11
%f Floating point type (basic) 66.66
%e Index type 1.11e+5
%a Floating point type (hexadecimal) FF.22
%h Hash code 11
%% Percentage type 17%
%n Line break
%tx Date and time type
test
public class Format01 {
    public static void main(String[] args) {
        System. Out. Println (string. Format ("string% s", "string"));
        System. Out. Println (string. Format ("character% C",'m '));
        System. Out. Println (string. Format ("boolean type% B",'m '>'a'));
        System. Out. Println (string. Format ("octal integer type: O", 17));
        System. Out. Println (string. Format ("decimal integer type% d", 17));
        System. Out. Println (string. Format ("hexadecimal integer type% X", 17));
        System. Out. Println (string. Format ("basic floating point type% F", 99.1));
        System. Out. Println (string. Format ("index type% e", 100. 111111));
        System. Out. Println (string. Format ("hexadecimal floating point type: a", 17.111111));
        System. Out. Println (string. Format ("hash code% H", 17));
        System. Out. Println (string. Format ("percentage type: 17%%));
        System. Out. Print (string. Format ("newline character% n", 17));    
    }

}
output
String: String
Character: M
Boolean type: true
Octal integer type: 21
Decimal integer type: 17
Hexadecimal integer type: 11
Basic floating point type: 99.100000
Index type: 1.001111e + 02
Hexadecimal floating point type: 0x1.11c71c53f39d2p4
Hash code: 11
Percentage type: 17%
Line break:

The format of string

sign explain Examples output
+ Sign a positive number (“positive number:% + F”, 11.11)) Positive number + 11.110000
Align left (“left alignment:% – 5D”, 11) Left aligned: 11
0 Zero before integer (fill in 0% 04D before the number, 11) Add 0:0011 before the number
, Grouping numbers (“by, group numbers:%, D”, 111111) Group numbers by: 111111111
Space Fill in the space before the number (“space:% 4D”, 11) Space: 11
Contains negative numbers (“use brackets to contain negative numbers:% (f), – 11.11) Use parentheses to contain negative numbers: (11.110000)
# Floating point contains decimal, octal contains 0, hexadecimal contains 0
< Format the parameter described by the previous converter (“parameters described before formatting:% < 3.3F after conversion”, 111.1111) Parameters described before formatting: 111.111111 converted 111.111
$ Formatted parameter index (“formatted parameter index: 1 $D,% 2 $s”, 11, “111.1111”) Formatted parameter index: 11111.1111
test
public class formatString {
    public static void main(String[] args) {
        System. Out. Println (string. Format ("positive number:% + F", 11.11));
        System. Out. Println (string. Format ("right alignment:% + 10d", 11));
        System. Out. Println (string. Format ("left alignment:% - 5D", 11));
        System. Out. Println (string. Format ("0: 044d", 11));
        System. Out. Println (string. Format ("space:% 4D", 11));
        System. Out. Println (string. Format (%, D ", 111111111));
        System. Out. Println (string. Format ("use brackets to contain negative numbers:% (F", - 11.11));
        System. Out. Println (string. Format ("floating point contains decimal point:% # F", 11.1));
        System. Out. Println (string. Format ("octal contains 0:% #o", 11));
        System. Out. Println (string. Format ("hexadecimal contains 0x:% #x", 11));
        System. Out. Println (string. Format ("parameters described before format:% < 3.3F", 111.1111 after conversion));
        System. Out. Println (string. Format ("formatted parameter index: 1 $D,% 2 $s", 11, "111.1111111"));
        
    }

}
output
Positive number + 11.110000
Align right + 11
Left aligned: 11   
Add 0 to the front of the number: 000000000000000000000000000000 11
Space: 11
Group numbers by: 111111111
Use parentheses to contain negative numbers: (11.110000)
Floating point number including decimal point: 11.100000
Octal contains 0:013
The hexadecimal system contains the following characters: 0xb
Parameters described before formatting: 111.111111 converted 111.111
Formatted parameter index: 11111.1111

Date formatting

Conversion character explain Examples
c All time date Thursday December 17 13:11:35 CST 2020
F Year month day format 2020-12-17
D Month / day / year format 12/17/20
r HH: mm: SS PM format (12 hours) 01:11:35 PM
T HH: mm: SS format (24 hours) 13:11:35
R HH: mm format (24 hours) 13:11
test
public class formatDate {
    public static void main(String[] args) {
        Date date = new Date();
        System. Out. Println (string. Format ("all time, date):% TC", date));
        System. Out. Println (string. Format ("Year Month Day format: TF", date));
        System. Out. Println (string. Format ("month / day / year format: TD", date));
        System. Out. Println (string. Format ("HH: mm: SS, PM format (12 hour system):% tr", date));
        System. Out. Println (string. Format ("HH: mm: SS format (24-hour system):% TT", date));
        System. Out. Println (string. Format ("HH: mm format (24-hour system):% tr", date));
    }
}
output
Full time date: Thursday December 17 13:21:35 CST 2020
Year month day format: December 17, 2020
Month / day / year format: 12 / 17 / 20
HH: mm: SS PM format (12:00): 01:21:35 PM
HH: mm: SS format (24 hours): 13:21:35
HH: mm format (24 hours): 13:21

The format of time

Conversion character explain Examples
H 2-digit 24-hour hour (0 before less than 2 digits) 13
l 2-digit 12 hour hour 1
k 2-digit 24-hour hour 13
M Two digit minutes 33
L 3-digit millisecond 745
S Two digit seconds 33
N 9-digit milliseconds 745000000
p Local. Us, \ “the morning or afternoon mark of lowercase letters afternoon
Z Time zone abbreviation string CST
z Offset of rfc822 time zone relative to GMT +0800
s The number of seconds from 1970-1 00:00:00 to now 1608183213
Q The number of milliseconds from 1970-1 00:00:00 to now 1608183213745
test
public class formatTime {
    public static void main(String[] args) {
        Date date = new Date();
        System. Out. Println (string. Format ("2-digit 24-hour hour hour (less than 2 digits with 0 in front):% th", date));
        System. Out. Println (string. Format ("2-digit 12 hour hour: TL", date));
        System. Out. Println (string. Format ("2-digit 24-hour hour hour: TK", date));
        System. Out. Println (string. Format ("2-digit minute: TM", date));
        System. Out. Println (string. Format ("3-digit MS% TL", date));
        System. Out. Println (string. Format ("2-digit seconds: ts", date));
        System. Out. Println (string. Format ("9-digit number of milliseconds: TN", date));
        System. Out. Println (string. Format ("time zone abbreviation string: TZ", date));
        System. Out. Println (string. Format ("offset of rfc822 time zone relative to GMT):% TZ", date));
        System. Out. Println (string. Format ("local. Us, \" small letter morning or afternoon mark (English):% TP ", date));
        System. Out. Println (string. Format ("1970-1-1 00:00:00 to the present time: seconds, date));
        System. Out. Println (string. Format ("number of milliseconds from 1970-1-00:00:00 to now):% TQ", date));

    }

}
output
2-digit 24-hour hour system (fill in 0 before less than 2 digits): 13
2-digit 12 hour hour: 1
2-digit 24-hour hour: 13
2-digit minutes: 33
3-digit MS: 745
2-digit seconds: 33
9-digit milliseconds: 745000000
Time zone abbreviation string: CST
Offset of rfc822 time zone relative to GMT + 0800
Local. Us, "the morning or afternoon mark of lowercase letters (English): afternoon
The number of seconds from 1970-1 00:00:00 to now: 1608183213
The number of milliseconds from 1970-1 00:00:00 to now: 1608183213745