目录
一、源码
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
二、Java 中日期时间格式化的方法
1. 用String.format()格式化日期时间
public static void main(String[] args) {
Date date1 = new Date();
System.out.println(date1);//Wed Apr 03 16:09:59 CST 2024
//%tF对应于日期格式yyyy-MM-dd
System.out.println(String.format("%tF",date1));// 2024-04-03
//%tD(完整日期MM/dd/yyyy)
System.out.println(String.format("%tD",date1));// 04/03/24
//%tT对应于时间格式HH:mm:ss
System.out.println(String.format("%tT",date1));// 16:19:26
//%tR(24小时制时间HH:mm)
System.out.println(String.format("%tR",date1));// 16:19
LocalDate date = LocalDate.now();
System.out.println(date);//2024-04-03
String formattedDate = String.format("%tF", date);
System.out.println(formattedDate); // 2024-04-03
LocalTime time = LocalTime.now();
System.out.println(time);
String formattedTime = String.format("%tT", time);//16:09:59.814
System.out.println(formattedTime); // 16:09:59
}
2. SimpleDateFormat 类
SimpleDateFormat
是一个具体的类,也是日期/时间格式化的具体实现。你可以定义自己的日期/时间模式来解析和格式化日期。示例:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedNow = sdf.format(now);
System.out.println(formattedNow); // 输出:2022-10-18 14:57:36
}
}
3. java.time 包中的类
自 Java 8 起,引入了 java.time
包,其中包含了诸如DateTimeFormatter和LocalDateTime
等类,提供了更现代且推荐的日期/时间处理方式。示例:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedNow = now.format(formatter);
System.out.println(formattedNow); // 输出:2022-10-18 14:57:36
}
}
三、Java中的格式化字符串占位符有哪些
在Java中,格式化字符串主要通过String.format()
方法或System.out.printf()
方法来实现,它们使用类似于C语言的格式化字符串语法。以下是Java中一些常见的占位符:
%d
: 整数,十进制形式。%f
: 浮点数,十进制形式。%e
: 科学记数法,小写'e'。%E
: 科学记数法,大写'E'。%s
: 字符串。%c
: 单个字符。%b
: 布尔值,true
或false
。%t
: 日期/时间,需要配合特定的时间格式如%tY
表示四位数年份。%n
: 换行符,平台无关的换行符。%%
: 百分号%
。
1. 占位符%
d和%
f
String name = "Alice";
int age = 25;
double height = 1.6823;
String formatted = String.format("Name: %s, Age: %d, Height: %.2f", name, age, height);
System.out.println(formatted);//Name: Alice, Age: 25, Height: 1.68
%.2f
意味着保留两位小数的浮点数。
2. 占位符%s
String name = "Alice";
int age = 25;
String result = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(result); // 输出:My name is Alice and I am 25 years old.
在这个例子中,%s
是占位符,表示将字符串类型的数据插入;%d
表示将整型数据插入。
3. 使用 `%t` 进行日期和时间格式化
%tF
对应于日期格式 yyyy-MM-dd
%tD
(完整日期 MM/dd/yyyy
)
%tT
对应于时间格式 HH:mm:ss
%tR
(24小时制时间 HH:mm
)等等
public static void main(String[] args) {
Date date1 = new Date();
System.out.println(date1);//Wed Apr 03 16:09:59 CST 2024
//%tF对应于日期格式yyyy-MM-dd
System.out.println(String.format("%tF",date1));// 2024-04-03
//%tD(完整日期MM/dd/yyyy)
System.out.println(String.format("%tD",date1));// 04/03/24
//%tT对应于时间格式HH:mm:ss
System.out.println(String.format("%tT",date1));// 16:19:26
//%tR(24小时制时间HH:mm)
System.out.println(String.format("%tR",date1));// 16:19
}