1、介绍
String类的 format()方法用于创建格式化的字符串以及连接多个字符串对象。format()方法有两种重载形式。
// 使用当前本地区域对象(Locale.getDefault()),制定字符串格式和参数生成格式化的字符串
String String.format(String fmt, Object... args);
// 自定义本地区域对象,制定字符串格式和参数生成格式化的字符串
String String.format(Locale locale, String fmt, Object... args);
2、格式:
下面的[]符号里面都是选择性的项目,因此只有%与type是必要的. 格式化说明的顺序是有规定的,必须要以这个顺序章指定.
格式:% [argument number] [flags] [width] [.precision] type
argument number : 如果要格式化的参数超过一个以上,可以在这里指定是哪一个。
flags : 特定类型的特定选项,例如:数字要加 逗号 或 正负号。
width : 最小的字符数。注意:这里不是总数,输出可以超过此宽度,若不是则会主动补0。如果数字宽度小于指定宽度,则在转换后的字符串前部补空格。 如:format("%,6.1f",42.00),转换后的字符串为: 42.0 (前面后两个空格,用以补全指定的6位宽度)。
.percision : 精确度。 注意:前面有个圆点符号。
type :一定要指定的类型标识。即:要转换参数的 数据类型。
2.1、超过一个以上参数时:
超过一个以上参数时,如两个参数,把新的参数加到后面,因此format()中会有3个参数,并且在第一个参数中,也就是格式化串中,会有两个不同的格式化设定,也就是两个%开头的字符组合,第二个会应用在第一个%上面,第三个参数会用在第二%上,也就是参数会依照顺序应用在%上面"。
如:
String s = String.format("第一个参数:%,d 第二个参数:%,.2f", 1234, 5678.123); System.out.println(s);输出:
第一个参数:1,234 第二个参数:5,678.12
3、转换符:
转换符 | 详细说明 | 示例 |
---|---|---|
%s | 字符串类型 | “喜欢请收藏” |
%c | 字符类型 | ‘m’ |
%b | 布尔类型 | true |
%d | 整数类型(十进制) | 88 |
%x | 整数类型(十六进制) | FF |
%o | 整数类型(八进制) | 77 |
%f | 浮点类型 | 8.888 |
%a | 十六进制浮点类型 | FF.35AE |
%e | 指数类型 | 9.38e+5 |
%g | 通用浮点类型(f和e类型中较短的) | 不举例(基本用不到) |
%h | 散列码 | 不举例(基本用不到) |
%% | 百分比类型 | %(%特殊字符%%才能显示%) |
%n | 换行符 | 不举例(基本用不到) |
%tx | 日期与时间类型(x代表不同的日期与时间转换符) | 不举例(基本用不到) |
4、转换符的标志
标志 | 说明 | 示例 | 结果 |
---|---|---|---|
+ | 为正数或者负数添加符号 | (“%+d”,15) | +15 |
0 | 数字前面补0(加密常用) | (“%04d”, 99) | 0099 |
空格 | 在整数之前添加指定数量的空格 | (“% 4d”, 99) | 99 |
, | 以“,”对数字分组(常用显示金额) | (“%,f”, 9999.99) | 9,999.990000 |
( | 使用括号包含负数 | (“%(f”, -99.99) | (99.990000) |
# | 如果是浮点数则包含小数点,如果是16进制或8进制则添加0x或0 | (“%#x”, 99)(“%#o”, 99) | 0x63 0143 |
< | 格式化前一个转换符所描述的参数 | (“%f和%<3.2f”, 99.45) | 99.450000和99.45 |
$ | 被格式化的参数索引 | ("%1$d,%2$s”, 99,”abc”) | 99,abc |
4.1、示例
【对字符串进行格式化】示例—将"hello"格式化为"hello "(左对齐)
public static void main(String[] args) throws ParseException {
String raw = "hello word";
String str1 = String.format("|%-15s|", raw);
System.out.println(str1);// |hello word |
}
【对整数进行格式化】示例—将-1000显示为(1,000)
public static void main(String[] args) throws ParseException {
int num = -1000; String str = String.format("%(,d", num);
System.out.println(str); // (1,000)
}
【对浮点数进行格式化】
public static void main(String[] args) throws ParseException {
double num = 123.456789; System.out.print(String.format("浮点类型:%.2f %n", num));
System.out.print(String.format("十六进制浮点类型:%a %n", num));
System.out.print(String.format("通用浮点类型:%g ", num));
/* 输出:
浮点类型:123.46
十六进制浮点类型:0x1.edd3c07ee0b0bp6
通用浮点类型:123.457
*/
}
5、对日期、时间进行格式化
3.1 转换符中有介绍 %tx , x代表日期转换符,具体如下:
5.1、日期的转换符
转换符 | 说明 | 示例 |
---|---|---|
c | 包括全部日期和时间信息 | 星期六 十月 27 14:21:20 CST 2007 |
F | “年-月-日”格式 | 2007-10-27 |
D | “月/日/年”格式 | 10/27/07 |
r | “HH:MM:SS PM”格式(12时制) | 02:25:51 下午 |
T | “HH:MM:SS”格式(24时制) | 14:28:16 |
R | “HH:MM”格式(24时制) | 14:28 |
5.2、时间的转换符
转换符 | 说明 | 示例 |
H | 2位数字的24时制的小时(00~23) | 14 |
I (i的大写) | 2位数字的12时制的小时(01~12) | 05 |
k | 2位数字的24时制的小时(0~23) | 5 |
l(L的小写) | 2位数字的12时制的小时(1~12) | 10 |
M | 2位数字的分钟(00~59) | 05 |
S | 2位数字的秒数(00~60) | 12 |
L | 3位数字的毫秒数(000~999) | 920 |
N | 9位数字的微秒数(000000000~999999999) | 062000000 |
p | 指定语言环境下上午或下午标记 | 下午(中文)、pm(英文) |
z | 相对于GMT RFC 82格式的数字时区偏移量 | +0800 |
Z | 时区缩写形式的字符串 | CST |
s | 1970-01-01 00:00:00至现在经过的秒数 | 1206345534 |
Q | 1970-01-01 00:00:00至现在经过的毫秒数 | 12923409349 |
5.3、示例:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2021-11-01 09:10:10");
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);
String form6 = String.format("%tl", date);
String form7 = String.format("%tI", date);
System.out.println("全部的时间信息是:" + time);
System.out.println("年-月-日格式:" + form);
System.out.println("年/月/日格式:" + form2);
System.out.println("时:分:秒 PM(AM)格式:" + form3);
System.out.println("时:分:秒格式:" + form4);
System.out.println("时:分格式:" + form5);
System.out.println("时:小时格式:" + form6);
System.out.println("时:小时格式:" + form7);
/*输出:
全部的时间信息是:星期一 十一月 01 09:10:10 CST 2021
年-月-日格式:2021-11-01
年/月/日格式:11/01/21
时:分:秒 PM(AM)格式:09:10:10 上午
时:分:秒格式:09:10:10
时:分格式:09:10
时:小时格式:9
时:小时格式:09
*/
}