Class DateUtil
java.lang.Object
com.sure.tool.date.DateUtil
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic int计算周岁年龄。static int计算周岁年龄(指定参考日期)。static DatebeginOfDay(Date date) 当天开始时间(00:00:00.000)。static DatebeginOfHour(Date date) 当前小时开始时间。static DatebeginOfMinute(Date date) 获取某时刻所在分钟的开始(秒和毫秒归零)。static DatebeginOfMonth(Date date) 当月开始时间(1 日 00:00:00.000)。static DatebeginOfQuarter(Date date) 获取季度开始时间(当季第 1 天 00:00:00)。static DatebeginOfSecond(Date date) 获取某时刻所在秒的开始(毫秒归零)。static DatebeginOfWeek(Date date) 一天开始(周一为一周第一天,00:00:00.000)。static DatebeginOfYear(Date date) 年初(1 月 1 日 00:00:00.000)。static long两个日期之间的时间差。static long两个日期的毫秒差(end - start)。static longbetweenSeconds(Date start, Date end) 两个日期的秒差(end - start,向下取整)。static Date进位到指定Calendar字段(字段本身保留,更小字段清零并向上进位)。static longcurrent()当前毫秒时间戳。static long当前秒级时间戳。static Datedate(long timeMillis) 时间戳转 Date。static DateCalendar 转 Date。static int获取日(1-31)。static intdayOfMonth(Date date) 当月第几天(1~31)。static int获取星期几(1=周日,7=周六)。static int当年第几天(1~366)。static longdaysBetween(Date start, Date end) 两个日期的自然日天数差(忽略时分秒)。static Date当天结束时间(23:59:59.999)。static Date当前小时结束时间。static DateendOfMinute(Date date) 获取某时刻所在分钟的结束(秒 59、毫秒 999)。static DateendOfMonth(Date date) 当月结束时间(最后一天 23:59:59.999)。static DateendOfQuarter(Date date) 获取季度结束时间(当季最后 1 天 23:59:59)。static DateendOfSecond(Date date) 获取某时刻所在秒的结束(毫秒 999)。static Date一周结束(周日 23:59:59.999)。static Date年末(12 月 31 日 23:59:59.999)。static String格式化 LocalDate(yyyy-MM-dd)。static Stringformat(LocalDateTime dateTime) 格式化 LocalDateTime(yyyy-MM-dd HH:mm:ss)。static String格式化日期时间(yyyy-MM-dd HH:mm:ss)。static String按格式格式化日期。static StringformatBetween(Date start, Date end) 两时间差的可读文本(x天x小时x分x秒)。static StringformatChineseDate(Date date) 中文格式日期(2026年9月16日)。static StringformatChineseDateTime(Date date) 中文格式日期时间(2026年9月16日 15:30:45)。static StringformatDate(Date date) 格式化日期(yyyy-MM-dd)。static StringformatTime(Date date) 格式化时间(HH:mm:ss)。static int计算周岁年龄。static StringgetChineseZodiac(int year) 获取农历生肖(按公历年,1900 年为鼠年)。static intgetDayOfMonth(Date date) 获取日(1-31)。static String年份天干地支(如 2026 → 丙午)。static int获取小时(0-23)。static int获取月份(1-12)。static StringgetMonthName(int month) 获取月份中文名(一月~十二月)。static StringgetMonthName(Date date) 获取日期所在月份的中文名。static intgetWeekOfMonth(Date date) 日期在当月中的第几周。static intgetWeekOfYear(Date date) 获取一年中的第几周。static int获取年份。static String获取星座。static int小时(0~23)。static boolean是否在时间区间内(含边界)。static booleanisLeapYear(int year) 是否为闰年。static boolean判断两个时间段是否重叠(含边界相接,start < end为前置条件)。static boolean是否为同一天。static booleanisSameMonth(Date date1, Date date2) 判断两个日期是否同一个月。static booleanisSameWeek(Date date1, Date date2) 判断两个日期是否在同一周(ISO 周定义:周一为一周首日)。static booleanisSameYear(Date date1, Date date2) 判断两个日期是否同年。static boolean是否今天。static boolean是否为周末(周六/周日)。static booleanisYesterday(Date date) 是否昨天。static int分钟(0~59)。static int获取月份(1-12)。static Datenow()当前时间。static Date日期偏移。static Date天偏移。static DateoffsetHour(Date date, int offset) 小时偏移。static DateoffsetMinute(Date date, int offset) 分钟偏移。static DateoffsetMonth(Date date, int offset) 按月偏移。static DateoffsetQuarter(Date date, int offset) 偏移季度。static DateoffsetSecond(Date date, int offset) 按秒偏移。static DateoffsetWeek(Date date, int offset) 按周偏移。static DateoffsetYear(Date date, int offset) 按年偏移。static Date自动识别解析日期,支持: yyyy-MM-dd HH:mm:ss yyyy-MM-dd HH:mm:ss yyyy-MM-dd'T'HH:mm:ssstatic Date按格式解析日期。static LocalDateparseLocalDate(String dateStr) 解析 ISO 日期字符串(yyyy-MM-dd)为LocalDate。static LocalDateTimeparseLocalDateTime(String dateTimeStr) 解析常见日期时间字符串为LocalDateTime(自动探测格式)。static LocalDateTimeparseLocalDateTime(String dateTimeStr, String pattern) 按指定格式解析日期时间字符串为LocalDateTime。rangeToList(Date start, Date end, DateUnit unit) 生成日期区间列表(含头含尾,按指定单位步进)。static Date四舍五入到指定Calendar字段。static int季度(1~4)。static int秒(0~59)。static CalendartoCalendar(Date date) Date 转 Calendar。static DateLocalDate 转Date(系统时区当日 00:00)。static DatetoDate(LocalDateTime localDateTime) LocalDateTime 转Date(系统默认时区)。static DateCalendar 转 Date。static LocalDatetoLocalDate(Date date) 转换为LocalDate(系统默认时区)。static LocalDateTimetoLocalDateTime(Date date) 转换为LocalDateTime(系统默认时区)。static Datetomorrow()明天。static Date截断到指定Calendar字段(该字段保留,更小字段清零)。static int获取年份。static Date昨天。
-
Field Details
-
NORM_DATE_PATTERN
-
NORM_TIME_PATTERN
-
NORM_DATETIME_PATTERN
-
ISO_DATETIME_PATTERN
-
-
Method Details
-
now
-
current
public static long current()当前毫秒时间戳。- Returns:
- 当前毫秒
-
currentSeconds
public static long currentSeconds()当前秒级时间戳。- Returns:
- 当前秒
-
date
-
date
-
format
-
format
-
formatDate
-
formatTime
-
parse
-
parse
-
offset
日期偏移。- Parameters:
date- 日期datePart- 偏移字段(Calendar常量,如Calendar.DAY_OF_MONTH)offset- 偏移量,负数向前- Returns:
- 偏移后的日期
-
offsetDay
-
offsetHour
-
offsetMinute
-
yesterday
-
tomorrow
-
beginOfDay
-
endOfDay
-
beginOfMonth
-
endOfMonth
-
between
-
age
-
year
-
month
-
day
-
dayOfWeek
-
isSameDay
-
isLeapYear
public static boolean isLeapYear(int year) 是否为闰年。- Parameters:
year- 年份- Returns:
- 是否为闰年
-
toCalendar
-
getZodiac
-
getChineseZodiac
获取农历生肖(按公历年,1900 年为鼠年)。- Parameters:
year- 公历年份- Returns:
- 生肖
-
beginOfWeek
-
endOfWeek
-
formatChineseDateTime
-
dayOfMonth
-
hour
-
season
-
formatChineseDate
-
dayOfYear
-
minute
-
second
-
formatBetween
-
isWeekend
-
offsetWeek
-
beginOfYear
-
endOfYear
-
offsetMonth
-
getGanzhi
-
offsetSecond
-
offsetYear
-
isIn
-
isToday
-
isYesterday
-
beginOfQuarter
-
endOfQuarter
-
age
-
isSameMonth
-
toDate
-
beginOfHour
-
endOfHour
-
daysBetween
-
betweenMs
-
betweenSeconds
-
isOverlap
-
rangeToList
-
getWeekOfMonth
-
isSameYear
-
getMonthName
获取月份中文名(一月~十二月)。- Parameters:
month- 月份(1-12)- Returns:
- 中文月份名
-
getMonthName
-
offsetQuarter
-
getYear
-
getMonth
-
getDayOfMonth
-
getHour
-
getWeekOfYear
-
format
-
format
格式化 LocalDateTime(yyyy-MM-dd HH:mm:ss)。- Parameters:
dateTime- 日期时间- Returns:
- 格式化字符串
-
beginOfMinute
-
endOfMinute
-
getAge
-
beginOfSecond
-
endOfSecond
-
toLocalDate
-
toLocalDateTime
转换为LocalDateTime(系统默认时区)。示例:
DateUtil.toLocalDateTime(new Date())。- Parameters:
date- 日期,可为null- Returns:
- LocalDateTime;
null返回null
-
toDate
-
toDate
LocalDateTime 转Date(系统默认时区)。示例:
DateUtil.toDate(LocalDateTime.now())。- Parameters:
localDateTime- LocalDateTime,可为null- Returns:
- Date;
null返回null
-
parseLocalDate
-
parseLocalDateTime
解析常见日期时间字符串为LocalDateTime(自动探测格式)。支持格式:
yyyy-MM-dd HH:mm:ss、yyyy-MM-dd HH:mm、yyyy-MM-dd HH:mm:ss.SSS、 ISO 日期时间等;纯日期按当日 00:00 处理。- Parameters:
dateTimeStr- 日期时间字符串,可为null- Returns:
- LocalDateTime;
null或空串返回null
-
parseLocalDateTime
按指定格式解析日期时间字符串为LocalDateTime。示例:
DateUtil.parseLocalDateTime("2026/10/15 08:30", "yyyy/MM/dd HH:mm")。- Parameters:
dateTimeStr- 日期时间字符串,可为nullpattern- 格式模式(DateTimeFormatter语法)- Returns:
- LocalDateTime;
null或空串返回null
-
isSameWeek
-
truncate
截断到指定Calendar字段(该字段保留,更小字段清零)。对齐 Commons
DateUtils.truncate:示例DateUtil.truncate(date, Calendar.DAY_OF_MONTH)等价于beginOfDay(date)。- Parameters:
date- 日期,可为nullfield-Calendar字段常量(如Calendar.HOUR_OF_DAY/Calendar.DAY_OF_MONTH/Calendar.MONTH)- Returns:
- 截断后的日期;
null返回null
-
round
-
ceiling
-