Class DateTime

java.lang.Object
java.util.Date
org.miaixz.bus.core.center.date.DateTime
All Implemented Interfaces:
Serializable, Cloneable, Comparable<Date>

public class DateTime extends Date
包装Date 此类继承了Date,并提供扩展方法,如时区等。 此类重写了父类的toString()方法,返回值为"yyyy-MM-dd HH:mm:ss"格式
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • DateTime

      public DateTime()
      当前时间
    • DateTime

      public DateTime(TimeZone timeZone)
      当前时间
      Parameters:
      timeZone - 时区,null表示默认时区
    • DateTime

      public DateTime(Date date)
      给定日期的构造
      Parameters:
      date - 日期
    • DateTime

      public DateTime(Date date, TimeZone timeZone)
      给定日期的构造
      Parameters:
      date - 日期,null表示当前时间
      timeZone - 时区,null表示默认时区
    • DateTime

      public DateTime(Calendar calendar)
      给定日期的构造
      Parameters:
      calendar - Calendar,不能为null
    • DateTime

      public DateTime(Instant instant)
      给定日期Instant的构造
      Parameters:
      instant - Instant 对象,不能为null
    • DateTime

      public DateTime(Instant instant, ZoneId zoneId)
      给定日期Instant的构造
      Parameters:
      instant - Instant 对象
      zoneId - 时区ID
    • DateTime

      public DateTime(TemporalAccessor temporalAccessor)
      给定日期TemporalAccessor的构造
      Parameters:
      temporalAccessor - TemporalAccessor 对象
    • DateTime

      public DateTime(ZonedDateTime zonedDateTime)
      给定日期ZonedDateTime的构造
      Parameters:
      zonedDateTime - ZonedDateTime 对象
    • DateTime

      public DateTime(long timeMillis)
      给定日期毫秒数的构造
      Parameters:
      timeMillis - 日期毫秒数
    • DateTime

      public DateTime(long timeMillis, TimeZone timeZone)
      给定日期毫秒数的构造
      Parameters:
      timeMillis - 日期毫秒数
      timeZone - 时区
    • DateTime

      public DateTime(CharSequence dateStr)
      构造格式:
      1. yyyy-MM-dd HH:mm:ss
      2. yyyy/MM/dd HH:mm:ss
      3. yyyy.MM.dd HH:mm:ss
      4. yyyy年MM月dd日 HH时mm分ss秒
      5. yyyy-MM-dd
      6. yyyy/MM/dd
      7. yyyy.MM.dd
      8. HH:mm:ss
      9. HH时mm分ss秒
      10. yyyy-MM-dd HH:mm
      11. yyyy-MM-dd HH:mm:ss.SSS
      12. yyyyMMddHHmmss
      13. yyyyMMddHHmmssSSS
      14. yyyyMMdd
      15. EEE, dd MMM yyyy HH:mm:ss z
      16. EEE MMM dd HH:mm:ss zzz yyyy
      17. yyyy-MM-dd'T'HH:mm:ss'Z'
      18. yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
      19. yyyy-MM-dd'T'HH:mm:ssZ
      20. yyyy-MM-dd'T'HH:mm:ss.SSSZ
      Parameters:
      dateStr - Date字符串
    • DateTime

      public DateTime(CharSequence dateStr, String format)
      构造
      Parameters:
      dateStr - Date字符串
      format - 格式
      See Also:
    • DateTime

      public DateTime(CharSequence dateStr, DateFormat dateFormat)
      构造
      Parameters:
      dateStr - Date字符串
      dateFormat - 格式化器 SimpleDateFormat
      See Also:
    • DateTime

      public DateTime(CharSequence dateStr, DateTimeFormatter formatter)
      构建DateTime对象
      Parameters:
      dateStr - Date字符串
      formatter - 格式化器,DateTimeFormatter
    • DateTime

      public DateTime(CharSequence dateStr, PositionDateParser dateParser)
      构造
      Parameters:
      dateStr - Date字符串
      dateParser - 格式化器 DateParser,可以使用 FormatBuilder
      See Also:
    • DateTime

      public DateTime(CharSequence dateStr, PositionDateParser dateParser, boolean lenient)
      构造
      Parameters:
      dateStr - Date字符串
      dateParser - 格式化器 DateParser,可以使用 FormatBuilder
      lenient - 是否宽容模式
      See Also:
  • Method Details

    • setUseJdkToStringStyle

      public static void setUseJdkToStringStyle(boolean customUseJdkToStringStyle)
      设置全局的,是否使用Date默认的toString()格式 如果为true,则调用toString()时返回"EEE MMM dd HH:mm:ss zzz yyyy"格式, 如果为false,则返回"yyyy-MM-dd HH:mm:ss", 默认为false
      Parameters:
      customUseJdkToStringStyle - 是否使用Date默认的toString()格式
    • of

      public static DateTime of(long timeMillis)
      转换时间戳为 DateTime
      Parameters:
      timeMillis - 时间戳,毫秒数
      Returns:
      DateTime
    • of

      public static DateTime of(Date date)
      转换JDK date为 DateTime
      Parameters:
      date - JDK Date
      Returns:
      DateTime
    • of

      public static DateTime of(Calendar calendar)
      转换 Calendar 为 DateTime
      Parameters:
      calendar - Calendar
      Returns:
      DateTime
    • of

      public static DateTime of(String dateStr, String format)
      构造
      Parameters:
      dateStr - Date字符串
      format - 格式
      Returns:
      this
      See Also:
    • now

      public static DateTime now()
      现在的时间
      Returns:
      现在的时间
    • offset

      public DateTime offset(Fields.Type datePart, int offset)
      调整日期和时间 如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见setMutable(boolean)
      Parameters:
      datePart - 调整的部分 Fields.Type
      offset - 偏移量,正数为向后偏移,负数为向前偏移
      Returns:
      如果此对象为可变对象,返回自身,否则返回新对象
    • offsetNew

      public DateTime offsetNew(Fields.Type datePart, int offset)
      调整日期和时间 返回调整后的新DateTime,不影响原对象
      Parameters:
      datePart - 调整的部分 Fields.Type
      offset - 偏移量,正数为向后偏移,负数为向前偏移
      Returns:
      如果此对象为可变对象,返回自身,否则返回新对象
    • getField

      public int getField(Fields.Type field)
      获得日期的某个部分 例如获得年的部分,则使用 getField(DatePart.YEAR)
      Parameters:
      field - 表示日期的哪个部分的枚举 Fields.Type
      Returns:
      某个部分的值
    • getField

      public int getField(int field)
      获得日期的某个部分 例如获得年的部分,则使用 getField(Calendar.YEAR)
      Parameters:
      field - 表示日期的哪个部分的int值 Calendar
      Returns:
      某个部分的值
    • setField

      public DateTime setField(Fields.Type field, int value)
      设置日期的某个部分 如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见setMutable(boolean)
      Parameters:
      field - 表示日期的哪个部分的枚举 Fields.Type
      value - 值
      Returns:
      this
    • setField

      public DateTime setField(int field, int value)
      设置日期的某个部分 如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见setMutable(boolean)
      Parameters:
      field - 表示日期的哪个部分的int值 Calendar
      value - 值
      Returns:
      this
    • setTime

      public void setTime(long time)
      Overrides:
      setTime in class Date
    • year

      public int year()
      获得年的部分
      Returns:
      年的部分
    • quarter

      public int quarter()
      获得当前日期所属季度,从1开始计数
      Returns:
      第几个季度 Fields.Quarter
    • quarterEnum

      public Fields.Quarter quarterEnum()
      获得当前日期所属季度
      Returns:
      第几个季度 Fields.Quarter
    • month

      public int month()
      获得月份,从0开始计数
      Returns:
      月份
    • monthBaseOne

      public int monthBaseOne()
      获取月,从1开始计数
      Returns:
      月份,1表示一月
    • monthStartFromOne

      public int monthStartFromOne()
      获得月份,从1开始计数 由于Calendar 中的月份按照0开始计数,导致某些需求容易误解,因此如果想用1表示一月,2表示二月则调用此方法
      Returns:
      月份
    • monthEnum

      public Fields.Month monthEnum()
      获得月份
      Returns:
      Fields.Month
    • weekOfYear

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

      public int weekOfMonth()
      获得指定日期是所在月份的第几周 此方法返回值与一周的第一天有关,比如: 2016年1月3日为周日,如果一周的第一天为周日,那这天是第二周(返回2) 如果一周的第一天为周一,那这天是第一周(返回1)
      Returns:
      See Also:
    • dayOfMonth

      public int dayOfMonth()
      获得指定日期是这个日期所在月份的第几天,从1开始
      Returns:
      天,1表示第一天
    • dayOfYear

      public int dayOfYear()
      获得指定日期是这个日期所在年份的第几天,从1开始
      Returns:
      天,1表示第一天
    • dayOfWeek

      public int dayOfWeek()
      获得指定日期是星期几,1表示周日,2表示周一
      Returns:
      星期几
    • dayOfWeekInMonth

      public int dayOfWeekInMonth()
      获得天所在的周是这个月的第几周
      Returns:
    • dayOfWeekEnum

      public Fields.Week dayOfWeekEnum()
      获得指定日期是星期几
      Returns:
      Fields.Week
    • hour

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

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

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

      public int millisecond()
      获得指定日期的毫秒数部分
      Returns:
      毫秒数
    • isAM

      public boolean isAM()
      是否为上午
      Returns:
      是否为上午
    • isPM

      public boolean isPM()
      是否为下午
      Returns:
      是否为下午
    • isWeekend

      public boolean isWeekend()
      是否为周末,周末指周六或者周日
      Returns:
      是否为周末,周末指周六或者周日
    • isLeapYear

      public boolean isLeapYear()
      是否闰年
      Returns:
      是否闰年
      See Also:
    • toCalendar

      public Calendar toCalendar()
      转换为Calendar, 默认 Locale
      Returns:
      Calendar
    • toCalendar

      public Calendar toCalendar(Locale locale)
      转换为Calendar
      Parameters:
      locale - 地域 Locale
      Returns:
      Calendar
    • toCalendar

      public Calendar toCalendar(TimeZone zone)
      转换为Calendar
      Parameters:
      zone - 时区 TimeZone
      Returns:
      Calendar
    • toCalendar

      public Calendar toCalendar(TimeZone zone, Locale locale)
      转换为Calendar
      Parameters:
      zone - 时区 TimeZone
      locale - 地域 Locale
      Returns:
      Calendar
    • toJdkDate

      public Date toJdkDate()
      转换为 Date 考虑到很多框架(例如Hibernate)的兼容性,提供此方法返回JDK原生的Date对象
      Returns:
      Date
    • toLocalDateTime

      public LocalDateTime toLocalDateTime()
      转换为 LocalDateTime
      Returns:
      LocalDateTime
    • between

      public Between between(Date date)
      计算相差时长
      Parameters:
      date - 对比的日期
      Returns:
      Between
    • between

      public long between(Date date, Fields.Units unit)
      计算相差时长
      Parameters:
      date - 对比的日期
      unit - 单位 Fields.Units
      Returns:
      相差时长
    • between

      public String between(Date date, Fields.Units unit, FormatPeriod.Level formatLevel)
      计算相差时长
      Parameters:
      date - 对比的日期
      unit - 单位 Fields.Units
      formatLevel - 格式化级别
      Returns:
      相差时长
    • isIn

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

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

      public 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:
      是否在范围内
    • isBefore

      public boolean isBefore(Date date)
      是否在给定日期之前
      Parameters:
      date - 日期
      Returns:
      是否在给定日期之前
    • isBeforeOrEquals

      public boolean isBeforeOrEquals(Date date)
      是否在给定日期之前或与给定日期相等
      Parameters:
      date - 日期
      Returns:
      是否在给定日期之前或与给定日期相等
    • isAfter

      public boolean isAfter(Date date)
      是否在给定日期之后
      Parameters:
      date - 日期
      Returns:
      是否在给定日期之后
    • isAfterOrEquals

      public boolean isAfterOrEquals(Date date)
      是否在给定日期之后或与给定日期相等
      Parameters:
      date - 日期
      Returns:
      是否在给定日期之后或与给定日期相等
    • isMutable

      public boolean isMutable()
      对象是否可变 如果为不可变对象,以下方法将返回新方法: 如果为不可变对象,setTime(long)将抛出异常
      Returns:
      对象是否可变
    • setMutable

      public DateTime setMutable(boolean mutable)
      设置对象是否可变 如果为不可变对象,以下方法将返回新方法: 如果为不可变对象,setTime(long)将抛出异常
      Parameters:
      mutable - 是否可变
      Returns:
      this
    • getFirstDayOfWeek

      public Fields.Week getFirstDayOfWeek()
      获得一周的第一天,默认为周一
      Returns:
      一周的第一天
    • setFirstDayOfWeek

      public DateTime setFirstDayOfWeek(Fields.Week firstDayOfWeek)
      设置一周的第一天 JDK的Calendar中默认一周的第一天是周日,将此默认值设置为周一 设置一周的第一天主要影响weekOfMonth()weekOfYear() 两个方法
      Parameters:
      firstDayOfWeek - 一周的第一天
      Returns:
      this
      See Also:
    • getTimeZone

      public TimeZone getTimeZone()
      获取时区
      Returns:
      时区
    • setTimeZone

      public DateTime setTimeZone(TimeZone timeZone)
      设置时区
      Parameters:
      timeZone - 时区
      Returns:
      this
    • getZoneId

      public ZoneId getZoneId()
      获取时区ID
      Returns:
      时区ID
    • setMinimalDaysInFirstWeek

      public DateTime setMinimalDaysInFirstWeek(int minimalDaysInFirstWeek)
      设置第一周最少天数
      Parameters:
      minimalDaysInFirstWeek - 第一周最少天数
      Returns:
      this
    • isLastDayOfMonth

      public boolean isLastDayOfMonth()
      是否为本月最后一天
      Returns:
      是否为本月最后一天
    • getLastDayOfMonth

      public int getLastDayOfMonth()
      获得本月的最后一天
      Returns:
    • toString

      public String toString()
      转为字符串,如果时区被设置,会转换为其时区对应的时间,否则转换为当前地点对应的时区 可以调用setUseJdkToStringStyle(boolean) 方法自定义默认的风格 如果useJdkToStringStyletrue,返回"EEE MMM dd HH:mm:ss zzz yyyy"格式, 如果为false,则返回"yyyy-MM-dd HH:mm:ss"
      Overrides:
      toString in class Date
      Returns:
      格式字符串
    • toStringDefaultTimeZone

      public String toStringDefaultTimeZone()
      转为"yyyy-MM-dd HH:mm:ss" 格式字符串 时区使用当前地区的默认时区
      Returns:
      "yyyy-MM-dd HH:mm:ss" 格式字符串
    • toString

      public String toString(TimeZone timeZone)
      转为"yyyy-MM-dd HH:mm:ss" 格式字符串 如果时区不为null,会转换为其时区对应的时间,否则转换为当前时间对应的时区
      Parameters:
      timeZone - 时区
      Returns:
      "yyyy-MM-dd HH:mm:ss" 格式字符串
    • toDateString

      public String toDateString()
      转为"yyyy-MM-dd" 格式字符串
      Returns:
      "yyyy-MM-dd" 格式字符串
    • toTimeString

      public String toTimeString()
      转为"HH:mm:ss" 格式字符串
      Returns:
      "HH:mm:ss" 格式字符串
    • toString

      public String toString(String format)
      转为字符串
      Parameters:
      format - 日期格式,常用格式见: Fields
      Returns:
      String
    • toString

      public String toString(FormatPrinter format)
      转为字符串
      Parameters:
      format - FormatPrinterFormatBuilder
      Returns:
      String
    • toString

      public String toString(DateFormat format)
      转为字符串
      Parameters:
      format - SimpleDateFormat
      Returns:
      String
    • toMsString

      public String toMsString()
      Returns:
      输出精确到毫秒的标准日期形式