
DateTimeFormatter处理不同格式的日期与时间?粉丝提问:
在Java中,如何用
DateTimeFormatter处理日期和时间的格式化与解析?是否可以支持自定义格式?
本文将详细讲解DateTimeFormatter的功能,并通过丰富的示例演示如何高效地格式化和解析日期与时间,包括自定义格式的应用。
DateTimeFormatter简介DateTimeFormatter是Java 8引入的时间格式化与解析工具,代替了旧的SimpleDateFormat,解决了以下问题:
DateTimeFormatter是不可变类,支持多线程操作。LocalDate、LocalTime、LocalDateTime等类。DateTimeFormatter提供了一些常用的预定义格式化器,例如:
ISO_LOCAL_DATE:格式为yyyy-MM-ddISO_LOCAL_TIME:格式为HH:mm:ssISO_LOCAL_DATE_TIME:格式为yyyy-MM-dd'T'HH:mm:ssimport java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class BuiltInFormatterExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
// 使用预定义格式
DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_LOCAL_TIME;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
System.out.println("当前日期:" + now.format(dateFormatter));
System.out.println("当前时间:" + now.format(timeFormatter));
System.out.println("当前日期和时间:" + now.format(dateTimeFormatter));
}
}通过ofPattern方法创建自定义格式化器,支持灵活的日期与时间格式。
模式 | 含义 | 示例 |
|---|---|---|
yyyy | 年 | 2024 |
MM | 月(两位数) | 12 |
dd | 日(两位数) | 22 |
HH | 小时(24小时制) | 14 |
mm | 分钟 | 30 |
ss | 秒 | 15 |
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CustomFormatterExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
// 自定义格式
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String formattedDateTime = now.format(customFormatter);
System.out.println("自定义格式日期和时间:" + formattedDateTime);
}
}DateTimeFormatter不仅可以格式化日期和时间,还可以将字符串解析为日期时间对象。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ParseWithBuiltInFormatterExample {
public static void main(String[] args) {
String dateStr = "2024-12-22";
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
LocalDate parsedDate = LocalDate.parse(dateStr, formatter);
System.out.println("解析后的日期:" + parsedDate);
}
}import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ParseWithCustomFormatterExample {
public static void main(String[] args) {
String dateTimeStr = "2024/12/22 14:30:15";
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeStr, customFormatter);
System.out.println("解析后的日期和时间:" + parsedDateTime);
}
}解析字符串时,如果格式不匹配,会抛出DateTimeParseException。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class ExceptionHandlingExample {
public static void main(String[] args) {
String invalidDateTimeStr = "2024-12-22 14:30:15"; // 格式不匹配
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
try {
LocalDateTime dateTime = LocalDateTime.parse(invalidDateTimeStr, formatter);
System.out.println("解析成功:" + dateTime);
} catch (DateTimeParseException e) {
System.out.println("解析失败:" + e.getMessage());
}
}
}如果需要解析或格式化旧格式的日期,可以结合java.util.Date与java.time的转换方法。
LocalDateimport java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class OldDateConversionExample {
public static void main(String[] args) {
Date oldDate = new Date();
// 转换为 LocalDate
LocalDate localDate = oldDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println("旧日期转换为 LocalDate:" + localDate);
}
}Q:DateTimeFormatter和SimpleDateFormat有什么区别?
A:
DateTimeFormatter是线程安全的,而SimpleDateFormat需要手动加锁。DateTimeFormatter与新时间API集成更好,支持不可变操作。Q:如何解析带时区的日期时间?
A:使用ZonedDateTime和适配的格式化器。
DateTimeFormatter的核心功能: