本文目錄ui
1 Formatter類this
2 格式化說明符.net
3 其它相關方法orm
1 Formatter類對象
1.1 使用方法blog
Formatter formatter = new Formatter([destination]);ci
formatter.format(String format, Object…args);字符串
1.2 使用說明get
(1)Formatter構造參數:io
若無參數,格式化後的字符串會被存放在一個內部的StringBuffer中,此後,可經過formatter.toString()方法返回格式化後的字符串。
// 無參數構造Formatter對象
Formatter formatter = new Formatter();
// 格式化操做
formatter.format("The result number is %d.", 7);
// 得到格式化後的字符串
String str = formatter.toString();
// 控制檯輸出內容:The result number is 7.
System.out.println(str);
如有參數,該參數表示要輸出的目標位置,能夠是一個StringBuffer對象,一個文件,或一個數據流,此後,格式化的字符串將直接輸出到指定位置。
// 構造Formatter對象並指向標準輸出流
Formatter formatter = new Formatter(System.out);
// 格式化並輸出到指定位置(控制檯輸出內容:The result number is 7.)
formatter.format("The result number is %d.", 7);
(2)format方法參數:
String format
是一個包含格式化說明符的字符串,該字符串指定了總體目標格式,經過格式化說明符進行佔位並指定相應位置的內容格式;
Object… args
表示多個參數對象,其內容將依次對應format中的佔位符(格式化說明符),根據指定的內容格式填充到指定位置,從而造成一個知足要求的字符串。
2 格式化說明符
2.1 格式
%[argument_index$][flags][width][.precision]conversion
2.2 格式說明
(1)argument_index$:指定對應的內容參數位置,默認按照順序依次對應。
(2)flags:格式控制。
(3)width:區域寬度。
(4).precision:對於浮點型數據,表示顯示的小數位數;對於字符串數據,表示顯示的字符數量。
(5)conversion:類型轉換字符。
2.3 格式控制(flags)
符號 做用 示例 效果
無負號 右對齊 formatter.format("***%8d***", 1000); *** 1000***
有負號「-」 左對齊 formatter.format("***%-8d***", 1000); ***1000 ***
有加號「+」 正數前顯示正號
負數前顯示負號 formatter.format("***%+8d***", 1000);
formatter.format("***%+8d***", -1000); *** +1000***
*** -1000***
有空格「 」 正數前顯示空格
負號前顯示負號 formatter.format("***% 8d***", 1000);
formatter.format("***% 8d***", -1000); *** 1000***
*** -1000***
有零「0」 使用0填充剩餘位置 formatter.format("***%08d***", 1000); ***00001000***
有逗號「,」 每3位數字添加一個逗號 formatter.format("***%,8d***", 1000); *** 1,000***
2.4 類型轉換字符
符號 類型 示例 效果
d 整數型(十進制) formatter.format("%d", 1000); 1000
o 整數型(八進制) formatter.format("%o", 1000); 1750
x 整數型(十六進制) formatter.format("%x", 1000); 3e8
f 浮點型(十進制) formatter.format("%f", 1000.0); 1000.000000
e 浮點型(科學計數) formatter.format("%e", 1000.0); 1.000000e+03
b 布爾型 formatter.format("%b", true); true
c 字符型 formatter.format("%c", 'A'); A
s 字符串型 formatter.format("%s", "String"); String
% 字符「%」 formatter.format("%d%%", 100); 100%
2.5 時間類型轉換字符
符號 類型 示例 效果
tC 上世紀 formatter.format("%tC", calendar); 20
tY 年(4位) formatter.format("%tY", calendar); 2017
ty 年(2位) formatter.format("%ty", calendar); 17
tm 月 formatter.format("%tm", calendar); 11
tB 月份 formatter.format("%tB", calendar); November
tb 月份縮寫 formatter.format("%tb", calendar); Nov
td 日(2位) formatter.format("%td", calendar); 20
te 日 formatter.format("%te", calendar); 20
tA 星期 formatter.format("%tA", calendar); Monday
ta 星期(縮寫) formatter.format("%ta", calendar); Mon
tH 小時(24小時制)(2位) formatter.format("%tH", calendar); 18
tk 小時(24小時制) formatter.format("%tk", calendar); 18
tI 小時(12小時制)(2位) formatter.format("%tI", calendar); 06
tl 小時(12小時制) formatter.format("%tl", calendar); 6
tM 分鐘 formatter.format("%tM", calendar); 10
tS 秒 formatter.format("%tS", calendar); 22
tL 毫秒 formatter.format("%tL", calendar); 490
tN 微秒 formatter.format("%tN", calendar); 570000000
tp 上午/下午 formatter.format("%tp", calendar); pm
tz 時區 formatter.format("%tz", calendar); +0800
tZ 時區(縮寫) formatter.format("%tZ", calendar); CST
ts 自1970-01-01 00:00的秒數 formatter.format("%ts", calendar); 1511172687
tQ 自1970-01-01 00:00的毫秒數 formatter.format("%tQ", calendar); 1511172687297
tF YYYY-MM-DD formatter.format("%tF", calendar); 2017-11-20
tD MM/DD/YY formatter.format("%tD", calendar); 11/20/17
tR HH:MM(24小時制) formatter.format("%tR", calendar); 18:11
tT HH:MM:SS(24小時制) formatter.format("%tT", calendar); 18:11:27
tr HH:MM:SS 上午/下午 formatter.format("%tr", calendar); 06:11:27 pm
tc 星期 月 日 HH:MM:SS YYYY formatter.format("%tc", calendar); Mon Nov 20 18:11:27 CST 2017
3 其它相關方法
3.1 String.format方法
該方法內部,實際上也是經過Formatter類進行格式化,而後,將格式化後的字符串返回,其內部代碼以下:
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
3.2 System.out.format方法
該方法內部,也是經過Formatter類進行格式化,而後,將格式化後的字符串直接輸出到System.out輸出流,其內部代碼以下:
public PrintStream format(String format, Object ... args) { try { synchronized (this) { ensureOpen(); if ((formatter == null) || (formatter.locale() != Locale.getDefault())) formatter = new Formatter((Appendable) this); formatter.format(Locale.getDefault(), format, args); } } catch (InterruptedIOException x) { Thread.currentThread().interrupt(); } catch (IOException x) { trouble = true; } return this;} 來源:CSDN 原文:https://blog.csdn.net/cuixianlong/article/details/78583186 版權聲明:本文爲博主原創文章,轉載請附上博文連接!