String.format()字符串常规类型格式化的两种重载方式
- format(String format, Object… args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字符串。
- format(Locale locale, String format, Object… args) 使用指定的语言环境,制定字符串格式和参数生成格式化的字符串。
第一个例子中有说到 %tx x代表日期转换符 我也顺便列举下日期转换符
Date date=new Date();//c的使用System.out.printf("全部日期和时间信息:%tc%n",date);//f的使用System.out.printf("年-月-日格式:%tF%n",date);//d的使用System.out.printf("月/日/年格式:%tD%n",date);//r的使用System.out.printf("HH:MM:SS PM格式(12时制):%tr%n",date);//t的使用System.out.printf("HH:MM:SS格式(24时制):%tT%n",date);//R的使用System.out.printf("HH:MM格式(24时制):%tR",date);输出:全部日期和时间信息:星期三 九月 21 22:43:36 CST 2016年-月-日格式:2016-09-21月/日/年格式:16/10/21HH:MM:SS PM格式(12时制):10:43:36 下午HH:MM:SS格式(24时制):22:43:36HH:MM格式(24时制):22:43

String str=null;str=String.format("Hi,%s", "小超");System.out.println(str);// Hi,小超str=String.format("Hi,%s %s %s", "小超","是个","大帅哥");System.out.println(str);// Hi,小超 是个 大帅哥System.out.printf("字母c的大写是:%c %n", 'C');//字母c的大写是:CSystem.out.printf("布尔结果是:%b %n", "小超".equal("帅哥"));//布尔的结果是:falseSystem.out.printf("100的一半是:%d %n", 100/2);//100的一半是:50System.out.printf("100的16进制数是:%x %n", 100);// 100的16进制数是:64System.out.printf("100的8进制数是:%o %n", 100);// 100的8进制数是:144System.out.printf("50元的书打8.5折扣是:%f 元%n", 50*0.85);// 100的8进制数是:144System.out.printf("上面价格的16进制数是:%a %n", 50*0.85);// 上面价格的16进制数是:0x1.54p5System.out.printf("上面价格的指数表示:%e %n", 50*0.85);// 上面价格的指数表示:4.250000e+01System.out.printf("上面价格的指数和浮点数结果的长度较短的是:%g %n", 50*0.85);// 上面价格的指数和浮点数结果的长度较短的是:42.5000System.out.printf("上面的折扣是%d%% %n", 85);// 上面的折扣是85%System.out.printf("字母A的散列码是:%h %n", 'A');// 字母A的散列码是:41
