formate JAVA_JAVA String.format 方法使用
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 = newDate();
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 = newDate();
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下午+0800CST1515323758
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 = newDate();
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 格式化后:%
輸出結果:
12345 格式化后:12,345
總結
以上是生活随笔為你收集整理的formate JAVA_JAVA String.format 方法使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 音响就是这么有趣有趣的音响
- 下一篇: 拿下win10的无情小盾牌win10去小