Class DateTimes

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

public final class DateTimes extends Object
Static Factory for Date and Time Types 日期时间类型的静态工厂

This class provides convenient static factory methods for creating various date/time types. It serves as a unified entry point for creating instances of standard java.time types as well as extended types from this module.

此类提供方便的静态工厂方法来创建各种日期时间类型。 它作为创建标准java.time类型以及本模块扩展类型实例的统一入口点。

Features | 主要功能:

  • Create standard java.time types - 创建标准java.time类型
  • Create extended types (YearQuarter, YearWeek, Interval) - 创建扩展类型
  • Create durations and periods - 创建时长和周期
  • Create ranges - 创建范围

Usage Examples | 使用示例:

// Create dates
LocalDate date = DateTimes.date(2024, 1, 15);
LocalTime time = DateTimes.time(14, 30, 0);
LocalDateTime dateTime = DateTimes.dateTime(2024, 1, 15, 14, 30, 0);

// Create extended types
YearQuarter yq = DateTimes.yearQuarter(2024, 1);
YearWeek yw = DateTimes.yearWeek(2024, 1);

// Create durations
Duration d1 = DateTimes.hours(2);
Duration d2 = DateTimes.minutes(30);
Period p = DateTimes.months(3);

// Create ranges
LocalDateRange range = DateTimes.dateRange(
    LocalDate.of(2024, 1, 1),
    LocalDate.of(2024, 12, 31)
);

Performance | 性能特性:

  • All methods are static and lightweight - 所有方法都是静态和轻量级的
  • No caching overhead - 无缓存开销

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Immutable results: 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
    Creates a PeriodDuration between two LocalDateTimes 计算两个LocalDateTime之间的PeriodDuration
    Gets the current YearQuarter 获取当前YearQuarter
    static YearWeek
    Gets the current YearWeek 获取当前YearWeek
    static Year
    Gets the current Year 获取当前Year
    static YearMonth
    Gets the current YearMonth 获取当前YearMonth
    static LocalDate
    date(int year, int month, int dayOfMonth)
    Creates a LocalDate 创建LocalDate
    static LocalDate
    date(int year, Month month, int dayOfMonth)
    Creates a LocalDate using Month enum 使用Month枚举创建LocalDate
    Creates a LocalDateRange 创建LocalDateRange
    dateTime(int year, int month, int dayOfMonth, int hour, int minute)
    Creates a LocalDateTime 创建LocalDateTime
    dateTime(int year, int month, int dayOfMonth, int hour, int minute, int second)
    Creates a LocalDateTime with seconds 创建带秒的LocalDateTime
    Creates a LocalDateTime from date and time 从日期和时间创建LocalDateTime
    Creates a LocalDateTimeRange 创建LocalDateTimeRange
    static Duration
    days(long days)
    Creates a Duration of the specified days 创建指定天数的Duration
    static Duration
    hours(long hours)
    Creates a Duration of the specified hours 创建指定小时数的Duration
    static Instant
    Gets the current Instant 获取当前Instant
    static Instant
    instant(long epochMilli)
    Creates an Instant from epoch milliseconds 从毫秒时间戳创建Instant
    static Instant
    instant(long epochSecond, long nanoAdjust)
    Creates an Instant from epoch seconds 从秒时间戳创建Instant
    static Interval
    interval(Instant start, Duration duration)
    Creates an Interval from start and duration 从开始时刻和时长创建Interval
    static Interval
    interval(Instant start, Instant end)
    Creates an Interval 创建Interval
    static LocalTime
    Gets midnight (00:00:00) 获取午夜(00:00:00)
    static Duration
    millis(long millis)
    Creates a Duration of the specified milliseconds 创建指定毫秒数的Duration
    static Duration
    minutes(long minutes)
    Creates a Duration of the specified minutes 创建指定分钟数的Duration
    static Period
    months(int months)
    Creates a Period of the specified months 创建指定月数的Period
    static Duration
    nanos(long nanos)
    Creates a Duration of the specified nanoseconds 创建指定纳秒数的Duration
    static LocalTime
    Gets noon (12:00:00) 获取中午(12:00:00)
    now()
    Gets the current datetime 获取当前日期时间
    static Period
    period(int years, int months, int days)
    Creates a Period from years, months, and days 从年、月、日创建Period
    static Period
    periodDays(int days)
    Creates a Period of the specified days 创建指定天数的Period
    periodDuration(Period period, Duration duration)
    Creates a PeriodDuration from Period and Duration 从Period和Duration创建PeriodDuration
    static Quarter
    quarter(int quarter)
    Creates a Quarter 创建Quarter
    static Duration
    seconds(long seconds)
    Creates a Duration of the specified seconds 创建指定秒数的Duration
    static LocalTime
    time(int hour, int minute)
    Creates a LocalTime 创建LocalTime
    static LocalTime
    time(int hour, int minute, int second)
    Creates a LocalTime with seconds 创建带秒的LocalTime
    static LocalDate
    Gets today's date 获取今天的日期
    static LocalDate
    Gets tomorrow's date 获取明天的日期
    static Period
    weeks(int weeks)
    Creates a Period of the specified weeks 创建指定周数的Period
    static Year
    year(int year)
    Creates a Year 创建Year
    static YearMonth
    yearMonth(int year, int month)
    Creates a YearMonth 创建YearMonth
    yearQuarter(int year, int quarter)
    Creates a YearQuarter 创建YearQuarter
    yearQuarter(int year, Quarter quarter)
    Creates a YearQuarter with Quarter enum 使用Quarter枚举创建YearQuarter
    static Period
    years(int years)
    Creates a Period of the specified years 创建指定年数的Period
    static YearWeek
    yearWeek(int weekBasedYear, int week)
    Creates a YearWeek 创建YearWeek
    static LocalDate
    Gets yesterday's date 获取昨天的日期

    Methods inherited from class Object

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

    • date

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

      public static LocalDate date(int year, Month month, int dayOfMonth)
      Creates a LocalDate using Month enum 使用Month枚举创建LocalDate
      Parameters:
      year - the year | 年
      month - the month | 月
      dayOfMonth - the day | 日
      Returns:
      the LocalDate | LocalDate
    • today

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

      public static LocalDate yesterday()
      Gets yesterday's date 获取昨天的日期
      Returns:
      yesterday | 昨天
    • tomorrow

      public static LocalDate tomorrow()
      Gets tomorrow's date 获取明天的日期
      Returns:
      tomorrow | 明天
    • time

      public static LocalTime time(int hour, int minute)
      Creates a LocalTime 创建LocalTime
      Parameters:
      hour - the hour | 时
      minute - the minute | 分
      Returns:
      the LocalTime | LocalTime
    • time

      public static LocalTime time(int hour, int minute, int second)
      Creates a LocalTime with seconds 创建带秒的LocalTime
      Parameters:
      hour - the hour | 时
      minute - the minute | 分
      second - the second | 秒
      Returns:
      the LocalTime | LocalTime
    • midnight

      public static LocalTime midnight()
      Gets midnight (00:00:00) 获取午夜(00:00:00)
      Returns:
      midnight | 午夜
    • noon

      public static LocalTime noon()
      Gets noon (12:00:00) 获取中午(12:00:00)
      Returns:
      noon | 中午
    • dateTime

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

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

      public static LocalDateTime dateTime(LocalDate date, LocalTime time)
      Creates a LocalDateTime from date and time 从日期和时间创建LocalDateTime
      Parameters:
      date - the date | 日期
      time - the time | 时间
      Returns:
      the LocalDateTime | LocalDateTime
    • now

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

      public static YearMonth yearMonth(int year, int month)
      Creates a YearMonth 创建YearMonth
      Parameters:
      year - the year | 年
      month - the month | 月
      Returns:
      the YearMonth | YearMonth
    • year

      public static Year year(int year)
      Creates a Year 创建Year
      Parameters:
      year - the year | 年
      Returns:
      the Year | Year
    • currentYearMonth

      public static YearMonth currentYearMonth()
      Gets the current YearMonth 获取当前YearMonth
      Returns:
      the current YearMonth | 当前YearMonth
    • currentYear

      public static Year currentYear()
      Gets the current Year 获取当前Year
      Returns:
      the current Year | 当前Year
    • quarter

      public static Quarter quarter(int quarter)
      Creates a Quarter 创建Quarter
      Parameters:
      quarter - the quarter value (1-4) | 季度值(1-4)
      Returns:
      the Quarter | Quarter
    • yearQuarter

      public static YearQuarter yearQuarter(int year, int quarter)
      Creates a YearQuarter 创建YearQuarter
      Parameters:
      year - the year | 年
      quarter - the quarter (1-4) | 季度(1-4)
      Returns:
      the YearQuarter | YearQuarter
    • yearQuarter

      public static YearQuarter yearQuarter(int year, Quarter quarter)
      Creates a YearQuarter with Quarter enum 使用Quarter枚举创建YearQuarter
      Parameters:
      year - the year | 年
      quarter - the Quarter | Quarter枚举
      Returns:
      the YearQuarter | YearQuarter
    • currentQuarter

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

      public static YearWeek yearWeek(int weekBasedYear, int week)
      Creates a YearWeek 创建YearWeek
      Parameters:
      weekBasedYear - the week-based year | 周基准年
      week - the week (1-52/53) | 周(1-52/53)
      Returns:
      the YearWeek | YearWeek
    • currentWeek

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

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

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

      public static Instant instant(long epochSecond, long nanoAdjust)
      Creates an Instant from epoch seconds 从秒时间戳创建Instant
      Parameters:
      epochSecond - the epoch seconds | 秒时间戳
      nanoAdjust - the nanosecond adjustment | 纳秒调整
      Returns:
      the Instant | Instant
    • nanos

      public static Duration nanos(long nanos)
      Creates a Duration of the specified nanoseconds 创建指定纳秒数的Duration
      Parameters:
      nanos - the nanoseconds | 纳秒数
      Returns:
      the Duration | Duration
    • millis

      public static Duration millis(long millis)
      Creates a Duration of the specified milliseconds 创建指定毫秒数的Duration
      Parameters:
      millis - the milliseconds | 毫秒数
      Returns:
      the Duration | Duration
    • seconds

      public static Duration seconds(long seconds)
      Creates a Duration of the specified seconds 创建指定秒数的Duration
      Parameters:
      seconds - the seconds | 秒数
      Returns:
      the Duration | Duration
    • minutes

      public static Duration minutes(long minutes)
      Creates a Duration of the specified minutes 创建指定分钟数的Duration
      Parameters:
      minutes - the minutes | 分钟数
      Returns:
      the Duration | Duration
    • hours

      public static Duration hours(long hours)
      Creates a Duration of the specified hours 创建指定小时数的Duration
      Parameters:
      hours - the hours | 小时数
      Returns:
      the Duration | Duration
    • days

      public static Duration days(long days)
      Creates a Duration of the specified days 创建指定天数的Duration
      Parameters:
      days - the days | 天数
      Returns:
      the Duration | Duration
    • periodDays

      public static Period periodDays(int days)
      Creates a Period of the specified days 创建指定天数的Period
      Parameters:
      days - the days | 天数
      Returns:
      the Period | Period
    • weeks

      public static Period weeks(int weeks)
      Creates a Period of the specified weeks 创建指定周数的Period
      Parameters:
      weeks - the weeks | 周数
      Returns:
      the Period | Period
    • months

      public static Period months(int months)
      Creates a Period of the specified months 创建指定月数的Period
      Parameters:
      months - the months | 月数
      Returns:
      the Period | Period
    • years

      public static Period years(int years)
      Creates a Period of the specified years 创建指定年数的Period
      Parameters:
      years - the years | 年数
      Returns:
      the Period | Period
    • period

      public static Period period(int years, int months, int days)
      Creates a Period from years, months, and days 从年、月、日创建Period
      Parameters:
      years - the years | 年
      months - the months | 月
      days - the days | 日
      Returns:
      the Period | Period
    • dateRange

      public static LocalDateRange dateRange(LocalDate start, LocalDate end)
      Creates a LocalDateRange 创建LocalDateRange
      Parameters:
      start - the start date | 开始日期
      end - the end date | 结束日期
      Returns:
      the LocalDateRange | LocalDateRange
    • dateTimeRange

      public static LocalDateTimeRange dateTimeRange(LocalDateTime start, LocalDateTime end)
      Creates a LocalDateTimeRange 创建LocalDateTimeRange
      Parameters:
      start - the start datetime | 开始日期时间
      end - the end datetime | 结束日期时间
      Returns:
      the LocalDateTimeRange | LocalDateTimeRange
    • interval

      public static Interval interval(Instant start, Instant end)
      Creates an Interval 创建Interval
      Parameters:
      start - the start instant | 开始时刻
      end - the end instant | 结束时刻
      Returns:
      the Interval | Interval
    • interval

      public static Interval interval(Instant start, Duration duration)
      Creates an Interval from start and duration 从开始时刻和时长创建Interval
      Parameters:
      start - the start instant | 开始时刻
      duration - the duration | 时长
      Returns:
      the Interval | Interval
    • periodDuration

      public static PeriodDuration periodDuration(Period period, Duration duration)
      Creates a PeriodDuration from Period and Duration 从Period和Duration创建PeriodDuration
      Parameters:
      period - the period | 周期
      duration - the duration | 时长
      Returns:
      the PeriodDuration | PeriodDuration
    • between

      public static PeriodDuration between(LocalDateTime start, LocalDateTime end)
      Creates a PeriodDuration between two LocalDateTimes 计算两个LocalDateTime之间的PeriodDuration
      Parameters:
      start - the start | 开始
      end - the end | 结束
      Returns:
      the PeriodDuration | PeriodDuration