Class DateTime

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

public class DateTime extends Date
包装java.util.Date
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • DateTime

      public DateTime()
      当前时间
    • DateTime

      public DateTime(TimeZone timeZone)
      当前时间
      Parameters:
      timeZone - 时区
    • DateTime

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

      public DateTime(Date date, TimeZone timeZone)
      给定日期的构造
      Parameters:
      date - 日期
      timeZone - 时区
    • DateTime

      public DateTime(Calendar calendar)
      给定日期的构造
      Parameters:
      calendar - Calendar
    • DateTime

      public DateTime(Instant instant)
      给定日期Instant的构造
      Parameters:
      instant - Instant 对象
    • 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, String format)
      构造
      Parameters:
      dateStr - Date字符串
      format - 格式
    • DateTime

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

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

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

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

    • 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:
      DateTime
    • now

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

      public static LocalDateTime of(TemporalAccessor temporalAccessor)
      TemporalAccessorLocalDateTime,使用默认时区
      Parameters:
      temporalAccessor - TemporalAccessor
      Returns:
      LocalDateTime
    • get

      public static int get(TemporalAccessor temporalAccessor, TemporalField field)
      安全获取时间的某个属性,属性不存在返回0
      Parameters:
      temporalAccessor - 需要获取的时间对象
      field - 需要获取的属性
      Returns:
      时间的值,如果无法获取则默认为 0
    • offset

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

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

      public int getField(Fields.Type field)
      获得日期的某个部分 例如获得年的部分,则使用 getField(Calendar.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:
      DateTime
    • setField

      public DateTime setField(int field, int value)
      设置日期的某个部分 如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见setMutable(boolean)
      Parameters:
      field - 表示日期的哪个部分的int值 Calendar
      value - 值
      Returns:
      DateTime
    • 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:
      月份
    • 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)
      Returns:
    • weekOfMonth

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

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

      public int dayOfYear()
      获得指定日期是这个日期所在年份的第几天
      Returns:
    • 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:
      秒数
    • millsecond

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

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

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

      public boolean isWeekend()
      是否为周末,周末指周六或者周日
      Returns:
      是否为周末,周末指周六或者周日
    • 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
    • toTimestamp

      public Timestamp toTimestamp()
      转为Timestamp
      Returns:
      Timestamp
    • toSqlDate

      public Date toSqlDate()
      转为 Date
      Returns:
      Date
    • between

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

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

      public boolean isIn(Date beginDate, Date endDate)
      当前日期是否在日期指定范围内 起始日期和结束日期可以互换
      Parameters:
      beginDate - 起始日期
      endDate - 结束日期
      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
    • setFirstWeekOfDays

      public DateTime setFirstWeekOfDays(int firstWeekOfDays)
      设置第一周最少天数
      Parameters:
      firstWeekOfDays - 第一周最少天数
      Returns:
      this
    • getZoneId

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

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

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

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

      public String toString()
      转为"yyyy-MM-dd yyyy-MM-dd HH:mm:ss " 格式字符串 如果时区被设置,会转换为其时区对应的时间,否则转换为当前地点对应的时区
      Overrides:
      toString in class Date
      Returns:
      "yyyy-MM-dd yyyy-MM-dd HH:mm:ss " 格式字符串
    • toString

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

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

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

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