Original address: 7962171
Formatting of regular types
The format () method of the string class is used to create a formatted string and to concatenate multiple string objects. Students familiar with C language should remember the C language of the sprintf () method, the two have similarities. There are two overloaded forms of the format () method.
Format (string format, Object ... args) a new string that uses the local locale to develop a string format and parameters to generate a new formatted string.
Format (locale locale, string format, Object ... args) uses the specified locale to develop string formatting and parameters to generate a formatted string.
Displays different conversion characters that implement different data types to string conversions.
Conversion character |
Description |
Example |
%s |
String type |
"Mingrisoft" |
%c |
Character type |
' m ' |
%b |
Boolean type |
True |
%d |
Integer type (decimal) |
99 |
%x |
Integer type (hex) |
Ff |
%o |
Integer type (octal) |
77 |
%f |
Floating-point types |
99.99 |
%a |
Hexadecimal floating-point type |
Ff.35ae |
%e |
Index type |
9.38e+5 |
%g |
Common floating-point types (shorter in type F and e) |
|
%h |
Hash code |
|
%% |
Percent type |
% |
%n |
Line break |
|
%tx |
Date and Time type (x represents a different date and time conversion character |
|
Test Cases
public static void main (String [] args) {
String str = null;
str = String.format ("Hi,% s", "Wang Li");
System.out.println (str);
str = String.format ("Hi,% s:% s.% s", "Wang Nan", "Wang Li", "Wang Zhang");
System.out.println (str);
System.out.printf ("The capitalization of the letter a is:% c% n", ‘A’);
System.out.printf ("3> 7 results are:% b% n", 3> 7);
System.out.printf ("Half of 100 is:% d% n", 100/2);
System.out.printf ("The hexadecimal number of 100 is:% x% n", 100);
System.out.printf ("The octal number of 100 is:% o% n", 100);
System.out.printf ("50 yuan book discount of 8.5 is:% f yuan% n", 50 * 0.85);
System.out.printf ("The hexadecimal number of the above price is:% a% n", 50 * 0.85);
System.out.printf ("The above price index means:% e% n", 50 * 0.85);
System.out.printf ("The length of the above index and floating-point result is shorter:% g% n", 50 * 0.85);
System.out.printf ("The discount above is% d %%% n", 85);
System.out.printf ("The hash code for the letter A is:% h% n", ‘A’);
}
Output results
Hi, Wang Li
Hi, Wang Nan: Wang Li. Wang Zhang
The capital letter a is: A
The result of 3> 7 is: false
Half of 100 is: 50
The hexadecimal number of 100 is: 64
The octal number of 100 is: 144
The 8.5 discount for a book of 50 yuan is: 42.5 million yuan
The hexadecimal number of the above price is: 0x1.54p5
The above price index indicates: 4.250000e + 01
The length of the above price index and floating point result is shorter: 42.5000
The discount above is 85%
The hash code for the letter A is: 41
With the symbol of the conversion character.
Flag |
Description |
Example |
Results |
+ |
Add a symbol to a positive or negative number |
("%+d", 15) |
+15 |
− |
Align Left |
("%-5d", 15) |
|15 | |
0 |
Number Front 0 |
("%04D", 99) |
0099 |
Space |
Add a specified number of spaces before an integer |
("% 4d", 99) |
| 792 |
, |
Group numbers with "," |
("%,f", 9999.99) |
9,999.990000 |
( |
Use parentheses to include negative numbers |
("% (f",-99.99) |
(99.990000) |
# |
If a floating-point number contains a decimal point, add 0x or 0 if the binary is 16 or 8 |
("% #x", 99) ("% #o", 99) |
0x63 0143 |
< |
Format the parameters described by the previous translator |
("%f and%<3.2f", 99.45) |
99.450000 and 99.45 |
$ |
Index of the parameter being formatted |
("%1$d,%2$s", "the", "ABC") |
99,abc |
Test Cases
public static void main (String [] args) {
String str = null;
// $ use
str = String.format ("Use of format parameter $:% 1 $ d,% 2 $ s", 99, "abc");
System.out.println (str);
// + use
System.out.printf ("Show the sign of positive and negative numbers:% + d and% d% n", 99, -99);
// Complement O use
System.out.printf ("The best number is:% 03d% n", 7);
// space use
System.out.printf ("The effect of the Tab key is:% 8d% n", 7);
//.Use
System.out.printf ("The effect of grouping integers is:%, d% n", 9989997);
// spaces and numbers after the decimal point
System.out.printf ("The price of a book is:% 50.5f yuan% n", 49.8);
}
Output results
Use of format parameter $: 99, abc
Signs showing positive and negative numbers: +99 and -99
The most cattle number is: 007
The effect of the Tab key is: 7
The effect of grouping integers is: 9,989,997
The price of a book is: 49.80000 yuan
date and event string formatting
< Span style= "Font-family:helvetica, Tahoma, Arial, Sans-serif; font-size:14px; line-height:25px; Text-align:left; text-indent:23px; " >
Format for common date and time combinations.
Conversion character |
Description |
Example |
C |
Include all date and time information |
Saturday 14:21:20 CST 2007 |
F |
"Year-month-day" format |
2007-10-27 |
D |
"Month/day/year" format |
10/27/07 |
R |
"HH:MM:SS PM" format (12 o'clock-year) |
02:25:51 pm |
T |
"HH:MM:SS" Format (24 o'clock-year) |
14:28:16 |
R |
"HH:MM" Format (24 o'clock-year) |
14:28 |
Test Cases
public static void main (String [] args) {
Date date = new Date ();
// c use
System.out.printf ("All date and time information:% tc% n", date);
// use of f
System.out.printf ("Year-Month-Day Format:% tF% n", date);
// d use
System.out.printf ("Month / Day / Year Format:% tD% n", date);
// The use of r
System.out.printf ("HH: MM: SS PM format (12 hours):% tr% n", date);
// t use
System.out.printf ("HH: MM: SS format (24 hours):% tT% n", date);
// The use of R
System.out.printf ("HH: MM format (24 hour clock):% tR", date);
}
Output results
Full date and time information: Monday September 10 10:43:36 CST 2012
Year-month-day format: 2012-09-10
Month / Day / Year Format: 09/10/12
HH: MM: SS PM format (12 hours): 10:43:36 am
HH: MM: SS format (24-hour format): 10:43:36
HH: MM format (24-hour format): 10:43
A conversion character that defines a date format enables a date to generate a new string from the specified conversion character. These date conversions.
public static void main (String [] args) {
Date date = new Date ();
// b use, short for month
String str = String.format (Locale.US, "English month abbreviation:% tb", date);
System.out.println (str);
System.out.printf ("Local month abbreviation:% tb% n", date);
// The use of B, the full name of the month
str = String.format (Locale.US, "English month full name:% tB", date);
System.out.println (str);
System.out.printf ("The full name of the local month:% tB% n", date);
// a use, week abbreviation
str = String.format (Locale.US, "Abbreviation for English week:% ta", date);
System.out.println (str);
// The use of A, full name
System.out.printf ("short for local week:% tA% n", date);
// The use of C, the first two digits
System.out.printf ("First two digits of the year (less than two digits followed by 0):% tC% n", date);
// y use, two digits after the year
System.out.printf ("The last two digits of the year (less than two digits followed by 0):% ty% n", date);
// The use of j, the number of days in a year
System.out.printf ("Days of the year (ie days of the year):% tj% n", date);
// m usage, month
System.out.printf ("Month with two digits (less than two digits followed by 0):% tm% n", date);
// d use, day (two digits, not enough to zero)
System.out.printf ("Two-digit day (less than two digits followed by 0):% td% n", date);
// e's use, day (one digit is not padded)
System.out.printf ("Day of the month (without zeros):% te", date);
}
Output results
English Month Abbreviation: Sep
Local month abbreviation: September
English full name of month: September
Local month full name: September
Abbreviation of English week: Mon
Local week abbreviation: Monday
The first two digits of the year (less than two digits followed by 0): 20
Last two digits of the year (less than two digits followed by 0): 12
Days of the year (ie days of the year): 254
Two-digit month (less than two digits followed by 0): 09
Two-digit day (less than two digits followed by 0): 10
Day of the month (without zeros): 10
the conversion of the time format is more and more accurate than the date format conversion character. It can format time into units of hours, minutes, seconds, or even milliseconds. The conversion character that formats the time string.
Conversion character |
Description |
Example |
H |
2-digit 24 o'clock hour (less than 2 bits in front of 0) |
15 |
I |
2-digit 12 o'clock hour (less than 2 bits in front of 0) |
03 |
K |
2-digit 24 o'clock hour (not 0 in front) |
15 |
L |
2-digit 12 o'clock hour (not 0 in front) |
3 |
M |
2-digit minute (less than 2 bits in front of 0) |
03 |
S |
2-digit seconds (less than 2 bits in front of 0) |
09 |
L |
3-digit millisecond (less than 3 bits preceded by 0) |
015 |
N |
9 digits in milliseconds (less than 9 bits in front of 0) |
562000000 |
P |
Small letter of the morning or afternoon mark |
Medium: PM English: PM |
Z |
Offset of the RFC822 time zone relative to GMT |
+0800 |
Z |
Time zone abbreviation string |
Cst |
S |
1970-1-1 00:00:00 to now the number of seconds elapsed |
1193468128 |
Q |
1970-1-1 00:00:00 to present the number of milliseconds passed |
1193468128984 |
Test code
public static void main (String [] args) {
Date date = new Date ();
// The use of H
System.out.printf ("Two-digit 24-hour hour (less than two digits followed by 0):% tH% n", date);
// I use
System.out.printf ("Two-digit 12-hour hour (less than two digits followed by 0):% tI% n", date);
// k use
System.out.printf ("Two-digit 24-hour hour (without zeros):% tk% n", date);
// l use
System.out.printf ("Two-digit 12-hour hour (without zeros):% tl% n", date);
// The use of M
System.out.printf ("Minutes of 2 digits (with zeros before 2 digits):% tM% n", date);
// S use
System.out.printf ("Second two-digit seconds (preceded by two before zero):% tS% n", date);
// The use of L
System.out.printf ("3 digit milliseconds (less than 3 digits followed by 0):% tL% n", date);
// The use of N
System.out.printf ("9-digit milliseconds (less than 9 digits followed by 0):% tN% n", date);
// use of p
String str = String.format (Locale.US, "Am or PM in lowercase letters (English):% tp", date);
System.out.println (str);
System.out.printf ("Morning or afternoon mark (middle) in lowercase:% tp% n", date);
// The use of z
System.out.printf ("Offset from GMT's RFC822 time zone:% tz% n", date);
// The use of Z
System.out.printf ("Time zone abbreviation string:% tZ% n", date);
// s use
System.out.printf ("1970-1-1 00:00:00 seconds until now:% ts% n", date);
// The use of Q
System.out.printf ("1970-1-1 00:00:00 milliseconds elapsed until now:% tQ% n", date);
}
Output results
24-hour hour with 2 digits (0 is added before 2 digits): 11
Two-digit hour at 12 o'clock (less than 2 digits followed by 0): 11
24-hour hour with 2 digits (no leading zeros): 11
Two-digit hour at 12 o'clock (without zeros): 11
2 digit minutes (less than 2 digits followed by 0): 03
2 digit seconds (less than 2 digits followed by 0): 52
3-digit milliseconds (less than 3 digits followed by 0): 773
9-digit milliseconds (less than 9 digits followed by 0): 773000000
Lowercase AM or PM marker (English): am
Lowercase AM or PM marker (middle): AM
RFC822 time zone offset from GMT: +0800
Time zone abbreviation string: CST
1970-1-1 00:00:00 seconds to now: 1347246232
1970-1-1 00:00:00 The number of milliseconds that have elapsed until now: 1134462462327
Java string Formatting-string.format () using "Go"