String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处。format()方法有两种重载形式。
format(String format, Object... args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字符串。
format(Locale locale, String format, Object... args) 使用指定的语言环境,制定字符串格式和参数生成格式化的字符串。
显示不同转换符实现不同数据类型到字符串的转换,如图所示。
转 换 符 | 说 明 | 示 例 |
%s | 字符串类型 | "mingrisoft" |
%c | 字符类型 | 'm' |
%b | 布尔类型 | true |
%d | 整数类型(十进制) | 99 |
%x | 整数类型(十六进制) | FF |
%o | 整数类型(八进制) | 77 |
%f | 浮点类型 | 99.99 |
%a | 十六进制浮点类型 | FF.35AE |
%e | 指数类型 | 9.38e+5 |
%g | 通用浮点类型(f和e类型中较短的) | |
%h | 散列码 | |
%% | 百分比类型 | % |
%n | 换行符 | |
%tx | 日期与时间类型(x代表不同的日期与时间转换符 |
输出结果
搭配转换符的标志,如图所示。
标 志 | 说 明 | 示 例 | 结 果 |
+ | 为正数或者负数添加符号 | ("%+d",15) | +15 |
− | 左对齐 | ("%-5d",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 |
输出结果
日期和事件字符串格式化
在程序界面中经常需要显示时间和日期,但是其显示的 格式经常不尽人意,需要编写大量的代码经过各种算法才得到理想的日期与时间格式。字符串格式中还有%tx转换符没有详细介绍,它是专门用来格式化日期和时 间的。%tx转换符中的x代表另外的处理日期和时间格式的转换符,它们的组合能够将日期和时间格式化成多种格式。
常见日期和时间组合的格式,如图所示。
转 换 符 | 说 明 | 示 例 |
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 |
输出结果
定义日期格式的转换符可以使日期通过指定的转换符生成新字符串。这些日期转换符如图所示。
输出结果
和日期格式转换符相比,时间格式的转换符要更多、更精确。它可以将时间格式化成时、分、秒甚至时毫秒等单位。格式化时间字符串的转换符如图所示。
转 换 符 | 说 明 | 示 例 |
H | 2位数字24时制的小时(不足2位前面补0) | 15 |
I | 2位数字12时制的小时(不足2位前面补0) | 03 |
k | 2位数字24时制的小时(前面不补0) | 15 |
l | 2位数字12时制的小时(前面不补0) | 3 |
M | 2位数字的分钟(不足2位前面补0) | 03 |
S | 2位数字的秒(不足2位前面补0) | 09 |
L | 3位数字的毫秒(不足3位前面补0) | 015 |
N | 9位数字的毫秒数(不足9位前面补0) | 562000000 |
p | 小写字母的上午或下午标记 | 中:下午 英:pm |
z | 相对于GMT的RFC822时区的偏移量 | +0800 |
Z | 时区缩写字符串 | CST |
s | 1970-1-1 00:00:00 到现在所经过的秒数 | 1193468128 |
Q | 1970-1-1 00:00:00 到现在所经过的毫秒数 | 1193468128984 |
测试代码
输出结果
- /*
- * Copyright 2002-2011 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.springframework.core.env;
- import java.util.LinkedHashSet;
- import java.util.Set;
- /**
- * Exception thrown when required properties are not found.
- *
- * @author Chris Beams
- * @since 3.1
- * @see ConfigurablePropertyResolver#setRequiredProperties(String...)
- * @see ConfigurablePropertyResolver#validateRequiredProperties()
- * @see org.springframework.context.support.AbstractApplicationContext#prepareRefresh()
- */
- @SuppressWarnings("serial")
- public class MissingRequiredPropertiesException extends IllegalStateException {
- private final Set<String> missingRequiredProperties = new LinkedHashSet<String>();
- /**
- * Return the set of properties marked as required but not present
- * upon validation.
- * @see ConfigurablePropertyResolver#setRequiredProperties(String...)
- * @see ConfigurablePropertyResolver#validateRequiredProperties()
- */
- public Set<String> getMissingRequiredProperties() {
- return missingRequiredProperties;
- }
- void addMissingRequiredProperty(String key) {
- missingRequiredProperties.add(key);
- }
- @Override
- public String getMessage() {
- return String.format(
- "The following properties were declared as required but could " +
- "not be resolved: %s", this.getMissingRequiredProperties());
- }
- }