一、format
1、public static String format(String format, Object... args)
使用本地语言环境,制定字符串格式和参数生成格式化的新字符串。
2、public static String format(Locale l, String format, Object... args)
使用指定语言环境,制定字符串格式和参数生成格式化的新字符串
3、public PrintStream printf(String format, Object ... args)
格式化字符串打印
二、转换符
示例1:
1 System.out.println(String.format("演员:%s,%s.%s", "周星驰", "刘德华", "梁朝伟")); 2 System.out.printf("字母H的大写:%c %n", 'H'); 3 System.out.printf("8>3的结果是:%b %n", 8 > 3); 4 System.out.printf("100的一半是:%d %n", 100 / 2); 5 System.out.printf("100的16进制数是:%x %n", 100); 6 System.out.printf("100的8进制数是:%o %n", 100); 7 System.out.printf("50元的书打8.5折扣是:%f 元%n", 50 * 0.85); 8 System.out.printf("上面价格的16进制数是:%a %n", 50 * 0.85); 9 System.out.printf("上面价格的指数表示:%e %n", 50 * 0.85); 10 System.out.printf("上面价格的指数和浮点数结果的长度较短的是:%g %n", 50 * 0.85); 11 System.out.printf("上面的折扣是:%d%% %n", 85); 12 System.out.printf("字母A的散列码是:%h", 'A');
示例2:
1 System.out.print(String.format("num: %.4g %n", num)); 2 System.out.print(String.format("num: %.5g %n", num)); 3 System.out.print(String.format("num: %.6g %n", num)); 4 System.out.print(String.format("num: %.4f %n", num)); 5 System.out.print(String.format("num: %.5f %n", num)); 6 System.out.print(String.format("num: %.6f %n", num));
注意:String.format()会换行,所以不用println()
三、转换符标志
示例:
1 System.out.printf("正负数符号:%+d与%d%n", 88,-88); 2 System.out.printf("编号:%03d%n", 7); 3 System.out.printf("编号:%04d%n", 7); 4 System.out.printf("Tab键的效果是:% 8d%n", 7); 5 System.out.printf("整数分组的效果是:%,d%n", 9989997); 6 //一共十位,小数点后面是五位,小数点之前加上小数点是剩下的,不够补空格 7 System.out.printf("一本书的价格是:%10.5f元%n", 49.8); 8 System.out.printf(":%(f%n", -76.04); 9 System.out.printf(":%f和%<3.2f%n", 86.04);
四、日期、时间组合格式转换符
示例:
1 Date date = new Date(); 2 System.out.printf("全部日期和时间信息:%tc%n", date); 3 System.out.printf("月/日/年格式:%tD%n", date); 4 System.out.printf("年-月-日格式:%tF%n", date); 5 System.out.printf("HH:MM:SS PM格式(12时制):%tr%n", date); 6 System.out.printf("HH:MM格式(24时制):%tR%n", date); 7 System.out.printf("HH:MM:SS格式(24时制):%tT%n", date);
五、日期格式转换符
示例:
1 Date date = new Date(); 2 //示例 3 System.out.println(String.format(Locale.CHINA,"中文星期的简称:%ta",date)); 4 System.out.println(String.format(Locale.CHINA,"中文星期的全称:%tA",date)); 5 System.out.printf("英文星期的简称:%ta%n",date); 6 System.out.printf("英文星期的全称:%tA%n",date); 7 System.out.println(String.format(Locale.CHINA,"中文月份简称:%tb",date)); 8 System.out.println(String.format(Locale.CHINA,"中文月份全称:%tB",date)); 9 System.out.printf("英文月份简称:%tb%n",date); 10 System.out.printf("英文月份全称:%tB%n",date); 11 System.out.printf("年的前两位数字(不足两位前面补0):%tC%n",date); 12 System.out.printf("年的后两位数字(不足两位前面补0):%ty%n",date); 13 System.out.printf("一年中的第几天:%tj%n",date); 14 System.out.printf("两位数字的月份(不足两位前面补0):%tm%n",date); 15 System.out.printf("两位数字的日(不足两位前面补0):%td%n",date); 16 System.out.printf("月份的日(前面不补0):%te",date);
六、时间格式转换符
示例:
1 Date date = new Date(); 2 System.out.printf(Locale.CHINA, "两位数字24时制的小时(不足2位前面补0):%tH%n", date); 3 System.out.printf(Locale.CHINA, "两位数字12时制的小时(不足2位前面补0):%tI%n", date); 4 System.out.printf(Locale.CHINA, "两位数字24时制的小时(前面不补0):%tk%n", date); 5 System.out.printf(Locale.CHINA, "两位数字12时制的小时(前面不补0):%tl%n", date); 6 System.out.printf(Locale.CHINA, "两位数字的分钟(不足2位前面补0):%tM%n", date); 7 System.out.printf(Locale.CHINA, "两位数字的秒(不足2位前面补0):%tS%n", date); 8 System.out.printf(Locale.CHINA, "三位数字的毫秒(不足3位前面补0):%tL%n", date); 9 System.out.printf(Locale.CHINA, "九位数字的毫秒数(不足9位前面补0):%tN%n", date); 10 System.out.println(String.format("小写字母的上午或下午标记(英):%tp", date)); 11 System.out.printf(Locale.CHINA, "小写字母的上午或下午标记(中):%tp%n", date); 12 System.out.printf("相对于GMT的RFC822时区的偏移量:%tz%n", date); //有问题 13 System.out.printf("时区缩写字符串:%tZ%n", date); //有问题 14 System.out.printf("1970-1-1 00:00:00 到现在所经过的秒数:%ts%n", date); 15 System.out.printf("1970-1-1 00:00:00 到现在所经过的毫秒数:%tQ%n", date);
原文地址:https://www.jianshu.com/p/c136dea8da9a