Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

      • 概述
        • 介绍
        • 日期时间工具
        • 日期枚举
          • 月份枚举
        • 时间枚举
      • 日期时间工具-DateUtil
      • 日期时间对象-DateTime
      • 农历日期-ChineseDate
      • LocalDateTime工具-LocalDateTimeUtil
      • 计时器工具-StopWatch
      • 日历工具-CalendarUtil
      • 日期构建器-DateBuilder
      • 日期间隔-DateBetween
      • 月份枚举-Month
      • 星期枚举-Week
      • 时间单位-DateUnit
      • 日期范围-DateRange
    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(text)

    • 并发和线程(thread)

    • 树结构(tree)

    • 工具集合(util)

    • XML操作(xml)

  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • 日期时间(date)
Hutool
2026-01-03
目录

概述

# 介绍

日期时间包是Hutool的核心包之一,提供针对JDK中time包、Date和Calendar对象的封装,封装对象如下:

# 日期时间工具

  • DateUtil 针对日期时间操作提供一系列静态方法
  • DateTime 提供类似于Joda-Time中日期时间对象的封装,继承自Date类,并提供更加丰富的对象方法。
  • FastDateFormat 提供线程安全的针对Date对象的格式化和日期字符串解析支持。此对象在实际使用中并不需要感知,相关操作已经封装在DateUtil和DateTime的相关方法中。
  • DateBetween 计算两个时间间隔的类,除了通过构造新对象使用外,相关操作也已封装在DateUtil和DateTime的相关方法中。
  • TimeInterval 一个简单的计时器类,常用于计算某段代码的执行时间,提供包括毫秒、秒、分、时、天、周等各种单位的花费时长计算,对象的静态构造已封装在DateUtil中。
  • DatePattern 提供常用的日期格式化模式,包括String类型和FastDateFormat两种类型。

# 日期枚举

考虑到Calendar类中表示时间的字段(field)都是使用int表示,在使用中非常不便,因此针对这些int字段,封装了与之对应的Enum枚举类,这些枚举类在DateUtil和DateTime相关方法中做为参数使用,可以更大限度的缩小参数限定范围。

这些定义的枚举值可以通过getValue()方法获得其与Calendar类对应的int值,通过of(int)方法从Calendar中int值转为枚举对象。

与Calendar对应的这些枚举包括:

  • Month 表示月份,与Calendar中的int值一一对应。
  • Week 表示周,与Calendar中的int值一一对应

# 月份枚举

通过月份枚举可以获得某个月的最后一天

// 31
int lastDay = Month.of(Calendar.JANUARY).getLastDay(false);

另外,Hutool还定义了季度枚举。Season.SPRING为第一季度,表示1~3月。季度的概念并不等同于季节,因为季节与月份并不对应,季度常用于统计概念。

# 时间枚举

时间枚举DateUnit主要表示某个时间单位对应的毫秒数,常用于计算时间差。

例如:DateUnit.MINUTE表示分,也表示一分钟的毫秒数,可以通过调用其getMillis()方法获得其毫秒数。

ID生成工具-IdUtil
日期时间工具-DateUtil

← ID生成工具-IdUtil 日期时间工具-DateUtil→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式