Class DateTimes
java.lang.Object
cloud.opencode.base.date.DateTimes
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 TypeMethodDescriptionstatic PeriodDurationbetween(LocalDateTime start, LocalDateTime end) Creates a PeriodDuration between two LocalDateTimes 计算两个LocalDateTime之间的PeriodDurationstatic YearQuarterGets the current YearQuarter 获取当前YearQuarterstatic YearWeekGets the current YearWeek 获取当前YearWeekstatic YearGets the current Year 获取当前Yearstatic YearMonthGets the current YearMonth 获取当前YearMonthstatic LocalDatedate(int year, int month, int dayOfMonth) Creates a LocalDate 创建LocalDatestatic LocalDateCreates a LocalDate using Month enum 使用Month枚举创建LocalDatestatic LocalDateRangeCreates a LocalDateRange 创建LocalDateRangestatic LocalDateTimedateTime(int year, int month, int dayOfMonth, int hour, int minute) Creates a LocalDateTime 创建LocalDateTimestatic LocalDateTimedateTime(int year, int month, int dayOfMonth, int hour, int minute, int second) Creates a LocalDateTime with seconds 创建带秒的LocalDateTimestatic LocalDateTimeCreates a LocalDateTime from date and time 从日期和时间创建LocalDateTimestatic LocalDateTimeRangedateTimeRange(LocalDateTime start, LocalDateTime end) Creates a LocalDateTimeRange 创建LocalDateTimeRangestatic Durationdays(long days) Creates a Duration of the specified days 创建指定天数的Durationstatic Durationhours(long hours) Creates a Duration of the specified hours 创建指定小时数的Durationstatic Instantinstant()Gets the current Instant 获取当前Instantstatic Instantinstant(long epochMilli) Creates an Instant from epoch milliseconds 从毫秒时间戳创建Instantstatic Instantinstant(long epochSecond, long nanoAdjust) Creates an Instant from epoch seconds 从秒时间戳创建Instantstatic IntervalCreates an Interval from start and duration 从开始时刻和时长创建Intervalstatic IntervalCreates an Interval 创建Intervalstatic LocalTimemidnight()Gets midnight (00:00:00) 获取午夜(00:00:00)static Durationmillis(long millis) Creates a Duration of the specified milliseconds 创建指定毫秒数的Durationstatic Durationminutes(long minutes) Creates a Duration of the specified minutes 创建指定分钟数的Durationstatic Periodmonths(int months) Creates a Period of the specified months 创建指定月数的Periodstatic Durationnanos(long nanos) Creates a Duration of the specified nanoseconds 创建指定纳秒数的Durationstatic LocalTimenoon()Gets noon (12:00:00) 获取中午(12:00:00)static LocalDateTimenow()Gets the current datetime 获取当前日期时间static Periodperiod(int years, int months, int days) Creates a Period from years, months, and days 从年、月、日创建Periodstatic PeriodperiodDays(int days) Creates a Period of the specified days 创建指定天数的Periodstatic PeriodDurationperiodDuration(Period period, Duration duration) Creates a PeriodDuration from Period and Duration 从Period和Duration创建PeriodDurationstatic Quarterquarter(int quarter) Creates a Quarter 创建Quarterstatic Durationseconds(long seconds) Creates a Duration of the specified seconds 创建指定秒数的Durationstatic LocalTimetime(int hour, int minute) Creates a LocalTime 创建LocalTimestatic LocalTimetime(int hour, int minute, int second) Creates a LocalTime with seconds 创建带秒的LocalTimestatic LocalDatetoday()Gets today's date 获取今天的日期static LocalDatetomorrow()Gets tomorrow's date 获取明天的日期static Periodweeks(int weeks) Creates a Period of the specified weeks 创建指定周数的Periodstatic Yearyear(int year) Creates a Year 创建Yearstatic YearMonthyearMonth(int year, int month) Creates a YearMonth 创建YearMonthstatic YearQuarteryearQuarter(int year, int quarter) Creates a YearQuarter 创建YearQuarterstatic YearQuarteryearQuarter(int year, Quarter quarter) Creates a YearQuarter with Quarter enum 使用Quarter枚举创建YearQuarterstatic Periodyears(int years) Creates a Period of the specified years 创建指定年数的Periodstatic YearWeekyearWeek(int weekBasedYear, int week) Creates a YearWeek 创建YearWeekstatic LocalDateGets yesterday's date 获取昨天的日期
-
Method Details
-
date
Creates a LocalDate 创建LocalDate- Parameters:
year- the year | 年month- the month | 月dayOfMonth- the day | 日- Returns:
- the LocalDate | LocalDate
-
date
-
today
-
yesterday
-
tomorrow
-
time
Creates a LocalTime 创建LocalTime- Parameters:
hour- the hour | 时minute- the minute | 分- Returns:
- the LocalTime | LocalTime
-
time
Creates a LocalTime with seconds 创建带秒的LocalTime- Parameters:
hour- the hour | 时minute- the minute | 分second- the second | 秒- Returns:
- the LocalTime | LocalTime
-
midnight
-
noon
-
dateTime
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
Creates a LocalDateTime from date and time 从日期和时间创建LocalDateTime- Parameters:
date- the date | 日期time- the time | 时间- Returns:
- the LocalDateTime | LocalDateTime
-
now
-
yearMonth
Creates a YearMonth 创建YearMonth- Parameters:
year- the year | 年month- the month | 月- Returns:
- the YearMonth | YearMonth
-
year
Creates a Year 创建Year- Parameters:
year- the year | 年- Returns:
- the Year | Year
-
currentYearMonth
Gets the current YearMonth 获取当前YearMonth- Returns:
- the current YearMonth | 当前YearMonth
-
currentYear
-
quarter
Creates a Quarter 创建Quarter- Parameters:
quarter- the quarter value (1-4) | 季度值(1-4)- Returns:
- the Quarter | Quarter
-
yearQuarter
Creates a YearQuarter 创建YearQuarter- Parameters:
year- the year | 年quarter- the quarter (1-4) | 季度(1-4)- Returns:
- the YearQuarter | YearQuarter
-
yearQuarter
Creates a YearQuarter with Quarter enum 使用Quarter枚举创建YearQuarter- Parameters:
year- the year | 年quarter- the Quarter | Quarter枚举- Returns:
- the YearQuarter | YearQuarter
-
currentQuarter
Gets the current YearQuarter 获取当前YearQuarter- Returns:
- the current YearQuarter | 当前YearQuarter
-
yearWeek
Creates a YearWeek 创建YearWeek- Parameters:
weekBasedYear- the week-based year | 周基准年week- the week (1-52/53) | 周(1-52/53)- Returns:
- the YearWeek | YearWeek
-
currentWeek
Gets the current YearWeek 获取当前YearWeek- Returns:
- the current YearWeek | 当前YearWeek
-
instant
Gets the current Instant 获取当前Instant- Returns:
- the current Instant | 当前Instant
-
instant
Creates an Instant from epoch milliseconds 从毫秒时间戳创建Instant- Parameters:
epochMilli- the epoch milliseconds | 毫秒时间戳- Returns:
- the Instant | Instant
-
instant
Creates an Instant from epoch seconds 从秒时间戳创建Instant- Parameters:
epochSecond- the epoch seconds | 秒时间戳nanoAdjust- the nanosecond adjustment | 纳秒调整- Returns:
- the Instant | Instant
-
nanos
Creates a Duration of the specified nanoseconds 创建指定纳秒数的Duration- Parameters:
nanos- the nanoseconds | 纳秒数- Returns:
- the Duration | Duration
-
millis
Creates a Duration of the specified milliseconds 创建指定毫秒数的Duration- Parameters:
millis- the milliseconds | 毫秒数- Returns:
- the Duration | Duration
-
seconds
Creates a Duration of the specified seconds 创建指定秒数的Duration- Parameters:
seconds- the seconds | 秒数- Returns:
- the Duration | Duration
-
minutes
Creates a Duration of the specified minutes 创建指定分钟数的Duration- Parameters:
minutes- the minutes | 分钟数- Returns:
- the Duration | Duration
-
hours
Creates a Duration of the specified hours 创建指定小时数的Duration- Parameters:
hours- the hours | 小时数- Returns:
- the Duration | Duration
-
days
Creates a Duration of the specified days 创建指定天数的Duration- Parameters:
days- the days | 天数- Returns:
- the Duration | Duration
-
periodDays
Creates a Period of the specified days 创建指定天数的Period- Parameters:
days- the days | 天数- Returns:
- the Period | Period
-
weeks
Creates a Period of the specified weeks 创建指定周数的Period- Parameters:
weeks- the weeks | 周数- Returns:
- the Period | Period
-
months
Creates a Period of the specified months 创建指定月数的Period- Parameters:
months- the months | 月数- Returns:
- the Period | Period
-
years
Creates a Period of the specified years 创建指定年数的Period- Parameters:
years- the years | 年数- Returns:
- the Period | Period
-
period
Creates a Period from years, months, and days 从年、月、日创建Period- Parameters:
years- the years | 年months- the months | 月days- the days | 日- Returns:
- the Period | Period
-
dateRange
Creates a LocalDateRange 创建LocalDateRange- Parameters:
start- the start date | 开始日期end- the end date | 结束日期- Returns:
- the LocalDateRange | LocalDateRange
-
dateTimeRange
Creates a LocalDateTimeRange 创建LocalDateTimeRange- Parameters:
start- the start datetime | 开始日期时间end- the end datetime | 结束日期时间- Returns:
- the LocalDateTimeRange | LocalDateTimeRange
-
interval
-
interval
-
periodDuration
Creates a PeriodDuration from Period and Duration 从Period和Duration创建PeriodDuration- Parameters:
period- the period | 周期duration- the duration | 时长- Returns:
- the PeriodDuration | PeriodDuration
-
between
Creates a PeriodDuration between two LocalDateTimes 计算两个LocalDateTime之间的PeriodDuration- Parameters:
start- the start | 开始end- the end | 结束- Returns:
- the PeriodDuration | PeriodDuration
-