Class OpenDate

java.lang.Object
cloud.opencode.base.date.OpenDate

public final class OpenDate extends Object
Core Date Utility Class - Main Entry Point for OpenCode Date Module 核心日期工具类 - OpenCode日期模块的主入口

This class provides comprehensive date/time operations as the main facade for the date module. It is designed to be the one-stop solution for most date/time needs, comparable to Commons DateUtils + Hutool DateUtil.

此类提供全面的日期时间操作,作为日期模块的主要门面。 设计为满足大多数日期时间需求的一站式解决方案, 可与Commons DateUtils + Hutool DateUtil相媲美。

Features | 主要功能:

  • Current time access - 当前时间访问
  • Date/time creation - 日期时间创建
  • Smart parsing - 智能解析
  • Formatting - 格式化
  • Truncation and rounding - 截断和取整
  • Add/subtract operations - 加减操作
  • Range and comparison - 范围和比较
  • Boundary calculations - 边界计算

Usage Examples | 使用示例:

// Current time
LocalDate today = OpenDate.today();
LocalDateTime now = OpenDate.now();
long millis = OpenDate.currentTimeMillis();

// Create dates
LocalDate date = OpenDate.of(2024, 1, 15);
LocalDateTime dateTime = OpenDate.of(2024, 1, 15, 14, 30, 0);

// Smart parsing
LocalDateTime dt1 = OpenDate.parse("2024-01-15 14:30:45");
LocalDateTime dt2 = OpenDate.parse("20240115143045");

// Formatting
String formatted = OpenDate.format(now);
String chinese = OpenDate.formatChinese(now);

// Truncation
LocalDateTime truncated = OpenDate.truncateToDay(now);

// Add/subtract
LocalDateTime future = OpenDate.plusDays(now, 7);

// Boundaries
LocalDate monthStart = OpenDate.startOfMonth(today);
LocalDate monthEnd = OpenDate.endOfMonth(today);

Performance | 性能特性:

  • All methods are static and stateless - 所有方法都是静态和无状态的
  • Cached formatters for performance - 缓存格式化器以提高性能

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes (with explicit null checks) - 空值安全: 是(有明确的空值检查)
Since:
JDK 25, opencode-base-date V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • today

      public static LocalDate today()
      Gets the current date 获取当前日期
      Returns:
      today's date | 今天的日期
    • today

      public static LocalDate today(ZoneId zone)
      Gets the current date for the specified zone 获取指定时区的当前日期
      Parameters:
      zone - the zone | 时区
      Returns:
      today's date in the zone | 该时区的今天日期
    • now

      public static LocalDateTime now()
      Gets the current datetime 获取当前日期时间
      Returns:
      the current datetime | 当前日期时间
    • now

      public static LocalDateTime now(ZoneId zone)
      Gets the current datetime for the specified zone 获取指定时区的当前日期时间
      Parameters:
      zone - the zone | 时区
      Returns:
      the current datetime in the zone | 该时区的当前日期时间
    • currentTimeMillis

      public static long currentTimeMillis()
      Gets the current time in milliseconds 获取当前时间戳(毫秒)
      Returns:
      current time in milliseconds | 当前毫秒时间戳
    • currentTimeSeconds

      public static long currentTimeSeconds()
      Gets the current time in seconds 获取当前时间戳(秒)
      Returns:
      current time in seconds | 当前秒时间戳
    • instant

      public static Instant instant()
      Gets the current Instant 获取当前Instant
      Returns:
      the current Instant | 当前Instant
    • of

      public static LocalDate of(int year, int month, int dayOfMonth)
      Creates a LocalDate 创建LocalDate
      Parameters:
      year - the year | 年
      month - the month | 月
      dayOfMonth - the day | 日
      Returns:
      the LocalDate | LocalDate
    • of

      public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)
      Creates a LocalDateTime 创建LocalDateTime
      Parameters:
      year - the year | 年
      month - the month | 月
      dayOfMonth - the day | 日
      hour - the hour | 时
      minute - the minute | 分
      second - the second | 秒
      Returns:
      the LocalDateTime | LocalDateTime
    • ofEpochMilli

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

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

      public static LocalDateTime from(Date date)
      Converts a Date to LocalDateTime 将Date转换为LocalDateTime
      Parameters:
      date - the Date | Date对象
      Returns:
      the LocalDateTime | LocalDateTime
    • from

      public static LocalDateTime from(Calendar calendar)
      Converts a Calendar to LocalDateTime 将Calendar转换为LocalDateTime
      Parameters:
      calendar - the Calendar | Calendar对象
      Returns:
      the LocalDateTime | LocalDateTime
    • parse

      public static LocalDateTime parse(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 parsing fails | 如果解析失败则抛出异常
    • 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 parsing fails | 如果解析失败则抛出异常
    • 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 parsing fails | 如果解析失败则抛出异常
    • 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 parsing fails | 如果解析失败则抛出异常
    • format

      public static String format(Temporal temporal)
      Formats a temporal to the default format (yyyy-MM-dd HH:mm:ss) 将时间对象格式化为默认格式(yyyy-MM-dd HH:mm:ss)
      Parameters:
      temporal - the temporal | 时间对象
      Returns:
      the formatted string | 格式化的字符串
    • format

      public static String format(Temporal temporal, String pattern)
      Formats a temporal to the specified pattern 将时间对象格式化为指定模式
      Parameters:
      temporal - the temporal | 时间对象
      pattern - the pattern | 模式
      Returns:
      the formatted string | 格式化的字符串
    • formatIso

      public static String formatIso(Temporal temporal)
      Formats a temporal to ISO format 将时间对象格式化为ISO格式
      Parameters:
      temporal - the temporal | 时间对象
      Returns:
      the formatted string | 格式化的字符串
    • formatChinese

      public static String formatChinese(Temporal temporal)
      Formats a temporal to Chinese format 将时间对象格式化为中文格式
      Parameters:
      temporal - the temporal | 时间对象
      Returns:
      the formatted string | 格式化的字符串
    • truncate

      public static <T extends Temporal> T truncate(T temporal, TemporalUnit unit)
      Truncates a temporal to the specified unit 将时间对象截断到指定单位
      Type Parameters:
      T - the temporal type | 时间类型
      Parameters:
      temporal - the temporal | 时间对象
      unit - the unit | 单位
      Returns:
      the truncated temporal | 截断后的时间对象
    • truncateToDay

      public static LocalDateTime truncateToDay(LocalDateTime dateTime)
      Truncates a LocalDateTime to the start of day 将LocalDateTime截断到当天开始
      Parameters:
      dateTime - the datetime | 日期时间
      Returns:
      the truncated datetime | 截断后的日期时间
    • truncateToHour

      public static LocalDateTime truncateToHour(LocalDateTime dateTime)
      Truncates a LocalDateTime to the start of hour 将LocalDateTime截断到小时开始
      Parameters:
      dateTime - the datetime | 日期时间
      Returns:
      the truncated datetime | 截断后的日期时间
    • truncateToMinute

      public static LocalDateTime truncateToMinute(LocalDateTime dateTime)
      Truncates a LocalDateTime to the start of minute 将LocalDateTime截断到分钟开始
      Parameters:
      dateTime - the datetime | 日期时间
      Returns:
      the truncated datetime | 截断后的日期时间
    • plusYears

      public static LocalDateTime plusYears(LocalDateTime dateTime, long years)
      Adds years to a LocalDateTime 在LocalDateTime上加年
      Parameters:
      dateTime - the datetime | 日期时间
      years - the years to add | 要加的年数
      Returns:
      the result | 结果
    • plusMonths

      public static LocalDateTime plusMonths(LocalDateTime dateTime, long months)
      Adds months to a LocalDateTime 在LocalDateTime上加月
      Parameters:
      dateTime - the datetime | 日期时间
      months - the months to add | 要加的月数
      Returns:
      the result | 结果
    • plusWeeks

      public static LocalDateTime plusWeeks(LocalDateTime dateTime, long weeks)
      Adds weeks to a LocalDateTime 在LocalDateTime上加周
      Parameters:
      dateTime - the datetime | 日期时间
      weeks - the weeks to add | 要加的周数
      Returns:
      the result | 结果
    • plusDays

      public static LocalDateTime plusDays(LocalDateTime dateTime, long days)
      Adds days to a LocalDateTime 在LocalDateTime上加天
      Parameters:
      dateTime - the datetime | 日期时间
      days - the days to add | 要加的天数
      Returns:
      the result | 结果
    • plusHours

      public static LocalDateTime plusHours(LocalDateTime dateTime, long hours)
      Adds hours to a LocalDateTime 在LocalDateTime上加小时
      Parameters:
      dateTime - the datetime | 日期时间
      hours - the hours to add | 要加的小时数
      Returns:
      the result | 结果
    • plusMinutes

      public static LocalDateTime plusMinutes(LocalDateTime dateTime, long minutes)
      Adds minutes to a LocalDateTime 在LocalDateTime上加分钟
      Parameters:
      dateTime - the datetime | 日期时间
      minutes - the minutes to add | 要加的分钟数
      Returns:
      the result | 结果
    • plusSeconds

      public static LocalDateTime plusSeconds(LocalDateTime dateTime, long seconds)
      Adds seconds to a LocalDateTime 在LocalDateTime上加秒
      Parameters:
      dateTime - the datetime | 日期时间
      seconds - the seconds to add | 要加的秒数
      Returns:
      the result | 结果
    • startOfMonth

      public static LocalDate startOfMonth(LocalDate date)
      Gets the first day of the month 获取月份的第一天
      Parameters:
      date - the date | 日期
      Returns:
      the first day of the month | 月份的第一天
    • endOfMonth

      public static LocalDate endOfMonth(LocalDate date)
      Gets the last day of the month 获取月份的最后一天
      Parameters:
      date - the date | 日期
      Returns:
      the last day of the month | 月份的最后一天
    • startOfYear

      public static LocalDate startOfYear(LocalDate date)
      Gets the first day of the year 获取年份的第一天
      Parameters:
      date - the date | 日期
      Returns:
      the first day of the year | 年份的第一天
    • endOfYear

      public static LocalDate endOfYear(LocalDate date)
      Gets the last day of the year 获取年份的最后一天
      Parameters:
      date - the date | 日期
      Returns:
      the last day of the year | 年份的最后一天
    • startOfWeek

      public static LocalDate startOfWeek(LocalDate date)
      Gets the first day of the week (Monday) 获取周的第一天(周一)
      Parameters:
      date - the date | 日期
      Returns:
      the first day of the week | 周的第一天
    • endOfWeek

      public static LocalDate endOfWeek(LocalDate date)
      Gets the last day of the week (Sunday) 获取周的最后一天(周日)
      Parameters:
      date - the date | 日期
      Returns:
      the last day of the week | 周的最后一天
    • startOfQuarter

      public static LocalDate startOfQuarter(LocalDate date)
      Gets the first day of the quarter 获取季度的第一天
      Parameters:
      date - the date | 日期
      Returns:
      the first day of the quarter | 季度的第一天
    • endOfQuarter

      public static LocalDate endOfQuarter(LocalDate date)
      Gets the last day of the quarter 获取季度的最后一天
      Parameters:
      date - the date | 日期
      Returns:
      the last day of the quarter | 季度的最后一天
    • isBetween

      public static boolean isBetween(LocalDate date, LocalDate start, LocalDate end)
      Checks if a date is between two dates (inclusive) 检查日期是否在两个日期之间(包含)
      Parameters:
      date - the date to check | 要检查的日期
      start - the start date | 开始日期
      end - the end date | 结束日期
      Returns:
      true if between | 如果在之间返回true
    • isBetween

      public static boolean isBetween(LocalDateTime dateTime, LocalDateTime start, LocalDateTime end)
      Checks if a datetime is between two datetimes (inclusive) 检查日期时间是否在两个日期时间之间(包含)
      Parameters:
      dateTime - the datetime to check | 要检查的日期时间
      start - the start datetime | 开始日期时间
      end - the end datetime | 结束日期时间
      Returns:
      true if between | 如果在之间返回true
    • isToday

      public static boolean isToday(LocalDate date)
      Checks if a date is today 检查日期是否为今天
      Parameters:
      date - the date | 日期
      Returns:
      true if today | 如果是今天返回true
    • isYesterday

      public static boolean isYesterday(LocalDate date)
      Checks if a date is yesterday 检查日期是否为昨天
      Parameters:
      date - the date | 日期
      Returns:
      true if yesterday | 如果是昨天返回true
    • isTomorrow

      public static boolean isTomorrow(LocalDate date)
      Checks if a date is tomorrow 检查日期是否为明天
      Parameters:
      date - the date | 日期
      Returns:
      true if tomorrow | 如果是明天返回true
    • isWeekend

      public static boolean isWeekend(LocalDate date)
      Checks if a date is a weekend (Saturday or Sunday) 检查日期是否为周末(周六或周日)
      Parameters:
      date - the date | 日期
      Returns:
      true if weekend | 如果是周末返回true
    • isLeapYear

      public static boolean isLeapYear(int year)
      Checks if a year is a leap year 检查年份是否为闰年
      Parameters:
      year - the year | 年份
      Returns:
      true if leap year | 如果是闰年返回true
    • daysBetween

      public static long daysBetween(LocalDate start, LocalDate end)
      Gets the number of days between two dates 获取两个日期之间的天数
      Parameters:
      start - the start date | 开始日期
      end - the end date | 结束日期
      Returns:
      the number of days | 天数
    • monthsBetween

      public static long monthsBetween(LocalDate start, LocalDate end)
      Gets the number of months between two dates 获取两个日期之间的月数
      Parameters:
      start - the start date | 开始日期
      end - the end date | 结束日期
      Returns:
      the number of months | 月数
    • yearsBetween

      public static long yearsBetween(LocalDate start, LocalDate end)
      Gets the number of years between two dates 获取两个日期之间的年数
      Parameters:
      start - the start date | 开始日期
      end - the end date | 结束日期
      Returns:
      the number of years | 年数
    • periodBetween

      public static Period periodBetween(LocalDate start, LocalDate end)
      Gets the Period between two dates 获取两个日期之间的Period
      Parameters:
      start - the start date | 开始日期
      end - the end date | 结束日期
      Returns:
      the Period | Period
    • durationBetween

      public static Duration durationBetween(LocalDateTime start, LocalDateTime end)
      Gets the Duration between two datetimes 获取两个日期时间之间的Duration
      Parameters:
      start - the start datetime | 开始日期时间
      end - the end datetime | 结束日期时间
      Returns:
      the Duration | Duration
    • toEpochMilli

      public static long toEpochMilli(LocalDateTime dateTime)
      Converts a LocalDateTime to epoch milliseconds 将LocalDateTime转换为毫秒时间戳
      Parameters:
      dateTime - the datetime | 日期时间
      Returns:
      the epoch milliseconds | 毫秒时间戳
    • toEpochSecond

      public static long toEpochSecond(LocalDateTime dateTime)
      Converts a LocalDateTime to epoch seconds 将LocalDateTime转换为秒时间戳
      Parameters:
      dateTime - the datetime | 日期时间
      Returns:
      the epoch seconds | 秒时间戳
    • toDate

      public static Date toDate(LocalDateTime dateTime)
      Converts a LocalDateTime to java.util.Date 将LocalDateTime转换为java.util.Date
      Parameters:
      dateTime - the datetime | 日期时间
      Returns:
      the Date | Date
    • toDate

      public static Date toDate(LocalDate date)
      Converts a LocalDate to java.util.Date 将LocalDate转换为java.util.Date
      Parameters:
      date - the date | 日期
      Returns:
      the Date | Date
    • range

      public static LocalDateRange range(LocalDate start, LocalDate end)
      Creates a LocalDateRange from start to end 创建从开始到结束的LocalDateRange
      Parameters:
      start - the start date | 开始日期
      end - the end date | 结束日期
      Returns:
      the LocalDateRange | LocalDateRange
    • currentQuarter

      public static YearQuarter currentQuarter()
      Gets the current YearQuarter 获取当前YearQuarter
      Returns:
      the current YearQuarter | 当前YearQuarter
    • currentWeek

      public static YearWeek currentWeek()
      Gets the current YearWeek 获取当前YearWeek
      Returns:
      the current YearWeek | 当前YearWeek