Java之String.format()方法案例講解
前言:
String.format()作為文本處理工具,為我們提供強(qiáng)大而豐富的字符串格式化功能,這里根據(jù)查閱的資料做個(gè)學(xué)習(xí)筆記,整理成如下文章,供后續(xù)復(fù)習(xí)查閱。
一. format()方法的兩種重載形式:
1. format(String format, Object ... args)
該方法使用指定的格式字符串和參數(shù)返回一個(gè)格式化的字符串,格式化后的新字符串使用本地默認(rèn)的語言環(huán)境。
2. format(Local l, String format, Pbject ... args)
其中,參數(shù)l為格式化過程中要應(yīng)用的語言環(huán)境。如果l為null,則不進(jìn)行本地化。
二. 占位符:
1.對(duì)整數(shù)進(jìn)行格式化:%[index$][標(biāo)識(shí)][最小寬度]轉(zhuǎn)換方式
格式化字符串由4部分組成,特殊的格式常以%index$開頭,index從1開始取值,表示將第index個(gè)參數(shù)拿進(jìn)來進(jìn)行格式化,[最小寬度]的含義也很好理解,就是最終該整數(shù)轉(zhuǎn)化的字符串最少包含多少位數(shù)字。剩下2個(gè)部分的含義:
標(biāo)識(shí):
- '-' 在最小寬度內(nèi)左對(duì)齊,不可以與"用0填充"同時(shí)使用
- '#' 只適用于8進(jìn)制和16進(jìn)制,8進(jìn)制時(shí)在結(jié)果前面增加一個(gè)0,16進(jìn)制時(shí)在結(jié)果前面增加0x
- '+' 結(jié)果總是包括一個(gè)符號(hào)(一般情況下只適用于10進(jìn)制,若對(duì)象為BigInteger才可以用于8進(jìn)制和16進(jìn)制)
- ' ' 正值前加空格,負(fù)值前加負(fù)號(hào)(一般情況下只適用于10進(jìn)制,若對(duì)象為BigInteger才可以用于8進(jìn)制和16進(jìn)制)
- '0' 結(jié)果將用零來填充
- ',' 只適用于10進(jìn)制,每3位數(shù)字之間用","分隔
- '(' 若參數(shù)是負(fù)數(shù),則結(jié)果中不添加負(fù)號(hào)而是用圓括號(hào)把數(shù)字括起來(同'+'具有同樣的限制)
轉(zhuǎn)換方式:
d-十進(jìn)制 o-八進(jìn)制 x或X-十六進(jìn)制
舉個(gè)例子,如下:
System.out.println(String.format("%1$-9d", 312356)); System.out.println(String.format("%1$#9x", 312356)); System.out.println(String.format("%1$-#9o", 312356)); System.out.println(String.format("%1$+9d", 312356)); System.out.println(String.format("%1$ 9d", 312356)); System.out.println(String.format("%1$ 9d", -312356)); System.out.println(String.format("%1$09d", 312356)); System.out.println(String.format("%1$,9d", 312356)); System.out.println(String.format("%1$(9d", 312356));
輸出結(jié)果:
312356 0x4c424 01142044 +312356 312356 -312356 000312356 312,356 312356
2.對(duì)浮點(diǎn)數(shù)進(jìn)行格式化:%[index$][標(biāo)識(shí)][最少寬度][.精度]轉(zhuǎn)換方式
我們可以看到,浮點(diǎn)數(shù)的轉(zhuǎn)換多了一個(gè)"精度"選項(xiàng),可以控制小數(shù)點(diǎn)后面的位數(shù)。
標(biāo)識(shí):
- '-' 在最小寬度內(nèi)左對(duì)齊,不可以與"用0填充"同時(shí)使用
- '+' 結(jié)果總是包括一個(gè)符號(hào)
- ' ' 正值前加空格,負(fù)值前加負(fù)號(hào)
- '0' 結(jié)果將用零來填充
- ',' 每3位數(shù)字之間用","分隔(只適用于fgG的轉(zhuǎn)換)
- '(' 若參數(shù)是負(fù)數(shù),則結(jié)果中不添加負(fù)號(hào)而是用圓括號(hào)把數(shù)字括起來(只適用于eEfgG的轉(zhuǎn)換)
轉(zhuǎn)換方式:
- 'e', 'E' -- 結(jié)果被格式化為用計(jì)算機(jī)科學(xué)記數(shù)法表示的十進(jìn)制數(shù)
- 'f' -- 結(jié)果被格式化為十進(jìn)制普通表示方式
- 'g', 'G' -- 根據(jù)具體情況,自動(dòng)選擇用普通表示方式還是科學(xué)計(jì)數(shù)法方式
- 'a', 'A' -- 結(jié)果被格式化為帶有效位數(shù)和指數(shù)的十六進(jìn)制浮點(diǎn)數(shù)
System.out.println(String.format("%1$-9.2f", 3123.32)); System.out.println(String.format("%1$+9.2f", 3123.32)); System.out.println(String.format("%1$ 9.2f", -3123.32)); System.out.println(String.format("%1$ 9.2f", 3123.32)); System.out.println(String.format("%1$09.2f", 3123.32)); System.out.println(String.format("%1$,9.2f", 3123.32)); System.out.println(String.format("%1$(9.2f", -3123.32)); System.out.println(String.format("%1$9.2e", -3123.32)); System.out.println(String.format("%1$9.2f", -3123.32)); System.out.println(String.format("%1$9.2g", -3123.32)); System.out.println(String.format("%1$9.2a", -3123.32));
輸出結(jié)果:
3123.32 +3123.32 -3123.32 3123.32 003123.32 3,123.32 (3123.32) -3.12e+03 -3123.32 -3.1e+03 -0x1.86p11
3.對(duì)字符進(jìn)行格式化:
對(duì)字符進(jìn)行格式化是非常簡(jiǎn)單的,c表示字符,標(biāo)識(shí)中'-'表示左對(duì)齊,其他就沒什么了。
三. 對(duì)日期進(jìn)行格式化:
常用的日期格式轉(zhuǎn)換符如下表所示:
轉(zhuǎn)換符 | 說明 | 示例 |
%te | 一個(gè)月中的某一天(1~31) | 2 |
%tb | 指定語言環(huán)境的月份簡(jiǎn)稱 | Feb(英文)、二月(中文) |
%tB | 指定語言環(huán)境的月份全稱 | February(英文)、二月(中文) |
%tA | 指定語言環(huán)境的星期幾全稱 | Monday(英文)、星期一(中文) |
%ta | 指定語言環(huán)境的星期幾簡(jiǎn)稱 | Mon(英文)、星期一(中文) |
%tc | 包括全部日期和時(shí)間信息 | 星期二 三月 25 13:37:22 CST 2008 |
%tY | 4位年份 | 2019 |
%tj | 一年中的第幾天(001~366) | 085 |
%tm | 月份 | 03 |
%td | 一個(gè)月中的第幾天(01~31) | 02 |
%ty | 2位年份 | 19 |
舉個(gè)例子,如下:
public class Eval { public static void main(String[] args) { Date date = new Date(); String day = String.format("%te", date); System.out.println("今天是2019年8月:" + day + "號(hào)"); String month = String.format("%tb", date); System.out.println("現(xiàn)在是2019年:" + month); String xingqi = String.format("%tA", date); System.out.println("今天是:" + xingqi); String year = String.format("%tY", date); System.out.println("現(xiàn)在是:" + year + "年"); } }
輸出結(jié)果:
1 今天是2019年8月:20號(hào)
2 現(xiàn)在是2019年:八月
3 今天是:星期二
4 現(xiàn)在是:2019年
常用的時(shí)間格式轉(zhuǎn)換符如下表所示:
轉(zhuǎn)換符 | 說明 | 示例 |
%tH | 2位數(shù)字的24時(shí)制的小時(shí)(00~23) | 14 |
%tI | 2位數(shù)字的12時(shí)制的小時(shí)(01~12) | 05 |
%tk | 2位數(shù)字的24時(shí)制的小時(shí)(0~23) | 5 |
%tl | 2位數(shù)字的12時(shí)制的小時(shí)(1~12) | 10 |
%tM | 2位數(shù)字的分鐘(00~59) | 05 |
%tS | 2位數(shù)字的秒數(shù)(00~60) | 12 |
%tL | 3位數(shù)字的毫秒數(shù)(000~999) | 920 |
%tN | 9位數(shù)字的微秒數(shù)(000000000~999999999) | 062000000 |
%tp | 指定語言環(huán)境下上午或下午標(biāo)記 | 下午(中文)、pm(英文) |
%tz | 相對(duì)于GMT RFC 82格式的數(shù)字時(shí)區(qū)偏移量 | +0800 |
%tZ | 時(shí)區(qū)縮寫形式的字符串 | CST |
%ts | 1970-01-01 00:00:00至現(xiàn)在經(jīng)過的秒數(shù) | 1206345534 |
%tQ | 1970-01-01 00:00:00至現(xiàn)在經(jīng)過的毫秒數(shù) | 12923409349034 |
舉個(gè)例子,如下:
public class GetDate { public static void main(String[] args) { Date date = new Date(); String hour = String.format("%tH", date); String minute = String.format("%tM", date); String second = String.format("%tS", date); System.out.println("現(xiàn)在是:" + hour + "點(diǎn)" + minute + "分" + second + "秒"); System.out.println("##################################"); String hour2 = String.format("%tI", date); String pm = String.format("%tp", date); System.out.println("現(xiàn)在是:" + pm + hour2 + "點(diǎn)" + minute + "分" + second + "秒"); } }
輸出結(jié)果:
1 現(xiàn)在是:15點(diǎn)06分37秒
2 ##################################
3 現(xiàn)在是:下午03點(diǎn)06分37秒
常見的日期和時(shí)間組合的格式如下表所示:
轉(zhuǎn)換符 | 說明 | 示例 |
%tF | “年-月-日”格式(4位年份) | 2019-08-20 |
%tD | “年/月/日”格式(2位年份) | 08/20/19 |
%tc | 全部日期和時(shí)間信息 | 星期二 三月 25 15:20:00 CST 2019 |
%tr | “時(shí):分:秒 PM(AM)”格式(12時(shí)制) | 03:22:06 下午 |
%tT | “時(shí):分:秒”格式(24時(shí)制) | 15:23:50 |
%tR | “時(shí):分”格式(24時(shí)制) | 15:25 |
舉個(gè)例子,如下:
public class DateAndTime { public static void main(String[] args) { Date date = new Date(); String time = String.format("%tc", date); String form = String.format("%tF", date); String form2 = String.format("%tD", date); String form3 = String.format("%tr", date); String form4 = String.format("%tT", date); String form5 = String.format("%tR", date); System.out.println("全部的時(shí)間信息是:" + time); System.out.println("年-月-日格式:" + form); System.out.println("年/月/日格式:" + form2); System.out.println("時(shí):分:秒 PM(AM)格式:" + form3); System.out.println("時(shí):分:秒格式:" + form4); System.out.println("時(shí):分格式:" + form5); } }
輸出結(jié)果:
全部的時(shí)間信息是:星期二 八月 20 15:14:20 CST 2019 年-月-日格式:2019-08-20 年/月/日格式:08/20/19 時(shí):分:秒 PM(AM)格式:03:14:20 下午 時(shí):分:秒格式:15:14:20 時(shí):分格式:15:14
結(jié)尾:
以上內(nèi)容為format()方法的一些常用功能,也是本人在工作場(chǎng)景中經(jīng)常用到的。整理歸納方便后續(xù)學(xué)習(xí)查閱,如果后面還有遇到相關(guān)方法的其他用法,后期再對(duì)該篇文章進(jìn)行補(bǔ)充。
到此這篇關(guān)于Java之String.format()方法案例講解的文章就介紹到這了,更多相關(guān)Java之String.format()方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
聊聊Spring AOP @Before @Around @After等advice的執(zhí)行順序
這篇文章主要介紹了聊聊Spring AOP @Before @Around @After等advice的執(zhí)行順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02Spring?MVC中JSON數(shù)據(jù)處理方式實(shí)戰(zhàn)案例
Spring MVC是個(gè)靈活的框架,返回JSON數(shù)據(jù)的也有很多五花八門的方式,下面這篇文章主要給大家介紹了關(guān)于Spring?MVC中JSON數(shù)據(jù)處理方式的相關(guān)資料,需要的朋友可以參考下2024-01-01Java8中Optional操作的實(shí)際應(yīng)用
Optional類是一個(gè)可以為null的容器對(duì)象,如果值存在則isPresent()方法會(huì)返回true,調(diào)用get()方法會(huì)返回該對(duì)象,下面這篇文章主要給大家介紹了關(guān)于Java8中Optional操作實(shí)際應(yīng)用的相關(guān)資料,需要的朋友可以參考下2022-02-02java不用循環(huán)語句打印數(shù)組元素的實(shí)例
下面小編就為大家?guī)硪黄猨ava不用循環(huán)語句打印數(shù)組元素的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03java實(shí)現(xiàn)學(xué)生教師管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)學(xué)生教師管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10SpringBoot3整合郵件服務(wù)實(shí)現(xiàn)郵件發(fā)送功能
本文介紹了spring boot整合email服務(wù),實(shí)現(xiàn)發(fā)送驗(yàn)證碼,郵件(普通文本郵件、靜態(tài)資源郵件、附件郵件),文中通過代碼示例介紹的非常詳細(xì),堅(jiān)持看完相信對(duì)你有幫助,需要的朋友可以參考下2024-05-05Java?設(shè)計(jì)模式以虹貓藍(lán)兔的故事講解原型模式
原型模式是用于創(chuàng)建重復(fù)的對(duì)象,同時(shí)又能保證性能。這種類型的設(shè)計(jì)模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對(duì)象的最佳方式,今天通過本文給大家介紹下Java 原型設(shè)計(jì)模式,感興趣的朋友一起看看吧2022-04-04