Class OpenDate
java.lang.Object
cloud.opencode.base.date.OpenDate
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 Summary
Modifier and TypeMethodDescriptionstatic YearQuarterGets the current YearQuarter 获取当前YearQuarterstatic longGets the current time in milliseconds 获取当前时间戳(毫秒)static longGets the current time in seconds 获取当前时间戳(秒)static YearWeekGets the current YearWeek 获取当前YearWeekstatic longdaysBetween(LocalDate start, LocalDate end) Gets the number of days between two dates 获取两个日期之间的天数static DurationdurationBetween(LocalDateTime start, LocalDateTime end) Gets the Duration between two datetimes 获取两个日期时间之间的Durationstatic LocalDateendOfMonth(LocalDate date) Gets the last day of the month 获取月份的最后一天static LocalDateendOfQuarter(LocalDate date) Gets the last day of the quarter 获取季度的最后一天static LocalDateGets the last day of the week (Sunday) 获取周的最后一天(周日)static LocalDateGets the last day of the year 获取年份的最后一天static StringFormats a temporal to the default format (yyyy-MM-dd HH:mm:ss) 将时间对象格式化为默认格式(yyyy-MM-dd HH:mm:ss)static StringFormats a temporal to the specified pattern 将时间对象格式化为指定模式static StringformatChinese(Temporal temporal) Formats a temporal to Chinese format 将时间对象格式化为中文格式static StringFormats a temporal to ISO format 将时间对象格式化为ISO格式static LocalDateTimeConverts a Calendar to LocalDateTime 将Calendar转换为LocalDateTimestatic LocalDateTimeConverts a Date to LocalDateTime 将Date转换为LocalDateTimestatic Instantinstant()Gets the current Instant 获取当前Instantstatic booleanChecks if a date is between two dates (inclusive) 检查日期是否在两个日期之间(包含)static booleanisBetween(LocalDateTime dateTime, LocalDateTime start, LocalDateTime end) Checks if a datetime is between two datetimes (inclusive) 检查日期时间是否在两个日期时间之间(包含)static booleanisLeapYear(int year) Checks if a year is a leap year 检查年份是否为闰年static booleanChecks if a date is today 检查日期是否为今天static booleanisTomorrow(LocalDate date) Checks if a date is tomorrow 检查日期是否为明天static booleanChecks if a date is a weekend (Saturday or Sunday) 检查日期是否为周末(周六或周日)static booleanisYesterday(LocalDate date) Checks if a date is yesterday 检查日期是否为昨天static longmonthsBetween(LocalDate start, LocalDate end) Gets the number of months between two dates 获取两个日期之间的月数static LocalDateTimenow()Gets the current datetime 获取当前日期时间static LocalDateTimeGets the current datetime for the specified zone 获取指定时区的当前日期时间static LocalDateof(int year, int month, int dayOfMonth) Creates a LocalDate 创建LocalDatestatic LocalDateTimeof(int year, int month, int dayOfMonth, int hour, int minute, int second) Creates a LocalDateTime 创建LocalDateTimestatic LocalDateTimeofEpochMilli(long epochMilli) Creates a LocalDateTime from epoch milliseconds 从毫秒时间戳创建LocalDateTimestatic LocalDateTimeofEpochSecond(long epochSecond) Creates a LocalDateTime from epoch seconds 从秒时间戳创建LocalDateTimestatic LocalDateTimeParses a string to LocalDateTime using smart format detection 使用智能格式检测将字符串解析为LocalDateTimestatic LocalDateTimeParses a string to LocalDateTime using the specified pattern 使用指定模式将字符串解析为LocalDateTimestatic LocalDateParses a string to LocalDate using smart format detection 使用智能格式检测将字符串解析为LocalDatestatic LocalTimeParses a string to LocalTime using smart format detection 使用智能格式检测将字符串解析为LocalTimestatic PeriodperiodBetween(LocalDate start, LocalDate end) Gets the Period between two dates 获取两个日期之间的Periodstatic LocalDateTimeplusDays(LocalDateTime dateTime, long days) Adds days to a LocalDateTime 在LocalDateTime上加天static LocalDateTimeplusHours(LocalDateTime dateTime, long hours) Adds hours to a LocalDateTime 在LocalDateTime上加小时static LocalDateTimeplusMinutes(LocalDateTime dateTime, long minutes) Adds minutes to a LocalDateTime 在LocalDateTime上加分钟static LocalDateTimeplusMonths(LocalDateTime dateTime, long months) Adds months to a LocalDateTime 在LocalDateTime上加月static LocalDateTimeplusSeconds(LocalDateTime dateTime, long seconds) Adds seconds to a LocalDateTime 在LocalDateTime上加秒static LocalDateTimeplusWeeks(LocalDateTime dateTime, long weeks) Adds weeks to a LocalDateTime 在LocalDateTime上加周static LocalDateTimeplusYears(LocalDateTime dateTime, long years) Adds years to a LocalDateTime 在LocalDateTime上加年static LocalDateRangeCreates a LocalDateRange from start to end 创建从开始到结束的LocalDateRangestatic LocalDatestartOfMonth(LocalDate date) Gets the first day of the month 获取月份的第一天static LocalDatestartOfQuarter(LocalDate date) Gets the first day of the quarter 获取季度的第一天static LocalDatestartOfWeek(LocalDate date) Gets the first day of the week (Monday) 获取周的第一天(周一)static LocalDatestartOfYear(LocalDate date) Gets the first day of the year 获取年份的第一天static DateConverts a LocalDate to java.util.Date 将LocalDate转换为java.util.Datestatic DatetoDate(LocalDateTime dateTime) Converts a LocalDateTime to java.util.Date 将LocalDateTime转换为java.util.Datestatic LocalDatetoday()Gets the current date 获取当前日期static LocalDateGets the current date for the specified zone 获取指定时区的当前日期static longtoEpochMilli(LocalDateTime dateTime) Converts a LocalDateTime to epoch milliseconds 将LocalDateTime转换为毫秒时间戳static longtoEpochSecond(LocalDateTime dateTime) Converts a LocalDateTime to epoch seconds 将LocalDateTime转换为秒时间戳static <T extends Temporal>
Ttruncate(T temporal, TemporalUnit unit) Truncates a temporal to the specified unit 将时间对象截断到指定单位static LocalDateTimetruncateToDay(LocalDateTime dateTime) Truncates a LocalDateTime to the start of day 将LocalDateTime截断到当天开始static LocalDateTimetruncateToHour(LocalDateTime dateTime) Truncates a LocalDateTime to the start of hour 将LocalDateTime截断到小时开始static LocalDateTimetruncateToMinute(LocalDateTime dateTime) Truncates a LocalDateTime to the start of minute 将LocalDateTime截断到分钟开始static longyearsBetween(LocalDate start, LocalDate end) Gets the number of years between two dates 获取两个日期之间的年数
-
Method Details
-
today
-
today
-
now
Gets the current datetime 获取当前日期时间- Returns:
- the current datetime | 当前日期时间
-
now
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
Gets the current Instant 获取当前Instant- Returns:
- the current Instant | 当前Instant
-
of
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
Creates a LocalDateTime from epoch milliseconds 从毫秒时间戳创建LocalDateTime- Parameters:
epochMilli- the epoch milliseconds | 毫秒时间戳- Returns:
- the LocalDateTime | LocalDateTime
-
ofEpochSecond
Creates a LocalDateTime from epoch seconds 从秒时间戳创建LocalDateTime- Parameters:
epochSecond- the epoch seconds | 秒时间戳- Returns:
- the LocalDateTime | LocalDateTime
-
from
Converts a Date to LocalDateTime 将Date转换为LocalDateTime- Parameters:
date- the Date | Date对象- Returns:
- the LocalDateTime | LocalDateTime
-
from
Converts a Calendar to LocalDateTime 将Calendar转换为LocalDateTime- Parameters:
calendar- the Calendar | Calendar对象- Returns:
- the LocalDateTime | LocalDateTime
-
parse
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
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
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
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
-
format
-
formatIso
-
formatChinese
-
truncate
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
Truncates a LocalDateTime to the start of day 将LocalDateTime截断到当天开始- Parameters:
dateTime- the datetime | 日期时间- Returns:
- the truncated datetime | 截断后的日期时间
-
truncateToHour
Truncates a LocalDateTime to the start of hour 将LocalDateTime截断到小时开始- Parameters:
dateTime- the datetime | 日期时间- Returns:
- the truncated datetime | 截断后的日期时间
-
truncateToMinute
Truncates a LocalDateTime to the start of minute 将LocalDateTime截断到分钟开始- Parameters:
dateTime- the datetime | 日期时间- Returns:
- the truncated datetime | 截断后的日期时间
-
plusYears
Adds years to a LocalDateTime 在LocalDateTime上加年- Parameters:
dateTime- the datetime | 日期时间years- the years to add | 要加的年数- Returns:
- the result | 结果
-
plusMonths
Adds months to a LocalDateTime 在LocalDateTime上加月- Parameters:
dateTime- the datetime | 日期时间months- the months to add | 要加的月数- Returns:
- the result | 结果
-
plusWeeks
Adds weeks to a LocalDateTime 在LocalDateTime上加周- Parameters:
dateTime- the datetime | 日期时间weeks- the weeks to add | 要加的周数- Returns:
- the result | 结果
-
plusDays
Adds days to a LocalDateTime 在LocalDateTime上加天- Parameters:
dateTime- the datetime | 日期时间days- the days to add | 要加的天数- Returns:
- the result | 结果
-
plusHours
Adds hours to a LocalDateTime 在LocalDateTime上加小时- Parameters:
dateTime- the datetime | 日期时间hours- the hours to add | 要加的小时数- Returns:
- the result | 结果
-
plusMinutes
Adds minutes to a LocalDateTime 在LocalDateTime上加分钟- Parameters:
dateTime- the datetime | 日期时间minutes- the minutes to add | 要加的分钟数- Returns:
- the result | 结果
-
plusSeconds
Adds seconds to a LocalDateTime 在LocalDateTime上加秒- Parameters:
dateTime- the datetime | 日期时间seconds- the seconds to add | 要加的秒数- Returns:
- the result | 结果
-
startOfMonth
-
endOfMonth
-
startOfYear
-
endOfYear
-
startOfWeek
-
endOfWeek
-
startOfQuarter
-
endOfQuarter
-
isBetween
-
isBetween
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
Checks if a date is today 检查日期是否为今天- Parameters:
date- the date | 日期- Returns:
- true if today | 如果是今天返回true
-
isYesterday
Checks if a date is yesterday 检查日期是否为昨天- Parameters:
date- the date | 日期- Returns:
- true if yesterday | 如果是昨天返回true
-
isTomorrow
Checks if a date is tomorrow 检查日期是否为明天- Parameters:
date- the date | 日期- Returns:
- true if tomorrow | 如果是明天返回true
-
isWeekend
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
-
monthsBetween
-
yearsBetween
-
periodBetween
-
durationBetween
Gets the Duration between two datetimes 获取两个日期时间之间的Duration- Parameters:
start- the start datetime | 开始日期时间end- the end datetime | 结束日期时间- Returns:
- the Duration | Duration
-
toEpochMilli
Converts a LocalDateTime to epoch milliseconds 将LocalDateTime转换为毫秒时间戳- Parameters:
dateTime- the datetime | 日期时间- Returns:
- the epoch milliseconds | 毫秒时间戳
-
toEpochSecond
Converts a LocalDateTime to epoch seconds 将LocalDateTime转换为秒时间戳- Parameters:
dateTime- the datetime | 日期时间- Returns:
- the epoch seconds | 秒时间戳
-
toDate
Converts a LocalDateTime to java.util.Date 将LocalDateTime转换为java.util.Date- Parameters:
dateTime- the datetime | 日期时间- Returns:
- the Date | Date
-
toDate
-
range
Creates a LocalDateRange from start to end 创建从开始到结束的LocalDateRange- Parameters:
start- the start date | 开始日期end- the end date | 结束日期- Returns:
- the LocalDateRange | LocalDateRange
-
currentQuarter
Gets the current YearQuarter 获取当前YearQuarter- Returns:
- the current YearQuarter | 当前YearQuarter
-
currentWeek
Gets the current YearWeek 获取当前YearWeek- Returns:
- the current YearWeek | 当前YearWeek
-