Class DateParser
java.lang.Object
cloud.opencode.base.date.formatter.DateParser
Smart Date Parser that can automatically detect and parse various date formats
智能日期解析器,可自动检测和解析各种日期格式
This class provides intelligent parsing that automatically detects the format of the input string and parses it appropriately. It supports 20+ common date formats.
此类提供智能解析,自动检测输入字符串的格式并进行适当解析。支持20多种常见日期格式。
Features | 主要功能:
- Auto-detect and parse 20+ common date/time formats - 自动检测并解析20多种常见日期时间格式
- Parse dates, times, and date-times - 解析日期、时间和日期时间
- Parse from Unix epoch (seconds and milliseconds) - 从Unix纪元(秒和毫秒)解析
- Parse with explicit pattern - 使用显式模式解析
Supported Formats | 支持的格式:
- yyyy-MM-dd, yyyy/MM/dd, yyyy.MM.dd
- yyyy-MM-dd HH:mm:ss, yyyy-MM-dd'T'HH:mm:ss
- yyyyMMdd, yyyyMMddHHmmss
- yyyy年MM月dd日, yyyy年MM月dd日 HH时mm分ss秒
- Unix timestamp (seconds and milliseconds)
- ISO 8601, RFC 1123, and more
Usage Examples | 使用示例:
// Smart parse - auto-detects format
LocalDateTime dt1 = DateParser.parseDateTime("2024-01-15 14:30:45");
LocalDateTime dt2 = DateParser.parseDateTime("2024/01/15 14:30:45");
LocalDateTime dt3 = DateParser.parseDateTime("20240115143045");
LocalDateTime dt4 = DateParser.parseDateTime("2024年01月15日 14时30分45秒");
// Parse date only
LocalDate date = DateParser.parseDate("2024-01-15");
// Parse time only
LocalTime time = DateParser.parseTime("14:30:45");
// Parse from timestamp
LocalDateTime fromMillis = DateParser.fromEpochMilli(1705312245000L);
LocalDateTime fromSeconds = DateParser.fromEpochSecond(1705312245L);
// Parse with explicit pattern
LocalDateTime explicit = DateParser.parse("15/01/2024", "dd/MM/yyyy");
Performance | 性能特性:
- Pattern matching for quick format detection - 模式匹配快速格式检测
- Cached formatters for repeated parsing - 缓存格式化器用于重复解析
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Input validation: Yes - 输入验证: 是
- Since:
- JDK 25, opencode-base-date V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic LocalDateTimefromEpochMilli(long epochMilli) Creates a LocalDateTime from epoch milliseconds 从毫秒时间戳创建LocalDateTimestatic LocalDateTimefromEpochMilli(long epochMilli, ZoneId zone) Creates a LocalDateTime from epoch milliseconds with specified zone 从毫秒时间戳创建LocalDateTime(指定时区)static LocalDateTimefromEpochSecond(long epochSecond) Creates a LocalDateTime from epoch seconds 从秒时间戳创建LocalDateTimestatic LocalDateTimefromEpochSecond(long epochSecond, ZoneId zone) Creates a LocalDateTime from epoch seconds with specified zone 从秒时间戳创建LocalDateTime(指定时区)static booleanisValidDate(String text) Checks if the given text can be parsed as a date 检查给定文本是否可以解析为日期static booleanisValidDateTime(String text) Checks if the given text can be parsed as a datetime 检查给定文本是否可以解析为日期时间static booleanisValidTime(String text) Checks if the given text can be parsed as a time 检查给定文本是否可以解析为时间static LocalDateTimeParses a string to LocalDateTime using the specified pattern 使用指定模式将字符串解析为LocalDateTimestatic LocalDateParses a string to LocalDate using smart format detection 使用智能格式检测将字符串解析为LocalDatestatic LocalDateParses a string to LocalDate using the specified pattern 使用指定模式将字符串解析为LocalDatestatic LocalDateTimeparseDateTime(String text) Parses a string to LocalDateTime using smart format detection 使用智能格式检测将字符串解析为LocalDateTimestatic LocalTimeParses a string to LocalTime using smart format detection 使用智能格式检测将字符串解析为LocalTimestatic LocalTimeParses a string to LocalTime using the specified pattern 使用指定模式将字符串解析为LocalTimestatic LocalDatetryParseDate(String text) Tries to parse a string to LocalDate, returns null on failure 尝试将字符串解析为LocalDate,失败返回nullstatic LocalDateTimetryParseDateTime(String text) Tries to parse a string to LocalDateTime, returns null on failure 尝试将字符串解析为LocalDateTime,失败返回nullstatic LocalTimetryParseTime(String text) Tries to parse a string to LocalTime, returns null on failure 尝试将字符串解析为LocalTime,失败返回null
-
Method Details
-
parseDateTime
Parses a string to LocalDateTime using smart format detection 使用智能格式检测将字符串解析为LocalDateTime- Parameters:
text- the text to parse | 要解析的文本- Returns:
- the parsed LocalDateTime | 解析后的LocalDateTime
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
parseDate
Parses a string to LocalDate using smart format detection 使用智能格式检测将字符串解析为LocalDate- Parameters:
text- the text to parse | 要解析的文本- Returns:
- the parsed LocalDate | 解析后的LocalDate
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
parseTime
Parses a string to LocalTime using smart format detection 使用智能格式检测将字符串解析为LocalTime- Parameters:
text- the text to parse | 要解析的文本- Returns:
- the parsed LocalTime | 解析后的LocalTime
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
parse
Parses a string to LocalDateTime using the specified pattern 使用指定模式将字符串解析为LocalDateTime- Parameters:
text- the text to parse | 要解析的文本pattern- the pattern | 模式- Returns:
- the parsed LocalDateTime | 解析后的LocalDateTime
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
parseDate
Parses a string to LocalDate using the specified pattern 使用指定模式将字符串解析为LocalDate- Parameters:
text- the text to parse | 要解析的文本pattern- the pattern | 模式- Returns:
- the parsed LocalDate | 解析后的LocalDate
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
parseTime
Parses a string to LocalTime using the specified pattern 使用指定模式将字符串解析为LocalTime- Parameters:
text- the text to parse | 要解析的文本pattern- the pattern | 模式- Returns:
- the parsed LocalTime | 解析后的LocalTime
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
fromEpochMilli
Creates a LocalDateTime from epoch milliseconds 从毫秒时间戳创建LocalDateTime- Parameters:
epochMilli- the epoch milliseconds | 毫秒时间戳- Returns:
- the LocalDateTime | LocalDateTime
-
fromEpochMilli
Creates a LocalDateTime from epoch milliseconds with specified zone 从毫秒时间戳创建LocalDateTime(指定时区)- Parameters:
epochMilli- the epoch milliseconds | 毫秒时间戳zone- the zone | 时区- Returns:
- the LocalDateTime | LocalDateTime
-
fromEpochSecond
Creates a LocalDateTime from epoch seconds 从秒时间戳创建LocalDateTime- Parameters:
epochSecond- the epoch seconds | 秒时间戳- Returns:
- the LocalDateTime | LocalDateTime
-
fromEpochSecond
Creates a LocalDateTime from epoch seconds with specified zone 从秒时间戳创建LocalDateTime(指定时区)- Parameters:
epochSecond- the epoch seconds | 秒时间戳zone- the zone | 时区- Returns:
- the LocalDateTime | LocalDateTime
-
tryParseDateTime
Tries to parse a string to LocalDateTime, returns null on failure 尝试将字符串解析为LocalDateTime,失败返回null- Parameters:
text- the text to parse | 要解析的文本- Returns:
- the parsed LocalDateTime, or null if parsing fails | 解析后的LocalDateTime,解析失败返回null
-
tryParseDate
-
tryParseTime
-
isValidDateTime
Checks if the given text can be parsed as a datetime 检查给定文本是否可以解析为日期时间- Parameters:
text- the text to check | 要检查的文本- Returns:
- true if parseable | 如果可解析返回true
-
isValidDate
Checks if the given text can be parsed as a date 检查给定文本是否可以解析为日期- Parameters:
text- the text to check | 要检查的文本- Returns:
- true if parseable | 如果可解析返回true
-
isValidTime
Checks if the given text can be parsed as a time 检查给定文本是否可以解析为时间- Parameters:
text- the text to check | 要检查的文本- Returns:
- true if parseable | 如果可解析返回true
-