1.对整数进行格式化:%[index$][标识][最小宽度]转换方式
对整数进行格式化,占位符分为5段,分别为%、index$(索引)、标识、最小宽度、转换方式
其中%是占位符的起始字符,是必须的,如何占位符中想使用%,要用%%。
下面重点介绍index$(索引)、标识、最小宽度、转换方式
1)index$(索引)
index位置索引从1开始计算,用于指定对索引相应的实参进行格式化并替换掉该占位符。
描述可能比较难懂,举个例子:
System.out.println(String.format("%1$,09d,%2$#9o", -12345, 12345));
输出:
-0012,345, 030071
讲解:其实是%1$,09d用于格式化第一个数字-12345,而%2$#9o用于格式化第二个数字12345。
2)标识
'-' 在最小宽度内左对齐,不可以与“用0填充”同时使用
'#' 只适用于8进制和16进制,8进制时在结果前面增加一个0,16进制时在结果前面增加0x
'+' 结果总是包括一个符号(一般情况下只适用于10进制,若对象为BigInteger才可以用于8进制和16进制)
' ' 正值前加空格,负值前加负号(一般情况下只适用于10进制,若对象为BigInteger才可以用于8进制和16进制),可省略。
'0' 结果将用零来填充
',' 只适用于10进制,每3位数字之间用“,”分隔
'(' 若参数是负数,则结果中不添加负号而是用圆括号把数字括起来(同‘+’具有同样的限制)
转换方式:
d-十进制 o-八进制 x或X-十六进制
语言总是显得很枯燥。举几个例子说明一下。
System.out.println(String.format("%1$,09d,%2$#9o", -12345, 12345));
System.out.println(String.format("%1$9d", -12345));
System.out.println(String.format("%1$ 9d", -12345));
System.out.println(String.format("%1$9d", 12345));
System.out.println(String.format("%1$ 9d", 12345));
System.out.println(String.format("%1$-9d", -12345));
System.out.println(String.format("%1$-(9d", -12345));
System.out.println(String.format("%1$+9d", -12345));
System.out.println(String.format("%1$(+9d", 12345));
System.out.println(String.format("%1$(9d", -12345));
System.out.println(String.format("%1$#09x", 12345));
输出结果:
-0012,345, 030071
-12345
-12345
12345
12345
-12345
(12345)
-12345
+12345
(12345)
0x0003039
讲解:其中,4、5的结果是一样的,也就是说' '是可以省略的。
2.对浮点数进行格式化:%[index$][标识][最少宽度][.精度]转换方式
我们可以看到,浮点数的转换多了一个“精度”选项,可以控制小数点后面的位数。
标识:
'-' 在最小宽度内左对齐,不可以与“用0填充”同时使用
'+' 结果总是包括一个符号
' ' 正值前加空格,负值前加负号
'0' 结果将用零来填充
',' 每3位数字之间用“,”分隔(只适用于fgG的转换)
'(' 若参数是负数,则结果中不添加负号而是用圆括号把数字括起来(只适用于eEfgG的转换)
转换方式:
'e', 'E' -- 结果被格式化为用计算机科学记数法表示的十进制数
'f' -- 结果被格式化为十进制普通表示方式
'g', 'G' -- 根据具体情况,自动选择用普通表示方式还是科学计数法方式
'a', 'A' -- 结果被格式化为带有效位数和指数的十六进制浮点数*
举例:
System.out.println(String.format("%1$09.2e", 123.45));
System.out.println(String.format("%1$09.2f", 123.45));
System.out.println(String.format("%1$09.2g", 123.45));
System.out.println(String.format("%1$09.2G", 123.45));
System.out.println(String.format("%1$9.2g", 1.2345));
System.out.println(String.format("%1$09.2G", 1.2345));
System.out.println(String.format("%1$9.2a", 123.45));
System.out.println(String.format("%1$09.2A", 12.345));
输出结果:
01.23e+02
000123.45
001.2e+02
001.2E+02
1.2
0000001.2
0x1.eep6
0X01.8BP3
3、对字符、字符串进行格式化
占位符格式: %[index$][标识][最小宽度]转换符
可用标识:
-,在最小宽度内左对齐,右边用空格补上。
可用转换符:
s,字符串类型。
c,字符类型,实参必须为char或int、short等可转换为char类型的数据类型,否则抛IllegalFormatConversionException异常。
b,布尔类型,只要实参为非false或null,均格式化为字符串true,否则为字符串false。实参可以不是布尔型,如字符串、整形、浮点型等。
n,平台独立的换行符(与通过 System.getProperty("line.separator") 是一样的)实参可以是任意类型,如字符串、整形、浮点型、布尔型等。
转换符为n时[标识][最小宽度]无效。设置的话会出java.util.IllegalFormatWidthException异常。
举例:
System.out.println(String.format("TQ%1$9c %2$9cT123", 'a', 'b'));
System.out.println(String.format("well %9s", "done"));
System.out.println(String.format("well %9b", true));
System.out.println(String.format("well %9b", false));
System.out.println(String.format("well %9b", 12.3));
System.out.println(String.format("well %1$n hello %2$n", true, 123.45));
System.out.println("end");
输出结果:
TQ a bT123
well done
well true
well false
well true
well
hello
end
4、对日期进行格式化
占位符格式为: %[index$]t/T转换符
可用转换符
1). 日期的转换符
'B' 特定于语言环境的月份全称,例如 "January" 和 "February"。
'b' 特定于语言环境的月份简称,例如 "Jan" 和 "Feb"。
'h' 与 'b' 相同。
'A' 特定于语言环境的星期几全称,例如 "Sunday" 和 "Monday"
'a' 特定于语言环境的星期几简称,例如 "Sun" 和 "Mon"
'C' 除以 100 的四位数表示的年份,被格式化为必要时带前导零的两位数,即 00 - 99
'Y' 年份,被格式化为必要时带前导零的四位数(至少),例如,0092 等于格里高利历的 92 CE。
'y' 年份的最后两位数,被格式化为必要时带前导零的两位数,即 00 - 99。
'j' 一年中的天数,被格式化为必要时带前导零的三位数,例如,对于格里高利历是 001 - 366。
'm' 月份,被格式化为必要时带前导零的两位数,即 01 - 13。
'd' 一个月中的天数,被格式化为必要时带前导零两位数,即 01 - 31
'e' 一个月中的天数,被格式化为两位数,即 1 - 31。
举例:
Date now = new Date(); System.out.println(String.format("%1$TB %2$tb", now, now)); System.out.println(String.format("%Th", now)); System.out.println(String.format("%1$TA %2$ta", now, now)); System.out.println(String.format("%tC", now)); System.out.println(String.format("%tY", now)); System.out.println(String.format("%ty", now)); System.out.println(String.format("%tj", now)); System.out.println(String.format("%tm", now)); System.out.println(String.format("%td", now)); System.out.println(String.format("%te", now));
输出结果:
一月 一月
一月
星期日 星期日
20
2018
18
007
01
07
7
2)时间转换符
'H' 24 小时制的小时,被格式化为必要时带前导零的两位数,即 00 - 23。 'I' 12 小时制的小时,被格式化为必要时带前导零的两位数,即 01 - 12。 'k' 24 小时制的小时,即 0 - 23。 'l' 12 小时制的小时,即 1 - 12。 'M' 小时中的分钟,被格式化为必要时带前导零的两位数,即 00 - 59。 'S' 分钟中的秒,被格式化为必要时带前导零的两位数,即 00 - 60 ("60" 是支持闰秒所需的一个特殊值)。 'L' 秒中的毫秒,被格式化为必要时带前导零的三位数,即 000 - 999。 'N' 秒中的毫微秒,被格式化为必要时带前导零的九位数,即 000000000 - 999999999。 'p' 特定于语言环境的 上午或下午 标记以小写形式表示,例如 "am" 或 "pm"。使用转换前缀 'T' 可以强行将此输出转换为大写形式。 'z' 相对于 GMT 的 RFC 822 格式的数字时区偏移量,例如 -0800。 'Z' 表示时区缩写形式的字符串。Formatter 的语言环境将取代参数的语言环境(如果有)。 's' 自协调世界时 (UTC) 1970 年 1 月 1 日 00:00:00 至现在所经过的秒数,即 Long.MIN_VALUE/1000 与 Long.MAX_VALUE/1000 之间的差值。 'Q' 自协调世界时 (UTC) 1970 年 1 月 1 日 00:00:00 至现在所经过的毫秒数,即 Long.MIN_VALUE 与 Long.MAX_VALUE 之间的差值。
举例:
Date now2 = new Date(); System.out.println(String.format("%1$TH %2$tI", now2, now2)); System.out.println(String.format("%1$Tk %2$tl", now2, now2)); System.out.println(String.format("%tM", now2)); System.out.println(String.format("%tS", now2)); System.out.println(String.format("%tL", now2)); System.out.println(String.format("%tN", now2)); System.out.println(String.format("%tp", now2)); System.out.println(String.format("%tz", now2)); System.out.println(String.format("%tZ", now2)); System.out.println(String.format("%ts", now2)); System.out.println(String.format("%tQ", now2));
输出结果:
19 07 19 7 15 58 496 496000000 下午 +0800 CST 1515323758 1515323758496
3)组合的日期时间格式
'R' 24 小时制的时间,被格式化为 "%tH:%tM" 'T' 24 小时制的时间,被格式化为 "%tH:%tM:%tS"。 'r' 12 小时制的时间,被格式化为 "%tI:%tM:%tS %Tp"。上午或下午标记 ('%Tp') 的位置可能与语言环境有关。 'D' 日期,被格式化为 "%tm/%td/%ty"。 'F' ISO 8601 格式的完整日期,被格式化为 "%tY-%tm-%td"。 'c' 日期和时间,被格式化为 "%ta %tb %td %tT %tZ %tY",例如 "Sun Jul 20 16:17:00 EDT 1969"。
举例:
Date now3 = new Date(); System.out.println(String.format("%1$TR %2$tT", now3, now3)); System.out.println(String.format("%Tr", now3)); System.out.println(String.format("%tD", now3)); System.out.println(String.format("%tF", now3)); System.out.println(String.format("%tc", now3));
输出结果:
19:27 19:27:32 07:27:32 下午 01/07/18 2018-01-07 星期日 一月 07 19:27:32 CST 2018
5、其他转换符
<,用于格式化前一个转换符所描述的参数。
举例:
System.out.println(String.format("%d 格式化后:%<,d", 12345));
输出结果:
12345 格式化后:12,345