Class DateParser

java.lang.Object
cloud.opencode.base.date.formatter.DateParser

public final class DateParser extends Object
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 Type
    Method
    Description
    fromEpochMilli(long epochMilli)
    Creates a LocalDateTime from epoch milliseconds 从毫秒时间戳创建LocalDateTime
    fromEpochMilli(long epochMilli, ZoneId zone)
    Creates a LocalDateTime from epoch milliseconds with specified zone 从毫秒时间戳创建LocalDateTime(指定时区)
    fromEpochSecond(long epochSecond)
    Creates a LocalDateTime from epoch seconds 从秒时间戳创建LocalDateTime
    fromEpochSecond(long epochSecond, ZoneId zone)
    Creates a LocalDateTime from epoch seconds with specified zone 从秒时间戳创建LocalDateTime(指定时区)
    static boolean
    Checks if the given text can be parsed as a date 检查给定文本是否可以解析为日期
    static boolean
    Checks if the given text can be parsed as a datetime 检查给定文本是否可以解析为日期时间
    static boolean
    Checks if the given text can be parsed as a time 检查给定文本是否可以解析为时间
    parse(String text, String pattern)
    Parses a string to LocalDateTime using the specified pattern 使用指定模式将字符串解析为LocalDateTime
    static LocalDate
    Parses a string to LocalDate using smart format detection 使用智能格式检测将字符串解析为LocalDate
    static LocalDate
    parseDate(String text, String pattern)
    Parses a string to LocalDate using the specified pattern 使用指定模式将字符串解析为LocalDate
    Parses a string to LocalDateTime using smart format detection 使用智能格式检测将字符串解析为LocalDateTime
    static LocalTime
    Parses a string to LocalTime using smart format detection 使用智能格式检测将字符串解析为LocalTime
    static LocalTime
    parseTime(String text, String pattern)
    Parses a string to LocalTime using the specified pattern 使用指定模式将字符串解析为LocalTime
    static LocalDate
    Tries to parse a string to LocalDate, returns null on failure 尝试将字符串解析为LocalDate,失败返回null
    Tries to parse a string to LocalDateTime, returns null on failure 尝试将字符串解析为LocalDateTime,失败返回null
    static LocalTime
    Tries to parse a string to LocalTime, returns null on failure 尝试将字符串解析为LocalTime,失败返回null

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • parseDateTime

      public static LocalDateTime parseDateTime(String text)
      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

      public static LocalDate parseDate(String text)
      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

      public static LocalTime parseTime(String text)
      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

      public static LocalDateTime parse(String text, String pattern)
      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

      public static LocalDate parseDate(String text, String pattern)
      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

      public static LocalTime parseTime(String text, String pattern)
      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

      public static LocalDateTime fromEpochMilli(long epochMilli)
      Creates a LocalDateTime from epoch milliseconds 从毫秒时间戳创建LocalDateTime
      Parameters:
      epochMilli - the epoch milliseconds | 毫秒时间戳
      Returns:
      the LocalDateTime | LocalDateTime
    • fromEpochMilli

      public static LocalDateTime fromEpochMilli(long epochMilli, ZoneId zone)
      Creates a LocalDateTime from epoch milliseconds with specified zone 从毫秒时间戳创建LocalDateTime(指定时区)
      Parameters:
      epochMilli - the epoch milliseconds | 毫秒时间戳
      zone - the zone | 时区
      Returns:
      the LocalDateTime | LocalDateTime
    • fromEpochSecond

      public static LocalDateTime fromEpochSecond(long epochSecond)
      Creates a LocalDateTime from epoch seconds 从秒时间戳创建LocalDateTime
      Parameters:
      epochSecond - the epoch seconds | 秒时间戳
      Returns:
      the LocalDateTime | LocalDateTime
    • fromEpochSecond

      public static LocalDateTime fromEpochSecond(long epochSecond, ZoneId zone)
      Creates a LocalDateTime from epoch seconds with specified zone 从秒时间戳创建LocalDateTime(指定时区)
      Parameters:
      epochSecond - the epoch seconds | 秒时间戳
      zone - the zone | 时区
      Returns:
      the LocalDateTime | LocalDateTime
    • tryParseDateTime

      public static LocalDateTime tryParseDateTime(String text)
      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

      public static LocalDate tryParseDate(String text)
      Tries to parse a string to LocalDate, returns null on failure 尝试将字符串解析为LocalDate,失败返回null
      Parameters:
      text - the text to parse | 要解析的文本
      Returns:
      the parsed LocalDate, or null if parsing fails | 解析后的LocalDate,解析失败返回null
    • tryParseTime

      public static LocalTime tryParseTime(String text)
      Tries to parse a string to LocalTime, returns null on failure 尝试将字符串解析为LocalTime,失败返回null
      Parameters:
      text - the text to parse | 要解析的文本
      Returns:
      the parsed LocalTime, or null if parsing fails | 解析后的LocalTime,解析失败返回null
    • isValidDateTime

      public static boolean isValidDateTime(String text)
      Checks if the given text can be parsed as a datetime 检查给定文本是否可以解析为日期时间
      Parameters:
      text - the text to check | 要检查的文本
      Returns:
      true if parseable | 如果可解析返回true
    • isValidDate

      public static boolean isValidDate(String text)
      Checks if the given text can be parsed as a date 检查给定文本是否可以解析为日期
      Parameters:
      text - the text to check | 要检查的文本
      Returns:
      true if parseable | 如果可解析返回true
    • isValidTime

      public static boolean isValidTime(String text)
      Checks if the given text can be parsed as a time 检查给定文本是否可以解析为时间
      Parameters:
      text - the text to check | 要检查的文本
      Returns:
      true if parseable | 如果可解析返回true