添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
static DateTime date ( TemporalAccessor temporalAccessor)
TemporalAccessor 类型时间转为 DateTime
始终根据已有 TemporalAccessor 产生新的 DateTime 对象
static DateTime dateNew ( Date date)
根据已有 Date 产生新的 DateTime 对象
static DateTime dateSecond ()
当前时间,转换为 DateTime 对象,忽略毫秒部分
static int dayOfMonth ( Date date)
获得指定日期是这个日期所在月份的第几天
static int dayOfWeek ( Date date)
获得指定日期是星期几,1表示周日,2表示周一
static Week dayOfWeekEnum ( Date date)
获得指定日期是星期几
static int dayOfYear ( Date date)
获得指定日期是这个日期所在年的第几天
static DateTime endOfDay ( Date date)
获取某天的结束时间
static DateTime endOfHour ( Date date)
获取某小时的结束时间
static DateTime endOfMinute ( Date date)
获取某分钟的结束时间
static DateTime endOfMonth ( Date date)
获取某月的结束时间
static DateTime endOfQuarter ( Date date)
获取某季度的结束时间
static DateTime endOfSecond ( Date date)
获取秒级别的结束时间,即毫秒设置为999
static DateTime endOfWeek ( Date date)
获取某周的结束时间,周日定为一周的结束
static DateTime endOfWeek ( Date date, boolean isSundayAsLastDay)
获取某周的结束时间
static DateTime endOfYear ( Date date)
获取某年的结束时间
static String format ( Date date, DateFormat format)
根据特定格式格式化日期
static String format ( Date date, DatePrinter format)
根据特定格式格式化日期
static String format ( Date date, DateTimeFormatter format)
根据特定格式格式化日期
static String format ( Date date, String format)
根据特定格式格式化日期
static String format ( LocalDateTime localDateTime, String format)
根据特定格式格式化日期
static String formatBetween ( Date beginDate, Date endDate)
格式化日期间隔输出,精确到毫秒
static String formatBetween ( Date beginDate, Date endDate, BetweenFormatter.Level level)
格式化日期间隔输出
static String formatBetween (long betweenMs)
格式化日期间隔输出,精确到毫秒
static String formatBetween (long betweenMs, BetweenFormatter.Level level)
格式化日期间隔输出
static String formatChineseDate ( Date date, boolean isUppercase, boolean withTime)
格式化为中文日期格式,如果isUppercase为false,则返回类似:2018年10月24日,否则返回二〇一八年十月二十四日
static String formatDate ( Date date)
格式化日期部分(不包括时间)
格式 yyyy-MM-dd
static String formatDateTime ( Date date)
格式化日期时间
格式 yyyy-MM-dd HH:mm:ss
static String formatHttpDate ( Date date)
格式化为Http的标准日期格式
标准日期格式遵循RFC 1123规范,格式类似于:Fri, 31 Dec 1999 23:59:59 GMT
static String formatLocalDateTime ( LocalDateTime localDateTime)
格式化日期时间
格式 yyyy-MM-dd HH:mm:ss
static String formatTime ( Date date)
格式化时间
格式 HH:mm:ss
static String getChineseZodiac (int year)
计算生肖,只计算1900年后出生的人
static int getLastDayOfMonth ( Date date)
获得本月的最后一天
static String getShotName ( TimeUnit unit)
获取时长单位简写
static String getZodiac (int month, int day)
通过生日计算星座
static int hour ( Date date, boolean is24HourClock)
获得指定日期的小时数部分
static boolean isAM ( Date date)
是否为上午
static boolean isExpired ( Date startDate, Date endDate, Date checkDate)
Deprecated.
使用isIn方法
此方法未纠正开始时间小于结束时间 当realStartTime和realEndTime或startTime和endTime相等时,退化为判断区间是否包含点 当realStartTime和realEndTime和startTime和endTime相等时,退化为判断点与点是否相等 See 准确的区间关系参考:艾伦区间代数
static boolean isPM ( Date date)
是否为下午
static boolean isSameDay ( Date date1, Date date2)
比较两个日期是否为同一天
static boolean isSameMonth ( Date date1, Date date2)
比较两个日期是否为同一月
static boolean isSameTime ( Date date1, Date date2)
是否为相同时间
此方法比较两个日期的时间戳是否相同
static boolean isSameWeek ( Date date1, Date date2, boolean isMon)
比较两个日期是否为同一周
static boolean isWeekend ( Date date)
是否为周末(周六或周日)
static DateTime lastMonth () static DateTime lastWeek () static int lengthOfMonth (int month, boolean isLeapYear)
获得指定月份的总天数
static int lengthOfYear (int year)
获得指定年份的总天数
static int millisecond ( Date date)
获得指定日期的毫秒数部分
static int minute ( Date date)
获得指定日期的分钟数部分
例如:10:04:15.250 =》 4
static int month ( Date date)
获得月份,从0开始计数
static Month monthEnum ( Date date) static long nanosToMillis (long duration)
纳秒转毫秒
static double nanosToSeconds (long duration)
纳秒转秒,保留小数
static SimpleDateFormat newSimpleFormat ( String pattern)
创建 SimpleDateFormat ,注意此对象非线程安全!
static SimpleDateFormat newSimpleFormat ( String pattern, Locale locale, TimeZone timeZone)
创建 SimpleDateFormat ,注意此对象非线程安全!
static DateTime nextMonth () static DateTime nextWeek () static String now ()
当前时间,格式 yyyy-MM-dd HH:mm:ss
static DateTime offset ( Date date, DateField dateField, int offset)
获取指定日期偏移指定时间后的时间,生成的偏移日期不影响原日期
static DateTime offsetDay ( Date date, int offset) static DateTime offsetHour ( Date date, int offset) static DateTime offsetMillisecond ( Date date, int offset)
偏移毫秒数
static DateTime offsetMinute ( Date date, int offset) static DateTime offsetMonth ( Date date, int offset) static DateTime offsetSecond ( Date date, int offset) static DateTime offsetWeek ( Date date, int offset) static DateTime offsetYear ( Date date, int offset) static DateTime parse ( CharSequence dateCharSequence)
将日期字符串转换为 DateTime 对象,格式:
yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss yyyy.MM.dd HH:mm:ss yyyy年MM月dd日 HH时mm分ss秒 yyyy-MM-dd yyyy/MM/dd yyyy.MM.dd HH:mm:ss HH时mm分ss秒 yyyy-MM-dd HH:mm yyyy-MM-dd HH:mm:ss.SSS yyyy-MM-dd HH:mm:ss.SSSSSS yyyyMMddHHmmss yyyyMMddHHmmssSSS yyyyMMdd EEE, dd MMM yyyy HH:mm:ss z EEE MMM dd HH:mm:ss zzz yyyy yyyy-MM-dd'T'HH:mm:ss'Z' yyyy-MM-dd'T'HH:mm:ss.SSS'Z' yyyy-MM-dd'T'HH:mm:ssZ yyyy-MM-dd'T'HH:mm:ss.SSSZ

Methods inherited from class cn.hutool.core.date. CalendarUtil

age , age , beginOfDay , beginOfHour , beginOfMinute , beginOfMonth , beginOfQuarter , beginOfSecond , beginOfWeek , beginOfWeek , beginOfYear , calendar , calendar , calendar , calendar , ceiling , ceiling , compare , endOfDay , endOfHour , endOfMinute , endOfMonth , endOfQuarter , endOfSecond , endOfWeek , endOfWeek , endOfYear , formatChineseDate , getBeginValue , getBeginValue , getEndValue , getEndValue , isAM , isLastDayOfMonth , isPM , isSameDay , isSameInstant , isSameMonth , isSameWeek , parse , parseByPatterns , parseByPatterns , parseByPatterns , round , toInstant , toLocalDateTime , truncate , yearAndQuarter , yearAndQuarter
public static DateTime date(Date date)
Date 类型时间转为 DateTime
如果date本身为DateTime对象,则返回强转后的对象,否则新建一个DateTime对象
Parameters:
date - Long类型Date(Unix时间戳),如果传入 null ,返回 null
Returns:
Since:
3.0.7
public static DateTime date(long date)
Long类型时间转为 DateTime
只支持毫秒级别时间戳,如果需要秒级别时间戳,请自行×1000
Parameters:
date - Long类型Date(Unix时间戳)
Returns:
public static DateTime date(Calendar calendar)
Calendar 类型时间转为 DateTime
始终根据已有 Calendar 产生新的 DateTime 对象
Parameters:
calendar - Calendar ,如果传入 null ,返回 null
Returns:
public static DateTime date(TemporalAccessor temporalAccessor)
TemporalAccessor 类型时间转为 DateTime
始终根据已有 TemporalAccessor 产生新的 DateTime 对象
Parameters:
temporalAccessor - TemporalAccessor ,常用子类: LocalDateTime 、 LocalDate,如果传入 null ,返回 null
Returns:
Since:
5.0.0

yearAndQuarter

public static LinkedHashSet<String> yearAndQuarter(Date startDate,
                                                   Date endDate)
获得指定日期区间内的年份和季节
Parameters:
startDate - 起始日期(包含)
endDate - 结束日期(包含)
Returns:
季度列表 ,元素类似于 20132

formatLocalDateTime

public static String formatLocalDateTime(LocalDateTime localDateTime)
格式化日期时间
格式 yyyy-MM-dd HH:mm:ss
Parameters:
localDateTime - 被格式化的日期
Returns:
格式化后的字符串
Parameters:
date - 被格式化的日期
format - 日期格式,常用格式见: DatePattern DatePattern.NORM_DATETIME_PATTERN
Returns:
格式化后的字符串
Parameters:
date - 被格式化的日期
format - DatePrinter FastDateFormat DatePattern.NORM_DATETIME_FORMAT
Returns:
格式化后的字符串
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,则返回类似:2018年10月24日,否则返回二〇一八年十月二十四日
Parameters:
date - 被格式化的日期
isUppercase - 是否采用大写形式
withTime - 是否包含时间部分
Returns:
中文日期字符串
Since:
5.3.9

parseLocalDateTime

public static LocalDateTime parseLocalDateTime(CharSequence dateStr)
构建LocalDateTime对象
格式:yyyy-MM-dd HH:mm:ss
Parameters:
dateStr - 时间字符串(带格式)
Returns:
LocalDateTime对象

parseLocalDateTime

public static LocalDateTime parseLocalDateTime(CharSequence dateStr,
                                               String format)
构建LocalDateTime对象
Parameters:
dateStr - 时间字符串(带格式)
format - 使用 DatePattern 定义的格式
Returns:
LocalDateTime对象
public static DateTime parse(CharSequence dateStr,
                             DateFormat dateFormat)
构建DateTime对象
Parameters:
dateStr - Date字符串
dateFormat - 格式化器 SimpleDateFormat
Returns:
DateTime对象
public static DateTime parse(CharSequence dateStr,
                             DateParser parser,
                             boolean lenient)
构建DateTime对象
Parameters:
dateStr - Date字符串
parser - 格式化器, FastDateFormat
lenient - 是否宽容模式
Returns:
DateTime对象
Since:
5.7.14
public static DateTime parse(CharSequence dateStr,
                             DateTimeFormatter formatter)
构建DateTime对象
Parameters:
dateStr - Date字符串
formatter - 格式化器, DateTimeFormatter
Returns:
DateTime对象
Since:
5.0.0
public static DateTime parse(String str,
                             String... parsePatterns)
                      throws DateException
通过给定的日期格式解析日期时间字符串。
传入的日期格式会逐个尝试,直到解析成功,返回 DateTime 对象,否则抛出 DateException 异常。
Parameters:
str - 日期时间字符串,非空
parsePatterns - 需要尝试的日期时间格式数组,非空, 见SimpleDateFormat
Returns:
解析后的Date
Throws:
IllegalArgumentException - if the date string or pattern array is null
DateException - if none of the date patterns were suitable
Since:
5.3.11

parseDateTime

public static DateTime parseDateTime(CharSequence dateString)
解析日期时间字符串,格式支持: yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss yyyy.MM.dd HH:mm:ss yyyy年MM月dd日 HH:mm:ss
Parameters:
dateString - 标准形式的时间字符串
Returns:

parseDate

public static DateTime parseDate(CharSequence dateString)
解析日期字符串,忽略时分秒,支持的格式包括: yyyy-MM-dd yyyy/MM/dd yyyy.MM.dd yyyy年MM月dd日
Parameters:
dateString - 标准形式的日期字符串
Returns:

parseTime

public static DateTime parseTime(CharSequence timeString)
解析时间,格式HH:mm:ss,日期部分默认为1970-01-01
Parameters:
timeString - 标准形式的日期字符串
Returns:

parseTimeToday

public static DateTime parseTimeToday(CharSequence timeString)
解析时间,格式HH:mm 或 HH:mm:ss,日期默认为今天
Parameters:
timeString - 标准形式的日期字符串
Returns:
Since:
3.1.1
@Deprecated
public static DateTime parseCST(CharSequence cstString)
Deprecated. 理解错误,请使用 parseRFC2822(CharSequence)
解析CST时间,格式:
  • EEE MMM dd HH:mm:ss z yyyy(例如:Wed Aug 01 00:00:00 CST 2012)
  • Parameters:
    cstString - UTC时间
    Returns:
    Since:
    4.6.9

    parse

    public static DateTime parse(CharSequence dateCharSequence)
    将日期字符串转换为 DateTime 对象,格式:
  • yyyy-MM-dd HH:mm:ss
  • yyyy/MM/dd HH:mm:ss
  • yyyy.MM.dd HH:mm:ss
  • yyyy年MM月dd日 HH时mm分ss秒
  • yyyy-MM-dd
  • yyyy/MM/dd
  • yyyy.MM.dd
  • HH:mm:ss
  • HH时mm分ss秒
  • yyyy-MM-dd HH:mm
  • yyyy-MM-dd HH:mm:ss.SSS
  • yyyy-MM-dd HH:mm:ss.SSSSSS
  • yyyyMMddHHmmss
  • yyyyMMddHHmmssSSS
  • yyyyMMdd
  • EEE, dd MMM yyyy HH:mm:ss z
  • EEE MMM dd HH:mm:ss zzz yyyy
  • yyyy-MM-dd'T'HH:mm:ss'Z'
  • yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
  • yyyy-MM-dd'T'HH:mm:ssZ
  • yyyy-MM-dd'T'HH:mm:ss.SSSZ
  • Parameters:
    dateCharSequence - 日期字符串
    Returns:
    Parameters:
    date - Date
    dateField - 保留到的时间字段,如定义为 DateField.SECOND ,表示这个字段不变,这个字段以下字段全部取最大值
    Returns:
    DateTime
    Since:
    4.5.7
    public static DateTime ceiling(Date date,
                                   DateField dateField,
                                   boolean truncateMillisecond)
    修改日期为某个时间字段结束时间
    可选是否归零毫秒。 有时候由于毫秒部分必须为0(如MySQL数据库中),因此在此加上选项。
    Parameters:
    date - Date
    dateField - 时间字段
    truncateMillisecond - 是否毫秒归零
    Returns:
    DateTime
    Since:
    4.5.7
    有时候我们计算相差天数的时候需要忽略时分秒。 比如:2016-02-01 23:59:59和2016-02-02 00:00:00相差一秒 如果isReset为 false 相差天数为0。 如果isReset为 true 相差天数将被计算为1
    Parameters:
    beginDate - 起始日期
    endDate - 结束日期
    isReset - 是否重置时间为起始时间
    Returns:
    Since:
    3.0.1
    public static String formatBetween(Date beginDate,
                                       Date endDate,
                                       BetweenFormatter.Level level)
    格式化日期间隔输出
    Parameters:
    beginDate - 起始日期
    endDate - 结束日期
    level - 级别,按照天、小时、分、秒、毫秒分为5个等级
    Returns:
    XX天XX小时XX分XX秒

    formatBetween

    public static String formatBetween(long betweenMs,
                                       BetweenFormatter.Level level)
    格式化日期间隔输出
    Parameters:
    betweenMs - 日期间隔
    level - 级别,按照天、小时、分、秒、毫秒分为5个等级
    Returns:
    XX天XX小时XX分XX秒XX毫秒
    public static int toIntSecond( Date date)
    Deprecated. 2022年后结果溢出,此方法废弃
    格式化成yyMMddHHmm后转换为int型
    Parameters:
    date - 日期
    Returns:

    createStopWatch

    public static StopWatch createStopWatch(String id)
    创建秒表 StopWatch ,用于对代码块的执行时间计数 使用方法如下: StopWatch stopWatch = DateUtil.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
    Since:
    5.2.3
    public static int age(Date birthday,
                          Date dateToCompare)
    计算相对于dateToCompare的年龄,常用于计算指定生日在某年的年龄
    Parameters:
    birthday - 生日
    dateToCompare - 需要对比的日期
    Returns:
    以商品为例,startDate即生产日期,endDate即保质期的截止日期,checkDate表示在何时检查是否过期(一般为当前时间)
    endDate和startDate的差值即为保质期(按照毫秒计),checkDate和startDate的差值即为实际经过的时长,实际时长大于保质期表示超时。
    Parameters:
    startDate - 开始时间
    endDate - 被比较的时间,即有效期的截止时间。如果经过时长后的时间晚于被检查的时间,就表示过期
    checkDate - 检查时间,可以是当前时间,既
    Returns:
    Since:
    5.1.1
    public static int timeToSecond(String timeStr)
    HH:mm:ss 时间格式字符串转为秒数
    参考: https://github.com/iceroot
    Parameters:
    timeStr - 字符串时分秒(HH:mm:ss)格式
    Returns:
    时分秒转换后的秒数
    Since:
    3.1.2
    public static String secondToTime(int seconds)
    秒数转为时间格式(HH:mm:ss)
    参考: https://github.com/iceroot
    Parameters:
    seconds - 需要转换的秒数
    Returns:
    转换后的字符串
    Since:
    3.1.2

    rangeContains

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

    rangeNotContains

    public static List<DateTime> rangeNotContains(DateRange start,
                                                  DateRange end)
    俩个时间区间取差集(end - start)
    Parameters:
    start - 开始区间
    end - 结束区间
    Returns:
    true 包含
    Since:
    5.7.21
    date1 - 日期1
    date2 - 日期2
    format - 日期格式,常用格式见: DatePattern ; 允许为空; date1 date2; eg: yyyy-MM-dd
    Returns:
    比较结果,如果date1 < date2,返回数小于0,date1==date2返回0,date1 > date2 大于0
    Since:
    5.6.4

    toInstant

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

    toLocalDateTime

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

    toLocalDateTime

    public static LocalDateTime toLocalDateTime(Date date)
    Date 转换为 LocalDateTime ,使用系统默认时区
    Parameters:
    date - Date
    Returns:
    LocalDateTime
    Since:
    5.0.5
    See Also:
    LocalDateTimeUtil.of(Date)

    newSimpleFormat

    public static SimpleDateFormat newSimpleFormat(String pattern)
    创建 SimpleDateFormat ,注意此对象非线程安全!
    此对象默认为严格格式模式,即parse时如果格式不正确会报错。
    Parameters:
    pattern - 表达式
    Returns:
    SimpleDateFormat
    Since:
    5.5.5

    newSimpleFormat

    public static SimpleDateFormat newSimpleFormat(String pattern,
                                                   Locale locale,
                                                   TimeZone timeZone)
    创建 SimpleDateFormat ,注意此对象非线程安全!
    此对象默认为严格格式模式,即parse时如果格式不正确会报错。
    Parameters:
    pattern - 表达式
    locale - Locale null 表示默认
    timeZone - TimeZone null 表示默认
    Returns:
    SimpleDateFormat
    Since:
    5.5.5
  • 此方法未纠正开始时间小于结束时间
  • 当realStartTime和realEndTime或startTime和endTime相等时,退化为判断区间是否包含点
  • 当realStartTime和realEndTime和startTime和endTime相等时,退化为判断点与点是否相等
  • See 准确的区间关系参考:艾伦区间代数
    Parameters:
    realStartTime - 第一个时间段的开始时间
    realEndTime - 第一个时间段的结束时间
    startTime - 第二个时间段的开始时间
    endTime - 第二个时间段的结束时间
    Returns:
    true 表示时间有重合或包含或相等
    Since:
    5.7.22