Conventional type of format
String class format () method is used to create formatted strings and a plurality of strings connected objects. Familiar with the C language students should remember that sprintf C language () method, the two were similar. format () method has two overloads.
format (String format, Object ... args) new string using the local language environment, develop new string parameter string format and generate formatted.
format (Locale locale, String format, Object ... args) using the specified locale, format string and string to develop parameters to generate formatted.
Conversion display different data types to achieve different symbol string conversions, shown in FIG.
Character conversion |
Description |
Shows an example |
%s |
String type |
"mingrisoft" |
%c |
Character Types |
'm' |
%b |
Boolean |
true |
%d |
Integer type (decimal) |
99 |
%x |
Integer type (hexadecimal) |
FF |
%O |
Integer type (octal) |
77 |
%f |
Floating-point type |
99.99 |
%a |
Hexadecimal floating-point type |
FF.35AE |
%e |
Index type |
9.38e + 5 |
%g |
General floating point type (f and e of the shorter type) |
|
%h |
Hash code |
|
%% |
Percentage Type |
% |
%n |
Newline |
|
%tx |
The type of date and time (x representing a different date and time conversion specifier |
static void main public (String [] args) { String STR = null; STR = String.format ( "the Hi,% S", "Wang"); System.out.println (STR); STR = String.format ( ". Hi,% s:% s% s", " King of the South", "Wang", "Wang Zhang"); System.out.println (str); System.out.printf ( "a letter is uppercase :%% n-C ", 'a'); System.out.printf (" result 3> is 7: n-% B% ", 3> 7); System.out.printf (" half 100 is:% % n-D ", 100/2); System.out.printf (" 100 is hexadecimal 16:%% n-X ", 100); System.out.printf (" 100 is a hexadecimal number. 8:% % n-O ", 100); System.out.printf (" 50-membered 8.5 discount is typed as:% f n-membered% ", 50 * 0.85); the System.out. printf ( "16 hexadecimal price was above: n-% A%", 50 * 0.85); System.out.printf ( "price index above indicates:% e% n", 50 * 0.85); System.out.printf ( "index price and float above results shorter length is:%% n-G", 50 * 0.85); System.out.printf ( "discount is above% d %%% n ", 85); System.out.printf (" hash code letter a is:%% n-H ", 'a'); }
Output:
Hi, Wang Hi, Wang Nan: Wang Wang Zhang letter a capital be: A Results> 7 is: false half 100 is: 50 hexadecimal number is 100: 64 octal 100 is: 144 $ 50 to play book discount is 8.5: 42.500000 yuan hexadecimal number is above the price: 0x1.54p5 above represents the price index: 4.250000e + 01 shorter length exponent floating point results and the price is above : 42.5000 above 85% discount hash code letter a is: 41
With AMI breaks
as the picture shows:
Mark |
Description |
Shows an example |
Results |
+ |
Add to positive or negative sign |
("%+d",15) |
+15 |
− |
Left |
("%-5d",15) |
|15 | |
0 |
Digital a leading zero |
("%04d", 99) |
0099 |
Blank |
Adds the specified number of spaces before integer |
("% 4d", 99) |
| 99| |
, |
To "," digital packet |
("%,f", 9999.99) |
9,999.990000 |
( |
Use brackets including negatives |
("%(f", -99.99) |
(99.990000) |
# |
If the float contains a decimal point, or if it is a hexadecimal or octal 0x is added 0 |
("%#x", 99) ("%#o", 99) |
0x63 0143 |
< |
A parameter identifier described in the pre-conversion format |
("%f和%<3.2f", 99.45) |
99.450000 and 99.45 |
$ |
Parameter index formatted |
("%1$d,%2$s", 99,"abc") |
99,abc |
public static void main(String[] args) { String str=null; //$使用 str=String.format("格式参数$的使用:%1$d,%2$s", 99,"abc"); System.out.println(str); //+使用 System.out.printf("显示正负数的符号:%+d与%d%n", 99,-99); //补O使用 System.out.printf("最牛的编号是:%03d%n", 7); //空格使用 System.out.printf("Tab键的效果是:% 8d%n", 7); //.使用 System.out.printf("整数分组的效果是:%,d%n", 9989997); //空格和小数点后面个数 System.out.printf("一本书的价格是:% 50.5f元%n", 49.8); }
输出结果
格式参数$的使用:99,abc 显示正负数的符号:+99与-99 最牛的编号是:007 Tab键的效果是: 7 整数分组的效果是:9,989,997 一本书的价格是: 49.80000元
日期和事件字符串格式化
在程序界面中经常需要显示时间和日期,但是其显示的 格式经常不尽人意,需要编写大量的代码经过各种算法才得到理想的日期与时间格式。字符串格式中还有%tx转换符没有详细介绍,它是专门用来格式化日期和时 间的。%tx转换符中的x代表另外的处理日期和时间格式的转换符,它们的组合能够将日期和时间格式化成多种格式。
常见日期和时间组合的格式,如图所示。
转 换 符 |
说 明 |
示 例 |
c |
包括全部日期和时间信息 |
星期六 十月 27 14:21:20 CST 2007 |
F |
“年-月-日”格式 |
2007-10-27 |
D |
“月/日/年”格式 |
10/27/07 |
r |
“HH:MM:SS PM”格式(12时制) |
02:25:51 下午 |
T |
“HH:MM:SS”格式(24时制) |
14:28:16 |
R |
“HH:MM”格式(24时制) |
14:28 |
public static void main(String[] args) { Date date=new Date(); //c的使用 System.out.printf("全部日期和时间信息:%tc%n",date); //f的使用 System.out.printf("年-月-日格式:%tF%n",date); //d的使用 System.out.printf("月/日/年格式:%tD%n",date); //r的使用 System.out.printf("HH:MM:SS PM格式(12时制):%tr%n",date); //t的使用 System.out.printf("HH:MM:SS格式(24时制):%tT%n",date); //R的使用 System.out.printf("HH:MM格式(24时制):%tR",date); }
输出结果:
全部日期和时间信息:星期一 九月 10 10:43:36 CST 2012 年-月-日格式:2012-09-10 月/日/年格式:09/10/12 HH:MM:SS PM格式(12时制):10:43:36 上午 HH:MM:SS格式(24时制):10:43:36 HH:MM格式(24时制):10:43
定义日期格式的转换符可以使日期通过指定的转换符生成新字符串。这些日期转换符如图所示。
public static void main(String[] args) { Date date=new Date(); //b的使用,月份简称 String str=String.format(Locale.US,"英文月份简称:%tb",date); System.out.println(str); System.out.printf("本地月份简称:%tb%n",date); //B的使用,月份全称 str=String.format(Locale.US,"英文月份全称:%tB",date); System.out.println(str); System.out.printf("本地月份全称:%tB%n",date); //a的使用,星期简称 str=String.format(Locale.US,"英文星期的简称:%ta",date); System.out.println(str); //A的使用,星期全称 System.out.printf("本地星期的简称:%tA%n",date); //C的使用,年前两位 System.out.printf("年的前两位数字(不足两位前面补0):%tC%n",date); //y的使用,年后两位 System.out.printf("年的后两位数字(不足两位前面补0):%ty%n",date); //j的使用,一年的天数 System.out.printf("一年中的天数(即年的第几天):%tj%n",date); //m的使用,月份 System.out.printf("两位数字的月份(不足两位前面补0):%tm%n",date); //d的使用,日(二位,不够补零) System.out.printf("两位数字的日(不足两位前面补0):%td%n",date); //e的使用,日(一位不补零) System.out.printf("月份的日(前面不补0):%te",date);
输出结果
英文月份简称:Sep 本地月份简称:九月 英文月份全称:September 本地月份全称:九月 英文星期的简称:Mon 本地星期的简称:星期一 年的前两位数字(不足两位前面补0):20 年的后两位数字(不足两位前面补0):12 一年中的天数(即年的第几天):254 两位数字的月份(不足两位前面补0):09 两位数字的日(不足两位前面补0):10 月份的日(前面不补0):10
和日期格式转换符相比,时间格式的转换符要更多、更精确。它可以将时间格式化成时、分、秒甚至时毫秒等单位。格式化时间字符串的转换符如图所示。
转 换 符 |
说 明 |
示 例 |
H |
2位数字24时制的小时(不足2位前面补0) |
15 |
I |
2位数字12时制的小时(不足2位前面补0) |
03 |
k |
2位数字24时制的小时(前面不补0) |
15 |
l |
2位数字12时制的小时(前面不补0) |
3 |
M |
2位数字的分钟(不足2位前面补0) |
03 |
S |
2位数字的秒(不足2位前面补0) |
09 |
L |
3位数字的毫秒(不足3位前面补0) |
015 |
N |
9位数字的毫秒数(不足9位前面补0) |
562000000 |
p |
小写字母的上午或下午标记 |
中:下午 英:pm |
z |
相对于GMT的RFC822时区的偏移量 |
+0800 |
Z |
时区缩写字符串 |
CST |
s |
1970-1-1 00:00:00 到现在所经过的秒数 |
1193468128 |
Q |
1970-1-1 00:00:00 到现在所经过的毫秒数 |
1193468128984 |
public static void main(String[] args) { Date date = new Date(); //H的使用 System.out.printf("2位数字24时制的小时(不足2位前面补0):%tH%n", date); //I的使用 System.out.printf("2位数字12时制的小时(不足2位前面补0):%tI%n", date); //k的使用 System.out.printf("2位数字24时制的小时(前面不补0):%tk%n", date); //l的使用 System.out.printf("2位数字12时制的小时(前面不补0):%tl%n", date); //M的使用 System.out.printf("2位数字的分钟(不足2位前面补0):%tM%n", date); //S的使用 System.out.printf("2位数字的秒(不足2位前面补0):%tS%n", date); //L的使用 System.out.printf("3位数字的毫秒(不足3位前面补0):%tL%n", date); //N的使用 System.out.printf("9位数字的毫秒数(不足9位前面补0):%tN%n", date); //p的使用 String str = String.format(Locale.US, "小写字母的上午或下午标记(英):%tp", date); System.out.println(str); System.out.printf("小写字母的上午或下午标记(中):%tp%n", date); //z的使用 System.out.printf("相对于GMT的RFC822时区的偏移量:%tz%n", date); //Z的使用 System.out.printf("时区缩写字符串:%tZ%n", date); //s的使用 System.out.printf("1970-1-1 00:00:00 到现在所经过的秒数:%ts%n", date); //Q的使用 System.out.printf("1970-1-1 00:00:00 到现在所经过的毫秒数:%tQ%n", date); }
输出结果
2 digits 24 made of hours (less than two leading zero): 11 2 digits 12 hour hour (less than two leading zero): 11 2 digits 24 made of h (without leading complement 0) : 11 2 digits 12 made of h (without leading complement 0): 11 two-digit minute (less than two leading zero): 03 two-digit seconds (less than two leading zero): 52 3 number of milliseconds (less than a leading zero 3): 773 9 milliseconds number (less than 9 leading zero): 773,000,000 lowercase letters AM or PM flag (English): AM lowercase letters AM or PM flag (in ): morning respect RFC822 time zone offset from GMT: +0800 time zone string abbreviation: CST 1970-1-1 00:00:00 to the current number of seconds elapsed: 1347246232 1970-1-1 00:00: 00 milliseconds to now elapsed: 1347246232773
Original Address: