Java SimpleDateFormat 类
SimpleDateFormat
是 Java 中用于格式化和解析日期时间的类,属于 java.text
包。它允许你将日期对象(Date
)转换为特定格式的字符串,或者将格式化的日期字符串解析回日期对象。
基本用法
1. 创建 SimpleDateFormat 对象
要使用 SimpleDateFormat
,首先需要创建一个实例并指定日期格式模式:
实例
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// 创建 SimpleDateFormat 对象并指定格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 获取当前日期时间
Date now = new Date();
// 格式化日期为字符串
String formattedDate = sdf.format(now);
System.out.println("当前时间: " + formattedDate);
}
}
import java.util.Date;
public class Main {
public static void main(String[] args) {
// 创建 SimpleDateFormat 对象并指定格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 获取当前日期时间
Date now = new Date();
// 格式化日期为字符串
String formattedDate = sdf.format(now);
System.out.println("当前时间: " + formattedDate);
}
}
2. 常用格式模式符号
以下是常用的格式模式符号:
符号 | 含义 | 示例 |
---|---|---|
yyyy | 四位年份 | 2023 |
MM | 两位月份 | 01-12 |
dd | 两位日期 | 01-31 |
HH | 24小时制小时 | 00-23 |
mm | 分钟 | 00-59 |
ss | 秒钟 | 00-59 |
E | 星期几 | Mon, Tue 等 |
a | AM/PM 标记 | AM 或 PM |
日期格式化
将 Date
对象格式化为字符串:
实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 EEEE a hh:mm:ss");
Date now = new Date();
System.out.println(sdf.format(now));
// 输出示例: 2023年11月15日 星期三 下午 03:45:22
Date now = new Date();
System.out.println(sdf.format(now));
// 输出示例: 2023年11月15日 星期三 下午 03:45:22
日期解析
将格式化的日期字符串解析为 Date
对象:
实例
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = "2023-11-15";
Date date = sdf.parse(dateString);
System.out.println(date);
// 输出: Wed Nov 15 00:00:00 CST 2023
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = "2023-11-15";
Date date = sdf.parse(dateString);
System.out.println(date);
// 输出: Wed Nov 15 00:00:00 CST 2023
} catch (ParseException e) {
e.printStackTrace();
}
注意:解析时可能会抛出 ParseException
,需要进行异常处理。
线程安全性问题
SimpleDateFormat
不是线程安全的。如果在多线程环境中共享同一个 SimpleDateFormat
实例,可能会导致问题。解决方案:
- 每次使用时创建新的实例(性能较低)
- 使用
ThreadLocal
为每个线程保存一个实例 - 使用 Java 8 的
DateTimeFormatter
(推荐)
实例
// 使用 ThreadLocal 的解决方案
private static final ThreadLocal<SimpleDateFormat> dateFormat =
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
// 使用方式
String formattedDate = dateFormat.get().format(new Date());
private static final ThreadLocal<SimpleDateFormat> dateFormat =
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
// 使用方式
String formattedDate = dateFormat.get().format(new Date());
Java 8 替代方案
在 Java 8 及更高版本中,推荐使用 java.time
包中的类:
实例
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 创建格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 获取当前时间并格式化
LocalDateTime now = LocalDateTime.now();
String formattedDate = now.format(formatter);
System.out.println("当前时间: " + formattedDate);
// 解析字符串为日期时间
LocalDateTime parsedDate = LocalDateTime.parse("2023-11-15 14:30:00", formatter);
System.out.println("解析后的时间: " + parsedDate);
}
}
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 创建格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 获取当前时间并格式化
LocalDateTime now = LocalDateTime.now();
String formattedDate = now.format(formatter);
System.out.println("当前时间: " + formattedDate);
// 解析字符串为日期时间
LocalDateTime parsedDate = LocalDateTime.parse("2023-11-15 14:30:00", formatter);
System.out.println("解析后的时间: " + parsedDate);
}
}
DateTimeFormatter
是线程安全的,性能更好,推荐在新项目中使用。
总结
SimpleDateFormat
用于日期格式化和解析- 通过模式字符串指定日期格式
- 不是线程安全的,需要注意多线程环境下的使用
- Java 8 及以上版本推荐使用
DateTimeFormatter
通过掌握 SimpleDateFormat
,你可以在 Java 程序中灵活地处理各种日期时间格式的转换需求。
以下是 java.text.SimpleDateFormat 类的常用方法:
构造方法
方法 | 描述 |
---|---|
SimpleDateFormat() |
使用默认模式和默认语言环境的日期格式符号构造对象 |
SimpleDateFormat(String pattern) |
使用给定模式和默认语言环境的日期格式符号构造对象 |
SimpleDateFormat(String pattern, Locale locale) |
使用给定模式和给定语言环境的日期格式符号构造对象 |
常用格式模式符号
符号 | 含义 | 示例 |
---|---|---|
y |
年 | yyyy → 2023 |
M |
月 | MM → 07, MMM → Jul, MMMM → July |
d |
月中的天数 | dd → 15 |
H |
小时(0-23) | HH → 14 |
h |
小时(1-12) | hh → 02 |
m |
分钟 | mm → 30 |
s |
秒 | ss → 45 |
S |
毫秒 | SSS → 789 |
E |
星期几 | EEE → Mon, EEEE → Monday |
a |
AM/PM标记 | a → PM |
z |
时区 | z → CST, zzzz → China Standard Time |
核心方法
方法 | 描述 |
---|---|
String format(Date date) |
将Date格式化为日期/时间字符串 |
Date parse(String source) |
从字符串解析文本生成Date对象 |
void applyPattern(String pattern) |
应用新的日期时间模式 |
String toPattern() |
返回当前日期时间模式字符串 |
void setTimeZone(TimeZone zone) |
设置时区 |
TimeZone getTimeZone() |
获取时区 |
void setLenient(boolean lenient) |
设置解析是否宽松(默认true) |
boolean isLenient() |
判断是否宽松解析 |
点我分享笔记