Class DateKit


public class DateKit extends Calendar
日期时间工具类
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • DateKit

      public DateKit()
  • Method Details

    • isAM

      public static boolean isAM(Date date)
      是否为上午
      Parameters:
      date - 日期
      Returns:
      是否为上午
    • isPM

      public static boolean isPM(Date date)
      是否为下午
      Parameters:
      date - 日期
      Returns:
      是否为下午
    • isSameTime

      public static boolean isSameTime(Date date1, Date date2)
      是否为相同时间 此方法比较两个日期的时间戳是否相同
      Parameters:
      date1 - 日期1
      date2 - 日期2
      Returns:
      是否为相同时间
    • isSameDay

      public static boolean isSameDay(Date date1, Date date2)
      比较两个日期是否为同一天
      Parameters:
      date1 - 日期1
      date2 - 日期2
      Returns:
      是否为同一天
    • isSameWeek

      public static boolean isSameWeek(Date date1, Date date2, boolean isMon)
      比较两个日期是否为同一周
      Parameters:
      date1 - 日期1
      date2 - 日期2
      isMon - 是否为周一。国内第一天为星期一,国外第一天为星期日
      Returns:
      是否为同一周
    • isSameMonth

      public static boolean isSameMonth(Date date1, Date date2)
      比较两个日期是否为同一月
      Parameters:
      date1 - 日期1
      date2 - 日期2
      Returns:
      是否为同一月
    • isIn

      public static boolean isIn(Date date, Date beginDate, Date endDate)
      当前日期是否在日期指定范围内 起始日期和结束日期可以互换
      Parameters:
      date - 被检查的日期
      beginDate - 起始日期(包含)
      endDate - 结束日期(包含)
      Returns:
      是否在范围内
    • isIn

      public static boolean isIn(Date date, Date beginDate, Date endDate, boolean includeBegin, boolean includeEnd)
      当前日期是否在日期指定范围内 起始日期和结束日期可以互换 通过includeBegin, includeEnd参数控制日期范围区间是否为开区间,例如:传入参数:includeBegin=true, includeEnd=false, 则本方法会判断 date ∈ (beginDate, endDate] 是否成立
      Parameters:
      date - 被检查的日期
      beginDate - 起始日期
      endDate - 结束日期
      includeBegin - 时间范围是否包含起始日期
      includeEnd - 时间范围是否包含结束日期
      Returns:
      是否在范围内
    • now

      public static DateTime now()
      当前时间,转换为DateTime对象
      Returns:
      当前时间
    • today

      public static DateTime today()
      当天开始的时间,结果类似:2022-10-26 00:00:00
      Returns:
      当天开始的时间
    • dateSecond

      public static DateTime dateSecond()
      当前时间,转换为DateTime对象,忽略毫秒部分
      Returns:
      当前时间
    • date

      public static DateTime date(Date date)
      Date类型时间转为DateTime 如果date本身为DateTime对象,则返回强转后的对象,否则新建一个DateTime对象
      Parameters:
      date - Date,如果传入null,返回null
      Returns:
      时间对象
    • date

      public static DateTime date(XMLGregorianCalendar date)
      Parameters:
      date - XMLGregorianCalendar,如果传入null,返回null
      Returns:
      时间对象
    • dateNew

      public static DateTime dateNew(Date date)
      根据已有Date 产生新的DateTime对象
      Parameters:
      date - Date对象,如果传入null,返回null
      Returns:
      DateTime对象
    • date

      public static DateTime date(Date date, TimeZone timeZone)
      根据已有Date 产生新的DateTime对象,并根据指定时区转换
      Parameters:
      date - Date对象,如果传入null,返回null
      timeZone - 时区,传入null则使用默认时区
      Returns:
      DateTime对象
    • date

      public static DateTime date(long date)
      Long类型时间转为DateTime 只支持毫秒级别时间戳,如果需要秒级别时间戳,请自行×1000L
      Parameters:
      date - Long类型Date(Unix时间戳)
      Returns:
      时间对象
    • date

      public static DateTime date(Calendar calendar)
      Calendar类型时间转为DateTime 始终根据已有Calendar 产生新的DateTime对象
      Parameters:
      calendar - Calendar,如果传入null,返回null
      Returns:
      时间对象
    • date

      public static DateTime date(TemporalAccessor temporalAccessor)
      TemporalAccessor类型时间转为DateTime 始终根据已有TemporalAccessor 产生新的DateTime对象
      Parameters:
      temporalAccessor - TemporalAccessor,常用子类: LocalDateTime、 LocalDate,如果传入null,返回null
      Returns:
      时间对象
    • current

      public static long current()
      当前时间的时间戳
      Returns:
      时间
    • currentSeconds

      public static long currentSeconds()
      当前时间的时间戳(秒)
      Returns:
      当前时间秒数
    • formatNow

      public static String formatNow()
      当前时间,格式 yyyy-MM-dd HH:mm:ss
      Returns:
      当前时间的标准形式字符串
    • formatToday

      public static String formatToday()
      当前日期,格式 yyyy-MM-dd
      Returns:
      当前日期的标准形式字符串
    • year

      public static int year(Date date)
      获得年的部分
      Parameters:
      date - 日期
      Returns:
      年的部分
    • quarter

      public static int quarter(Date date)
      获得指定日期所属季度,从1开始计数
      Parameters:
      date - 日期
      Returns:
      第几个季度
    • quarterEnum

      public static Quarter quarterEnum(Date date)
      获得指定日期所属季度
      Parameters:
      date - 日期
      Returns:
      第几个季度枚举
    • month

      public static int month(Date date)
      获得月份,从0开始计数
      Parameters:
      date - 日期
      Returns:
      月份,从0开始计数
    • monthEnum

      public static Month monthEnum(Date date)
      获得月份
      Parameters:
      date - 日期
      Returns:
      Month
    • weekOfYear

      public static int weekOfYear(Date date)
      获得指定日期是所在年份的第几周 此方法返回值与一周的第一天有关,比如: 2016年1月3日为周日,如果一周的第一天为周日,那这天是第二周(返回2) 如果一周的第一天为周一,那这天是第一周(返回1) 跨年的那个星期得到的结果总是1
      Parameters:
      date - 日期
      Returns:
      See Also:
    • weekOfMonth

      public static int weekOfMonth(Date date)
      获得指定日期是所在月份的第几周
      Parameters:
      date - 日期
      Returns:
    • dayOfMonth

      public static int dayOfMonth(Date date)
      获得指定日期是这个日期所在月份的第几天
      Parameters:
      date - 日期
      Returns:
    • dayOfYear

      public static int dayOfYear(Date date)
      获得指定日期是这个日期所在年的第几天
      Parameters:
      date - 日期
      Returns:
    • dayOfWeek

      public static int dayOfWeek(Date date)
      获得指定日期是星期几,1表示周日,2表示周一
      Parameters:
      date - 日期
      Returns:
    • dayOfWeekEnum

      public static Week dayOfWeekEnum(Date date)
      获得指定日期是星期几
      Parameters:
      date - 日期
      Returns:
      Week
    • isWeekend

      public static boolean isWeekend(Date date)
      是否为周末(周六或周日)
      Parameters:
      date - 判定的日期Date
      Returns:
      是否为周末(周六或周日)
    • hour

      public static int hour(Date date, boolean is24HourClock)
      获得指定日期的小时数部分
      Parameters:
      date - 日期
      is24HourClock - 是否24小时制
      Returns:
      小时数
    • minute

      public static int minute(Date date)
      获得指定日期的分钟数部分 例如:10:04:15.250 = 4
      Parameters:
      date - 日期
      Returns:
      分钟数
    • second

      public static int second(Date date)
      获得指定日期的秒数部分
      Parameters:
      date - 日期
      Returns:
      秒数
    • millisecond

      public static int millisecond(Date date)
      获得指定日期的毫秒数部分
      Parameters:
      date - 日期
      Returns:
      毫秒数
    • thisYear

      public static int thisYear()
      Returns:
      今年
    • thisMonth

      public static int thisMonth()
      Returns:
      当前月份,从0开始计数
    • thisMonthEnum

      public static Month thisMonthEnum()
      Returns:
      当前月份 Month
    • thisWeekOfYear

      public static int thisWeekOfYear()
      Returns:
      当前日期所在年份的第几周
    • thisWeekOfMonth

      public static int thisWeekOfMonth()
      Returns:
      当前日期所在月份的第几周
    • thisDayOfMonth

      public static int thisDayOfMonth()
      Returns:
      当前日期是这个日期所在月份的第几天
    • thisDayOfWeek

      public static int thisDayOfWeek()
      Returns:
      当前日期是星期几
    • thisDayOfWeekEnum

      public static Week thisDayOfWeekEnum()
      Returns:
      当前日期是星期几 Week
    • thisHour

      public static int thisHour(boolean is24HourClock)
      Parameters:
      is24HourClock - 是否24小时制
      Returns:
      当前日期的小时数部分
    • thisMinute

      public static int thisMinute()
      Returns:
      当前日期的分钟数部分
    • thisSecond

      public static int thisSecond()
      Returns:
      当前日期的秒数部分
    • thisMillisecond

      public static int thisMillisecond()
      Returns:
      当前日期的毫秒数部分
    • yearAndQuarter

      public static String yearAndQuarter(Date date)
      获得指定日期年份和季节 格式:[20131]表示2013年第一季度
      Parameters:
      date - 日期
      Returns:
      Quarter ,类似于 20132
    • format

      public static String format(Date date, String format)
      根据特定格式格式化日期
      Parameters:
      date - 被格式化的日期
      format - 日期格式,常用格式见: Fields Fields.NORM_DATETIME
      Returns:
      格式化后的字符串
    • format

      public static String format(Date date, FormatPrinter format)
      根据特定格式格式化日期
      Parameters:
      date - 被格式化的日期
      format - FormatPrinterFormatBuilder Formatter.NORM_DATETIME_FORMAT
      Returns:
      格式化后的字符串
    • format

      public static String format(Date date, DateFormat format)
      根据特定格式格式化日期
      Parameters:
      date - 被格式化的日期
      format - SimpleDateFormat
      Returns:
      格式化后的字符串
    • format

      public static String format(Date date, DateTimeFormatter format)
      根据特定格式格式化日期
      Parameters:
      date - 被格式化的日期
      format - SimpleDateFormat Formatter.NORM_DATETIME_FORMATTER
      Returns:
      格式化后的字符串
    • formatDateTime

      public static String formatDateTime(Date date)
      格式化日期时间 格式 yyyy-MM-dd HH:mm:ss
      Parameters:
      date - 被格式化的日期
      Returns:
      格式化后的日期
    • formatDate

      public static String formatDate(Date date)
      格式化日期部分(不包括时间) 格式 yyyy-MM-dd
      Parameters:
      date - 被格式化的日期
      Returns:
      格式化后的字符串
    • formatTime

      public static String formatTime(Date date)
      格式化时间 格式 HH:mm:ss
      Parameters:
      date - 被格式化的日期
      Returns:
      格式化后的字符串
    • formatHttpDate

      public static String formatHttpDate(Date date)
      格式化为Http的标准日期格式 标准日期格式遵循RFC 1123规范,格式类似于:Fri, 31 Dec 1999 23:59:59 GMT
      Parameters:
      date - 被格式化的日期
      Returns:
      HTTP标准形式日期字符串
    • formatChineseDate

      public static String formatChineseDate(Date date, boolean isUppercase, boolean withTime)
      格式化为中文日期格式,如果isUppercase为false,则返回类似:2024年05月20日,否则返回二〇二四年五月二十日
      Parameters:
      date - 被格式化的日期
      isUppercase - 是否采用大写形式
      withTime - 是否包含时间部分
      Returns:
      中文日期字符串
    • truncate

      public static DateTime truncate(Date date, Various various)
      修改日期为某个时间字段起始时间
      Parameters:
      date - Date
      various - 保留到的时间字段,如定义为 Various.SECOND,表示这个字段不变,这个字段以下字段全部归0
      Returns:
      DateTime
    • round

      public static DateTime round(Date date, Various various)
      修改日期为某个时间字段四舍五入时间
      Parameters:
      date - Date
      various - 时间字段
      Returns:
      DateTime
    • ceiling

      public static DateTime ceiling(Date date, Various various)
      修改日期为某个时间字段结束时间
      Parameters:
      date - Date
      various - 保留到的时间字段,如定义为 Various.SECOND,表示这个字段不变,这个字段以下字段全部取最大值
      Returns:
      DateTime
    • ceiling

      public static DateTime ceiling(Date date, Various various, boolean truncateMillisecond)
      修改日期为某个时间字段结束时间 可选是否归零毫秒。

      有时候由于毫秒部分必须为0(如MySQL数据库中),因此在此加上选项。

      Parameters:
      date - Date
      various - 时间字段
      truncateMillisecond - 是否毫秒归零
      Returns:
      DateTime
    • beginOfSecond

      public static DateTime beginOfSecond(Date date)
      获取秒级别的开始时间,即毫秒部分设置为0
      Parameters:
      date - 日期
      Returns:
      DateTime
    • endOfSecond

      public static DateTime endOfSecond(Date date)
      获取秒级别的结束时间,即毫秒设置为999
      Parameters:
      date - 日期
      Returns:
      DateTime
    • beginOfHour

      public static DateTime beginOfHour(Date date)
      获取某小时的开始时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • endOfHour

      public static DateTime endOfHour(Date date)
      获取某小时的结束时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • beginOfMinute

      public static DateTime beginOfMinute(Date date)
      获取某分钟的开始时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • endOfMinute

      public static DateTime endOfMinute(Date date)
      获取某分钟的结束时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • beginOfDay

      public static DateTime beginOfDay(Date date)
      获取某天的开始时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • endOfDay

      public static DateTime endOfDay(Date date)
      获取某天的结束时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • beginOfWeek

      public static DateTime beginOfWeek(Date date)
      获取某周的开始时间,周一定为一周的开始时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • beginOfWeek

      public static DateTime beginOfWeek(Date date, boolean isMondayAsFirstDay)
      获取某周的开始时间
      Parameters:
      date - 日期
      isMondayAsFirstDay - 是否周一做为一周的第一天(false表示周日做为第一天)
      Returns:
      DateTime
    • endOfWeek

      public static DateTime endOfWeek(Date date)
      获取某周的结束时间,周日定为一周的结束
      Parameters:
      date - 日期
      Returns:
      DateTime
    • endOfWeek

      public static DateTime endOfWeek(Date date, boolean isSundayAsLastDay)
      获取某周的结束时间
      Parameters:
      date - 日期
      isSundayAsLastDay - 是否周日做为一周的最后一天(false表示周六做为最后一天)
      Returns:
      DateTime
    • beginOfMonth

      public static DateTime beginOfMonth(Date date)
      获取某月的开始时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • endOfMonth

      public static DateTime endOfMonth(Date date)
      获取某月的结束时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • beginOfQuarter

      public static DateTime beginOfQuarter(Date date)
      获取某季度的开始时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • endOfQuarter

      public static DateTime endOfQuarter(Date date)
      获取某季度的结束时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • beginOfYear

      public static DateTime beginOfYear(Date date)
      获取某年的开始时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • endOfYear

      public static DateTime endOfYear(Date date)
      获取某年的结束时间
      Parameters:
      date - 日期
      Returns:
      DateTime
    • yesterday

      public static DateTime yesterday()
      昨天
      Returns:
      昨天
    • tomorrow

      public static DateTime tomorrow()
      明天
      Returns:
      明天
    • lastWeek

      public static DateTime lastWeek()
      上周
      Returns:
      上周
    • nextWeek

      public static DateTime nextWeek()
      下周
      Returns:
      下周
    • lastMonth

      public static DateTime lastMonth()
      上个月
      Returns:
      上个月
    • nextMonth

      public static DateTime nextMonth()
      下个月
      Returns:
      下个月
    • offsetMillisecond

      public static DateTime offsetMillisecond(Date date, int offset)
      偏移毫秒数
      Parameters:
      date - 日期
      offset - 偏移毫秒数,正数向未来偏移,负数向历史偏移
      Returns:
      偏移后的日期
    • offsetSecond

      public static DateTime offsetSecond(Date date, int offset)
      偏移秒数
      Parameters:
      date - 日期
      offset - 偏移秒数,正数向未来偏移,负数向历史偏移
      Returns:
      偏移后的日期
    • offsetMinute

      public static DateTime offsetMinute(Date date, int offset)
      偏移分钟
      Parameters:
      date - 日期
      offset - 偏移分钟数,正数向未来偏移,负数向历史偏移
      Returns:
      偏移后的日期
    • offsetHour

      public static DateTime offsetHour(Date date, int offset)
      偏移小时
      Parameters:
      date - 日期
      offset - 偏移小时数,正数向未来偏移,负数向历史偏移
      Returns:
      偏移后的日期
    • offsetDay

      public static DateTime offsetDay(Date date, int offset)
      偏移天
      Parameters:
      date - 日期
      offset - 偏移天数,正数向未来偏移,负数向历史偏移
      Returns:
      偏移后的日期
    • offsetWeek

      public static DateTime offsetWeek(Date date, int offset)
      偏移周
      Parameters:
      date - 日期
      offset - 偏移周数,正数向未来偏移,负数向历史偏移
      Returns:
      偏移后的日期
    • offsetMonth

      public static DateTime offsetMonth(Date date, int offset)
      偏移月
      Parameters:
      date - 日期
      offset - 偏移月数,正数向未来偏移,负数向历史偏移
      Returns:
      偏移后的日期
    • offsetYear

      public static DateTime offsetYear(Date date, int offset)
      偏移年
      Parameters:
      date - 日期
      offset - 偏移月数,正数向未来偏移,负数向历史偏移
      Returns:
      偏移后的日期
    • offset

      public static DateTime offset(Date date, Various various, int offset)
      获取指定日期偏移指定时间后的时间,生成的偏移日期不影响原日期
      Parameters:
      date - 基准日期
      various - 偏移的粒度大小(小时、天、月等)Various
      offset - 偏移量,正数为向后偏移,负数为向前偏移
      Returns:
      偏移后的日期
    • between

      public static long between(Date beginDate, Date endDate, Units unit)
      判断两个日期相差的时长,只保留绝对值
      Parameters:
      beginDate - 起始日期
      endDate - 结束日期
      unit - 相差的单位:相差 天Units.DAY、小时Units.HOUR
      Returns:
      日期差
    • between

      public static long between(Date beginDate, Date endDate, Units unit, boolean isAbs)
      判断两个日期相差的时长
      Parameters:
      beginDate - 起始日期
      endDate - 结束日期
      unit - 相差的单位:相差 天Units.DAY、小时Units.HOUR
      isAbs - 日期间隔是否只保留绝对值正数
      Returns:
      日期差
    • betweenMs

      public static long betweenMs(Date beginDate, Date endDate)
      判断两个日期相差的毫秒数
      Parameters:
      beginDate - 起始日期
      endDate - 结束日期
      Returns:
      日期差
    • betweenDay

      public static long betweenDay(Date beginDate, Date endDate, boolean isReset)
      判断两个日期相差的天数
       有时候我们计算相差天数的时候需要忽略时分秒。
       比如:2016-02-01 23:59:59和2016-02-02 00:00:00相差一秒
       如果isReset为false相差天数为0。
       如果isReset为true相差天数将被计算为1
       
      Parameters:
      beginDate - 起始日期
      endDate - 结束日期
      isReset - 是否重置时间为起始时间
      Returns:
      日期差
    • betweenWeek

      public static long betweenWeek(Date beginDate, Date endDate, boolean isReset)
      计算指定时间区间内的周数
      Parameters:
      beginDate - 开始时间
      endDate - 结束时间
      isReset - 是否重置时间为起始时间
      Returns:
      周数
    • betweenMonth

      public static long betweenMonth(Date beginDate, Date endDate, boolean isReset)
      计算两个日期相差月数 在非重置情况下,如果起始日期的天大于结束日期的天,月数要少算1(不足1个月)
      Parameters:
      beginDate - 起始日期
      endDate - 结束日期
      isReset - 是否重置时间为起始时间(重置天时分秒)
      Returns:
      相差月数
    • betweenYear

      public static long betweenYear(Date beginDate, Date endDate, boolean isReset)
      计算两个日期相差年数 在非重置情况下,如果起始日期的月大于结束日期的月,年数要少算1(不足1年)
      Parameters:
      beginDate - 起始日期
      endDate - 结束日期
      isReset - 是否重置时间为起始时间(重置月天时分秒)
      Returns:
      相差年数
    • formatBetween

      public static String formatBetween(Date beginDate, Date endDate, FormatPeriod.Level level)
      格式化日期间隔输出
      Parameters:
      beginDate - 起始日期
      endDate - 结束日期
      level - 级别,按照天、小时、分、秒、毫秒分为5个等级
      Returns:
      XX天XX小时XX分XX秒
    • formatBetween

      public static String formatBetween(Date beginDate, Date endDate)
      格式化日期间隔输出,精确到毫秒
      Parameters:
      beginDate - 起始日期
      endDate - 结束日期
      Returns:
      XX天XX小时XX分XX秒
    • formatBetween

      public static String formatBetween(long betweenMs, FormatPeriod.Level level)
      格式化日期间隔输出
      Parameters:
      betweenMs - 日期间隔
      level - 级别,按照天、小时、分、秒、毫秒分为5个等级
      Returns:
      XX天XX小时XX分XX秒XX毫秒
    • formatBetween

      public static String formatBetween(long betweenMs)
      格式化日期间隔输出,精确到毫秒
      Parameters:
      betweenMs - 日期间隔
      Returns:
      XX天XX小时XX分XX秒XX毫秒
    • spendNt

      public static long spendNt(long preTime)
      计时,常用于记录某段代码的执行时间,单位:纳秒
      Parameters:
      preTime - 之前记录的时间
      Returns:
      时间差,纳秒
    • spendMs

      public static long spendMs(long preTime)
      计时,常用于记录某段代码的执行时间,单位:毫秒
      Parameters:
      preTime - 之前记录的时间
      Returns:
      时间差,毫秒
    • createStopWatch

      public static StopWatch createStopWatch()
      创建秒表StopWatch,用于对代码块的执行时间计数

      使用方法如下:

       StopWatch stopWatch = DateKit.createStopWatch();
      
       // 任务1
       stopWatch.start("任务一");
       Thread.sleep(1000);
       stopWatch.stop();
      
       // 任务2
       stopWatch.start("任务二");
       Thread.sleep(2000);
       stopWatch.stop();
      
       // 打印出耗时
       Console.log(stopWatch.prettyPrint());
      
       
      Returns:
      StopWatch
    • createStopWatch

      public static StopWatch createStopWatch(String id)
      创建秒表StopWatch,用于对代码块的执行时间计数

      使用方法如下:

       StopWatch stopWatch = DateKit.createStopWatch("任务名称");
      
       // 任务1
       stopWatch.start("任务一");
       Thread.sleep(1000);
       stopWatch.stop();
      
       // 任务2
       stopWatch.start("任务二");
       Thread.sleep(2000);
       stopWatch.stop();
      
       // 打印出耗时
       Console.log(stopWatch.prettyPrint());
      
       
      Parameters:
      id - 用于标识秒表的唯一ID
      Returns:
      StopWatch
    • ageOfNow

      public static int ageOfNow(String birthDay)
      生日转为年龄,计算法定年龄
      Parameters:
      birthDay - 生日,标准日期字符串
      Returns:
      年龄
    • ageOfNow

      public static int ageOfNow(Date birthDay)
      生日转为年龄,计算法定年龄(周岁) 按照《最高人民法院关于审理未成年人刑事案件具体应用法律若干问题的解释》第二条规定刑法第十七条规定的“周岁”,按照公历的年、月、日计算,从周岁生日的第二天起算。
      • 2022-03-01出生,则相对2023-03-01,周岁为0,相对于2023-03-02才是1岁。
      • 1999-02-28出生,则相对2000-02-29,周岁为1
      Parameters:
      birthDay - 生日
      Returns:
      年龄
    • age

      public static int age(Date birthday, Date dateToCompare)
      计算相对于dateToCompare的年龄,常用于计算指定生日在某年的年龄 按照《最高人民法院关于审理未成年人刑事案件具体应用法律若干问题的解释》第二条规定刑法第十七条规定的“周岁”,按照公历的年、月、日计算,从周岁生日的第二天起算。
      • 2022-03-01出生,则相对2023-03-01,周岁为0,相对于2023-03-02才是1岁。
      • 1999-02-28出生,则相对2000-02-29,周岁为1
      Parameters:
      birthday - 生日
      dateToCompare - 需要对比的日期
      Returns:
      年龄
    • timeToSecond

      public static int timeToSecond(String timeStr)
      HH:mm:ss 时间格式字符串转为秒数 参考:https://github.com/iceroot
      Parameters:
      timeStr - 字符串时分秒(HH:mm:ss)格式
      Returns:
      时分秒转换后的秒数
    • secondToTime

      public static String secondToTime(int seconds)
      秒数转为时间格式(HH:mm:ss) 参考:https://github.com/iceroot
      Parameters:
      seconds - 需要转换的秒数
      Returns:
      转换后的字符串
    • range

      public static Boundary range(Date start, Date end, Various unit)
      创建日期范围生成器
      Parameters:
      start - 起始日期时间(包括)
      end - 结束日期时间
      unit - 步进单位
      Returns:
      Boundary
    • rangeContains

      public static List<DateTime> rangeContains(Boundary start, Boundary end)
      俩个时间区间取交集
      Parameters:
      start - 开始区间
      end - 结束区间
      Returns:
      true 包含
    • rangeNotContains

      public static List<DateTime> rangeNotContains(Boundary start, Boundary end)
      俩个时间区间取差集(end - start)
      Parameters:
      start - 开始区间
      end - 结束区间
      Returns:
      true 包含
    • rangeFunc

      public static <T> List<T> rangeFunc(Date start, Date end, Various unit, Function<Date,T> func)
      按日期范围遍历,执行 function
      Type Parameters:
      T - Date经过函数处理结果类型
      Parameters:
      start - 起始日期时间(包括)
      end - 结束日期时间
      unit - 步进单位
      func - 每次遍历要执行的 function
      Returns:
      结果列表
    • rangeConsume

      public static void rangeConsume(Date start, Date end, Various unit, Consumer<Date> consumer)
      按日期范围遍历,执行 consumer
      Parameters:
      start - 起始日期时间(包括)
      end - 结束日期时间
      unit - 步进单位
      consumer - 每次遍历要执行的 consumer
    • rangeToList

      public static List<DateTime> rangeToList(Date start, Date end, Various unit)
      根据步进单位获取起始日期时间和结束日期时间的时间区间集合
      Parameters:
      start - 起始日期时间
      end - 结束日期时间
      unit - 步进单位
      Returns:
      Boundary
    • rangeToList

      public static List<DateTime> rangeToList(Date start, Date end, Various unit, int step)
      根据步进单位和步进获取起始日期时间和结束日期时间的时间区间集合
      Parameters:
      start - 起始日期时间
      end - 结束日期时间
      unit - 步进单位
      step - 步进
      Returns:
      Boundary
    • getConstellation

      public static String getConstellation(int month, int day)
      通过生日计算星座
      Parameters:
      month - 月,从0开始计数
      day - 天
      Returns:
      星座名
    • getZodiac

      public static String getZodiac(int year)
      计算生肖
      Parameters:
      year - 农历年
      Returns:
      生肖名
    • compare

      public static int compare(Date date1, Date date2)
      null安全的日期比较,null对象排在末尾
      Parameters:
      date1 - 日期1
      date2 - 日期2
      Returns:
      比较结果,如果date1 < date2,返回数小于0,date1==date2返回0,date1 > date2 大于0
    • compare

      public static int compare(Date date1, Date date2, String format)
      null安全的日期比较,并只比较指定格式; null对象排在末尾, 并指定日期格式;
      Parameters:
      date1 - 日期1
      date2 - 日期2
      format - 日期格式,常用格式见: Fields; 允许为空; date1 date2; eg: yyyy-MM-dd
      Returns:
      比较结果,如果date1 < date2,返回数小于0,date1==date2返回0,date1 > date2 大于0
    • nanosToMillis

      public static long nanosToMillis(long duration)
      纳秒转毫秒
      Parameters:
      duration - 时长
      Returns:
      时长毫秒
    • nanosToSeconds

      public static double nanosToSeconds(long duration)
      纳秒转秒,保留小数
      Parameters:
      duration - 时长
      Returns:
    • toCalendar

      public static Calendar toCalendar(Date date)
      转换为Calendar对象
      Parameters:
      date - 日期对象
      Returns:
      Calendar对象
    • toInstant

      public static Instant toInstant(Date date)
      Date对象转换为Instant对象
      Parameters:
      date - Date对象
      Returns:
      Instant对象
    • toInstant

      public static Instant toInstant(TemporalAccessor temporalAccessor)
      Date对象转换为Instant对象
      Parameters:
      temporalAccessor - Date对象
      Returns:
      Instant对象
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(Instant instant)
      Instant 转换为 LocalDateTime,使用系统默认时区
      Parameters:
      instant - Instant
      Returns:
      LocalDateTime
      See Also:
    • toLocalDateTime

      public static LocalDateTime toLocalDateTime(Date date)
      Date 转换为 LocalDateTime,使用系统默认时区
      Parameters:
      date - Date
      Returns:
      LocalDateTime
    • lengthOfYear

      public static int lengthOfYear(int year)
      获得指定年份的总天数
      Parameters:
      year - 年份
      Returns:
    • lengthOfMonth

      public static int lengthOfMonth(int month, boolean isLeapYear)
      获得指定月份的总天数
      Parameters:
      month - 月份
      isLeapYear - 是否闰年
      Returns:
    • of

      public static LocalDateTime of(Date date)
      DateLocalDateTime,使用默认时区
      Parameters:
      date - Date对象
      Returns:
      LocalDateTime
    • getShortName

      public static String getShortName(TimeUnit unit)
      获取时长单位简写
      Parameters:
      unit - 单位
      Returns:
      单位简写名称
    • isOverlap

      public static boolean isOverlap(Date realStartTime, Date realEndTime, Date startTime, Date endTime)
      检查两个时间段是否有时间重叠 重叠指两个时间段是否有交集,注意此方法时间段重合时如:
      • 此方法未纠正开始时间小于结束时间
      • 当realStartTime和realEndTime或startTime和endTime相等时,退化为判断区间是否包含点
      • 当realStartTime和realEndTime和startTime和endTime相等时,退化为判断点与点是否相等
      See 准确的区间关系参考:艾伦区间代数
      Parameters:
      realStartTime - 第一个时间段的开始时间
      realEndTime - 第一个时间段的结束时间
      startTime - 第二个时间段的开始时间
      endTime - 第二个时间段的结束时间
      Returns:
      true 表示时间有重合或包含或相等
    • isLastDayOfMonth

      public static boolean isLastDayOfMonth(Date date)
      是否为本月最后一天
      Parameters:
      date - Date
      Returns:
      是否为本月最后一天
    • getLastDayOfMonth

      public static int getLastDayOfMonth(Date date)
      获得本月的最后一天
      Parameters:
      date - Date
      Returns:
    • isIn

      public boolean isIn(Date beginDate, Date endDate)
      当前日期是否在日期指定范围内 起始日期和结束日期可以互换
      Parameters:
      beginDate - 起始日期(包含)
      endDate - 结束日期(包含)
      Returns:
      是否在范围内