Java 8 日期时间格式化指南
Java 8 日期时间格式化指南
Java 8 引入的新日期时间 API 提供了强大的格式化功能,使日期时间的格式化和解析变得简单直观。格式化是将日期时间对象转换为字符串的过程,而解析则是将字符串转换回日期时间对象。
1. DateTimeFormatter 类
DateTimeFormatter 是 java.time.format 包中的核心类,用于格式化和解析日期时间。它既可以使用预定义的格式器,也可以创建自定义格式器。
创建 DateTimeFormatter
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class DateFormatDemo {
public static void main(String[] args) {
// 获取当前日期和时间
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
// 使用预定义格式器
DateTimeFormatter standardFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
String formattedDate = standardFormatter.format(currentDate);
System.out.println("标准格式日期: " + formattedDate);
// 创建自定义格式器
DateTimeFormatter patternFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
String customFormattedDate = patternFormatter.format(currentDate);
System.out.println("自定义格式日期: " + customFormattedDate);
}
}
2. 日期时间格式化
使用 DateTimeFormatter 可以轻松将日期时间对象格式化为字符串。
格式化示例
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TimeFormattingDemo {
public static void main(String[] args) {
// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
// 使用 ISO 格式
DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String isoResult = now.format(isoFormatter);
System.out.println("ISO 格式: " + isoResult);
// 使用自定义模式
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String customResult = now.format(customFormatter);
System.out.println("自定义格式: " + customResult);
}
}
3. 日期时间解析
同样可以使用 DateTimeFormatter 将字符串解析为日期时间对象。
解析示例
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class TimeParsingDemo {
public static void main(String[] args) {
String dateStr = "2023-08-15T14:30:00";
// 使用 ISO 格式解析
DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime parsedDateTime = LocalDateTime.parse(dateStr, isoFormatter);
System.out.println("解析结果: " + parsedDateTime);
// 使用自定义格式解析
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
try {
LocalDateTime customParsed = LocalDateTime.parse("2023-08-15 14:30:00", customFormatter);
System.out.println("自定义解析结果: " + customParsed);
} catch (DateTimeParseException e) {
System.out.println("日期时间解析失败: " + e.getMessage());
}
}
}
4. 时区日期时间格式化
DateTimeFormatter 同样支持带时区的日期时间格式化和解析。
时区日期时间示例
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class ZoneTimeFormattingDemo {
public static void main(String[] args) {
// 获取当前时区日期时间
ZonedDateTime nowWithZone = ZonedDateTime.now();
// 使用 ISO 格式
DateTimeFormatter isoZoneFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
String isoZoneResult = nowWithZone.format(isoZoneFormatter);
System.out.println("ISO 时区格式: " + isoZoneResult);
// 使用自定义格式
DateTimeFormatter customZoneFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm z");
try {
ZonedDateTime customZoneTime = ZonedDateTime.parse("2023-08-15 14:30 Asia/Shanghai", customZoneFormatter);
System.out.println("自定义时区解析结果: " + customZoneTime);
} catch (DateTimeParseException e) {
System.out.println("时区日期时间解析失败: " + e.getMessage());
}
}
}
注意事项
DateTimeFormatter是不可变类,线程安全,可在多线程环境中共享使用- 格式模式必须与日期时间字符串匹配,否则会抛出
DateTimeParseException - 格式化和解析带时区的日期时间时,格式模式中应包含时区信息
- 预定义格式器提供了常用的标准格式,适用于大多数场景