public class DateTime extends Date
Constructor and Description |
---|
DateTime()
当前时间
|
DateTime(Calendar calendar)
给定日期的构造
|
DateTime(CharSequence dateStr,
DateFormat dateFormat)
构造
|
DateTime(CharSequence dateStr,
DateParser dateParser)
构造
|
DateTime(CharSequence dateStr,
DateTimeFormatter formatter)
构建DateTime对象
|
DateTime(CharSequence dateStr,
String format)
构造
|
DateTime(Date date)
给定日期的构造
|
DateTime(Date date,
TimeZone timeZone)
给定日期的构造
|
DateTime(Instant instant)
给定日期Instant的构造
|
DateTime(Instant instant,
ZoneId zoneId)
给定日期Instant的构造
|
DateTime(long timeMillis)
给定日期毫秒数的构造
|
DateTime(long timeMillis,
TimeZone timeZone)
给定日期毫秒数的构造
|
DateTime(TemporalAccessor temporalAccessor)
给定日期TemporalAccessor的构造
|
DateTime(TimeZone timeZone)
当前时间
|
DateTime(ZonedDateTime zonedDateTime)
给定日期ZonedDateTime的构造
|
Modifier and Type | Method and Description |
---|---|
DateBetween |
between(Date date)
计算相差时长
|
long |
between(Date date,
DateUnit unit)
计算相差时长
|
String |
between(Date date,
DateUnit unit,
BetweenFormatter.Level formatLevel)
计算相差时长
|
int |
dayOfMonth()
获得指定日期是这个日期所在月份的第几天,从1开始
|
int |
dayOfWeek()
获得指定日期是星期几,1表示周日,2表示周一
|
Week |
dayOfWeekEnum()
获得指定日期是星期几
|
int |
dayOfWeekInMonth()
获得天所在的周是这个月的第几周
|
int |
dayOfYear()
获得指定日期是这个日期所在年份的第几天,从1开始
|
int |
getField(DateField field)
获得日期的某个部分
例如获得年的部分,则使用 getField(DatePart.YEAR) |
int |
getField(int field)
获得日期的某个部分
例如获得年的部分,则使用 getField(Calendar.YEAR) |
Week |
getFirstDayOfWeek()
获得一周的第一天,默认为周一
|
TimeZone |
getTimeZone()
获取时区
|
ZoneId |
getZoneId()
获取时区ID
|
int |
hour(boolean is24HourClock)
获得指定日期的小时数部分
|
boolean |
isAfter(Date date)
是否在给定日期之后
|
boolean |
isAfterOrEquals(Date date)
是否在给定日期之后或与给定日期相等
|
boolean |
isAM()
是否为上午
|
boolean |
isBefore(Date date)
是否在给定日期之前
|
boolean |
isBeforeOrEquals(Date date)
是否在给定日期之前或与给定日期相等
|
boolean |
isIn(Date beginDate,
Date endDate)
当前日期是否在日期指定范围内
起始日期和结束日期可以互换 |
boolean |
isLeapYear()
是否闰年
|
boolean |
isMutable()
对象是否可变
如果为不可变对象,以下方法将返回新方法: offset(DateField, int)
setField(DateField, int)
setField(int, int)
如果为不可变对象,setTime(long) 将抛出异常 |
boolean |
isPM()
是否为下午
|
boolean |
isWeekend()
是否为周末,周末指周六或者周日
|
int |
millisecond()
获得指定日期的毫秒数部分
|
int |
millsecond()
Deprecated.
拼写错误,请使用
millisecond() |
int |
minute()
获得指定日期的分钟数部分
例如:10:04:15.250 =》 4 |
int |
month()
获得月份,从0开始计数
|
int |
monthBaseOne()
获取月,从1开始计数
|
Month |
monthEnum()
获得月份
|
int |
monthStartFromOne()
获得月份,从1开始计数
由于 Calendar 中的月份按照0开始计数,导致某些需求容易误解,因此如果想用1表示一月,2表示二月则调用此方法 |
static DateTime |
now()
现在的时间
|
static DateTime |
of(Calendar calendar)
转换
Calendar 为 DateTime |
static DateTime |
of(Date date)
转换JDK date为 DateTime
|
static DateTime |
of(long timeMillis)
转换时间戳为 DateTime
|
static DateTime |
of(String dateStr,
String format)
构造
|
DateTime |
offset(DateField datePart,
int offset)
调整日期和时间
如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见 setMutable(boolean) |
DateTime |
offsetNew(DateField datePart,
int offset)
调整日期和时间
返回调整后的新DateTime,不影响原对象 |
int |
quarter()
获得当前日期所属季度,从1开始计数
|
Quarter |
quarterEnum()
获得当前日期所属季度
|
int |
second()
获得指定日期的秒数部分
|
DateTime |
setField(DateField field,
int value)
设置日期的某个部分
如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见 setMutable(boolean) |
DateTime |
setField(int field,
int value)
设置日期的某个部分
如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见 setMutable(boolean) |
DateTime |
setFirstDayOfWeek(Week firstDayOfWeek)
|
DateTime |
setMutable(boolean mutable)
设置对象是否可变 如果为不可变对象,以下方法将返回新方法:
offset(DateField, int)
setField(DateField, int)
setField(int, int)
如果为不可变对象,setTime(long) 将抛出异常 |
void |
setTime(long time) |
DateTime |
setTimeZone(TimeZone timeZone)
设置时区
|
Calendar |
toCalendar()
转换为Calendar, 默认
Locale |
Calendar |
toCalendar(Locale locale)
转换为Calendar
|
Calendar |
toCalendar(TimeZone zone)
转换为Calendar
|
Calendar |
toCalendar(TimeZone zone,
Locale locale)
转换为Calendar
|
String |
toDateStr()
转为"yyyy-MM-dd" 格式字符串
|
Date |
toJdkDate()
转换为
Date 考虑到很多框架(例如Hibernate)的兼容性,提供此方法返回JDK原生的Date对象 |
String |
toMsStr() |
Date |
toSqlDate()
转为
Date |
String |
toString()
转为"yyyy-MM-dd HH:mm:ss" 格式字符串
如果时区被设置,会转换为其时区对应的时间,否则转换为当前地点对应的时区 |
String |
toString(DateFormat format)
转为字符串
|
String |
toString(DatePrinter format)
转为字符串
|
String |
toString(String format)
转为字符串
|
String |
toString(TimeZone timeZone)
转为"yyyy-MM-dd HH:mm:ss" 格式字符串
如果时区不为 null ,会转换为其时区对应的时间,否则转换为当前时间对应的时区 |
String |
toStringDefaultTimeZone()
转为"yyyy-MM-dd HH:mm:ss" 格式字符串
时区使用当前地区的默认时区 |
Timestamp |
toTimestamp()
|
String |
toTimeStr()
转为"HH:mm:ss" 格式字符串
|
int |
weekOfMonth()
获得指定日期是所在月份的第几周
此方法返回值与一周的第一天有关,比如: 2016年1月3日为周日,如果一周的第一天为周日,那这天是第二周(返回2) 如果一周的第一天为周一,那这天是第一周(返回1) |
int |
weekOfYear()
获得指定日期是所在年份的第几周
此方法返回值与一周的第一天有关,比如: 2016年1月3日为周日,如果一周的第一天为周日,那这天是第二周(返回2) 如果一周的第一天为周一,那这天是第一周(返回1) 跨年的那个星期得到的结果总是1 |
int |
year()
获得年的部分
|
after, before, clone, compareTo, equals, from, getDate, getDay, getHours, getMinutes, getMonth, getSeconds, getTime, getTimezoneOffset, getYear, hashCode, parse, setDate, setHours, setMinutes, setMonth, setSeconds, setYear, toGMTString, toInstant, toLocaleString, UTC
public DateTime()
public DateTime(TimeZone timeZone)
timeZone
- 时区public DateTime(Date date)
date
- 日期public DateTime(Date date, TimeZone timeZone)
date
- 日期timeZone
- 时区public DateTime(Instant instant)
instant
- Instant
对象public DateTime(Instant instant, ZoneId zoneId)
instant
- Instant
对象zoneId
- 时区IDpublic DateTime(TemporalAccessor temporalAccessor)
temporalAccessor
- TemporalAccessor
对象public DateTime(ZonedDateTime zonedDateTime)
zonedDateTime
- ZonedDateTime
对象public DateTime(long timeMillis)
timeMillis
- 日期毫秒数public DateTime(long timeMillis, TimeZone timeZone)
timeMillis
- 日期毫秒数timeZone
- 时区public DateTime(CharSequence dateStr, String format)
dateStr
- Date字符串format
- 格式DatePattern
public DateTime(CharSequence dateStr, DateFormat dateFormat)
dateStr
- Date字符串dateFormat
- 格式化器 SimpleDateFormat
DatePattern
public DateTime(CharSequence dateStr, DateTimeFormatter formatter)
dateStr
- Date字符串formatter
- 格式化器,DateTimeFormatter
public DateTime(CharSequence dateStr, DateParser dateParser)
dateStr
- Date字符串dateParser
- 格式化器 DateParser
,可以使用 FastDateFormat
DatePattern
public static DateTime of(long timeMillis)
timeMillis
- 时间戳,毫秒数public static DateTime of(Date date)
date
- JDK Datepublic static DateTime of(Calendar calendar)
Calendar
为 DateTimecalendar
- Calendar
public static DateTime of(String dateStr, String format)
dateStr
- Date字符串format
- 格式DatePattern
public static DateTime now()
public DateTime offset(DateField datePart, int offset)
setMutable(boolean)
datePart
- 调整的部分 DateField
offset
- 偏移量,正数为向后偏移,负数为向前偏移public DateTime offsetNew(DateField datePart, int offset)
datePart
- 调整的部分 DateField
offset
- 偏移量,正数为向后偏移,负数为向前偏移public int getField(DateField field)
field
- 表示日期的哪个部分的枚举 DateField
public int getField(int field)
field
- 表示日期的哪个部分的int值 Calendar
public DateTime setField(DateField field, int value)
setMutable(boolean)
field
- 表示日期的哪个部分的枚举 DateField
value
- 值public DateTime setField(int field, int value)
setMutable(boolean)
field
- 表示日期的哪个部分的int值 Calendar
value
- 值public int year()
public int quarter()
Quarter
public int month()
public int monthBaseOne()
public int monthStartFromOne()
Calendar
中的月份按照0开始计数,导致某些需求容易误解,因此如果想用1表示一月,2表示二月则调用此方法public int weekOfYear()
setFirstDayOfWeek(Week)
public int weekOfMonth()
setFirstDayOfWeek(Week)
public int dayOfMonth()
public int dayOfYear()
public int dayOfWeek()
public int dayOfWeekInMonth()
public int hour(boolean is24HourClock)
is24HourClock
- 是否24小时制public int minute()
public int second()
public int millisecond()
@Deprecated public int millsecond()
millisecond()
public boolean isAM()
public boolean isPM()
public boolean isWeekend()
public boolean isLeapYear()
DateUtil.isLeapYear(int)
public Date toJdkDate()
Date
Date
public DateBetween between(Date date)
date
- 对比的日期DateBetween
public long between(Date date, DateUnit unit)
date
- 对比的日期unit
- 单位 DateUnit
public String between(Date date, DateUnit unit, BetweenFormatter.Level formatLevel)
date
- 对比的日期unit
- 单位 DateUnit
formatLevel
- 格式化级别public boolean isIn(Date beginDate, Date endDate)
beginDate
- 起始日期endDate
- 结束日期public boolean isBefore(Date date)
date
- 日期public boolean isBeforeOrEquals(Date date)
date
- 日期public boolean isAfter(Date date)
date
- 日期public boolean isAfterOrEquals(Date date)
date
- 日期public boolean isMutable()
setTime(long)
将抛出异常public DateTime setMutable(boolean mutable)
setTime(long)
将抛出异常mutable
- 是否可变public Week getFirstDayOfWeek()
public DateTime setFirstDayOfWeek(Week firstDayOfWeek)
firstDayOfWeek
- 一周的第一天weekOfMonth()
,
weekOfYear()
public TimeZone getTimeZone()
public ZoneId getZoneId()
public DateTime setTimeZone(TimeZone timeZone)
timeZone
- 时区public String toString()
public String toStringDefaultTimeZone()
public String toString(TimeZone timeZone)
null
,会转换为其时区对应的时间,否则转换为当前时间对应的时区timeZone
- 时区public String toDateStr()
public String toTimeStr()
public String toString(String format)
format
- 日期格式,常用格式见: DatePattern
public String toString(DatePrinter format)
format
- DatePrinter
或 FastDateFormat
public String toString(DateFormat format)
format
- SimpleDateFormat
public String toMsStr()
Copyright © 2021. All rights reserved.