String.format()用法

package junit.test;

?import java.util.Date;import java.util.Locale;

?import org.junit.Test;

?publicclass StringFormat {

?/*String.format()用法


1、轉(zhuǎn)換符

%s: 字符串類(lèi)型,如:"ljq"

%b: 布爾類(lèi)型,如:true

%d: 整數(shù)類(lèi)型(十進(jìn)制),如:99

%f: 浮點(diǎn)類(lèi)型,如:99.99

%%: 百分比類(lèi)型,如:%

%n: 換行符

?*/@Testpublicvoid test1(){

?String str=null;

????str=String.format("Hi, %s", "林計(jì)欽");// 格式化字符串System.out.println(str);// 輸出字符串變量str的內(nèi)容System.out.printf("3>7的結(jié)果是:%b %n", 3>7);

????System.out.printf("100的一半是:%d %n", 100/2);

????System.out.printf("50元的書(shū)打8.5折扣是:%f 元%n", 50*0.85);

????System.out.printf("上面的折扣是%d%% %n", 85);

}/*2、常見(jiàn)日期時(shí)間格式化

c: 包括全部日期和時(shí)間信息 星期六 十月 27 14:21:20 CST 2007

F: "年-月-日"格式,如:2007-10-27

D: "月/日/年"格式,如:10/27/07

r: "HH:MM:SS PM"格式(12時(shí)制),如:02:25:51 下午

T: "HH:MM:SS"格式(24時(shí)制),如:14:28:16

R: "HH:MM"格式(24時(shí)制),如:14:28

?*/@Testpublicvoid test2(){

????Date date=newDate();// 創(chuàng)建日期對(duì)象System.out.printf("全部日期和時(shí)間信息:%tc%n", date);//格式化輸出日期或時(shí)間System.out.printf("年-月-日格式:%tF%n", date);

????System.out.printf("月/日/年格式:%tD%n", date);

????System.out.printf("HH:MM:SS PM格式(12時(shí)制):%tr%n", date);

????System.out.printf("HH:MM:SS格式(24時(shí)制):%tT%n", date);

????System.out.printf("HH:MM格式(24時(shí)制):%tR%n", date);

}/*3、格式化日期字符串

b或者h(yuǎn): 月份簡(jiǎn)稱,如

中:十月

英:Oct


B: 月份全稱,如

中:十月

英:October


a: 星期的簡(jiǎn)稱,如

中:星期六

英:Sat


A: 星期的全稱,如:

中:星期六

英:Saturday


C: 年的前兩位數(shù)字(不足兩位前面補(bǔ)0),如:20

y: 年的后兩位數(shù)字(不足兩位前面補(bǔ)0),如:07

Y: 4位數(shù)字的年份(不足4位前面補(bǔ)0),如:2007

j: 一年中的天數(shù)(即年的第幾天),如:300

m: 兩位數(shù)字的月份(不足兩位前面補(bǔ)0),如:10

d: 兩位數(shù)字的日(不足兩位前面補(bǔ)0),如:27

e: 月份的日(前面不補(bǔ)0),如:5

?*/@Testpublicvoid test3(){

????Date date=newDate();// 創(chuàng)建日期對(duì)象String str=String.format(Locale.US,"英文月份簡(jiǎn)稱:%tb",date);// 格式化日期字符串System.out.println(str);// 輸出字符串內(nèi)容System.out.printf("本地月份簡(jiǎn)稱:%tb%n",date);

????str=String.format(Locale.US,"英文月份全稱:%tB",date);

????System.out.println(str);

????System.out.printf("本地月份全稱:%tB%n",date);

????str=String.format(Locale.US,"英文星期的簡(jiǎn)稱:%ta",date);

????System.out.println(str);

????System.out.printf("本地星期的簡(jiǎn)稱:%tA%n",date);

????System.out.printf("年的前兩位數(shù)字(不足兩位前面補(bǔ)0):%tC%n",date);

????System.out.printf("年的后兩位數(shù)字(不足兩位前面補(bǔ)0):%ty%n",date);

????System.out.printf("一年中的天數(shù)(即年的第幾天):%tj%n",date);

????System.out.printf("兩位數(shù)字的月份(不足兩位前面補(bǔ)0):%tm%n",date);

????System.out.printf("兩位數(shù)字的日(不足兩位前面補(bǔ)0):%td%n",date);

????System.out.printf("月份的日(前面不補(bǔ)0):%te",date);

}/*4、格式化時(shí)間字符串

H: 2位數(shù)字24時(shí)制的小時(shí)(不足2位前面補(bǔ)0),如:15

I: 2位數(shù)字12時(shí)制的小時(shí)(不足2位前面補(bǔ)0),如:03

k: 2位數(shù)字24時(shí)制的小時(shí)(前面不補(bǔ)0),如:15

l: 2位數(shù)字12時(shí)制的小時(shí)(前面不補(bǔ)0),如:3

M: 2位數(shù)字的分鐘(不足2位前面補(bǔ)0),如:03

S: 2位數(shù)字的秒(不足2位前面補(bǔ)0),如:09

L: 3位數(shù)字的毫秒(不足3位前面補(bǔ)0),如:015

N: 9位數(shù)字的毫秒數(shù)(不足9位前面補(bǔ)0),如:562000000


p: 小寫(xiě)字母的上午或下午標(biāo)記,如:

中:下午

英:pm


z: 相對(duì)于GMT的RFC822時(shí)區(qū)的偏移量,如:+0800

Z: 時(shí)區(qū)縮寫(xiě)字符串,如:CST

s: 1970-1-1 00:00:00 到現(xiàn)在所經(jīng)過(guò)的秒數(shù),如:1193468128

Q: 1970-1-1 00:00:00 到現(xiàn)在所經(jīng)過(guò)的毫秒數(shù),如:1193468128984


?*/@Testpublicvoid test4(){

????Date date=newDate();// 創(chuàng)建日期對(duì)象System.out.printf("2位數(shù)字24時(shí)制的小時(shí)(不足2位前面補(bǔ)0):%tH%n",date);

????System.out.printf("2位數(shù)字12時(shí)制的小時(shí)(不足2位前面補(bǔ)0):%tI%n",date);

????System.out.printf("2位數(shù)字24時(shí)制的小時(shí)(前面不補(bǔ)0):%tk%n",date);

????System.out.printf("2位數(shù)字12時(shí)制的小時(shí)(前面不補(bǔ)0):%tl%n",date);

????System.out.printf("2位數(shù)字的分鐘(不足2位前面補(bǔ)0):%tM%n",date);

????System.out.printf("2位數(shù)字的秒(不足2位前面補(bǔ)0):%tS%n",date);

????System.out.printf("3位數(shù)字的毫秒(不足3位前面補(bǔ)0):%tL%n",date);

????System.out.printf("9位數(shù)字的毫秒數(shù)(不足9位前面補(bǔ)0):%tN%n",date);

????String str=String.format(Locale.US,"小寫(xiě)字母的上午或下午標(biāo)記(英):%tp",date);

????System.out.println(str);????????????????????????? // 輸出字符串變量str的內(nèi)容System.out.printf ("小寫(xiě)字母的上午或下午標(biāo)記(中):%tp%n",date);

????System.out.printf("相對(duì)于GMT的RFC822時(shí)區(qū)的偏移量:%tz%n",date);

????System.out.printf("時(shí)區(qū)縮寫(xiě)字符串:%tZ%n",date);

????System.out.printf("1970-1-1 00:00:00 到現(xiàn)在所經(jīng)過(guò)的秒數(shù):%ts%n",date);

????System.out.printf("1970-1-1 00:00:00 到現(xiàn)在所經(jīng)過(guò)的毫秒數(shù):%tQ%n",date);

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容