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,
DateParser dateParser,
boolean lenient)
构造
|
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 |
|---|---|
Between |
between(Date date)
计算相差时长
|
long |
between(Date date,
Fields.Units units)
计算相差时长
|
int |
dayOfMonth()
获得指定日期是这个日期所在月份的第几天
|
int |
dayOfWeek()
获得指定日期是星期几,1表示周日,2表示周一
|
Fields.Week |
dayOfWeekEnum()
获得指定日期是星期几
|
int |
dayOfWeekInMonth()
获得天所在的周是这个月的第几周
|
int |
dayOfYear()
获得指定日期是这个日期所在年份的第几天
|
static int |
get(TemporalAccessor temporalAccessor,
TemporalField field)
安全获取时间的某个属性,属性不存在返回0
|
int |
getField(Fields.Type field)
获得日期的某个部分
例如获得年的部分,则使用 getField(Calendar.YEAR)
|
int |
getField(int field)
获得日期的某个部分
例如获得年的部分,则使用 getField(Calendar.YEAR)
|
Fields.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 |
isMutable()
对象是否可变
如果为不可变对象,以下方法将返回新方法:
offset(Fields.Type, int)
setField(Fields.Type, int)
setField(int, int)
如果为不可变对象,setTime(long)将抛出异常 |
boolean |
isPM()
是否为下午
|
boolean |
isWeekend()
是否为周末,周末指周六或者周日
|
int |
millsecond()
获得指定日期的毫秒数部分
|
int |
minute()
获得指定日期的分钟数部分
例如:10:04:15.250 = 4
|
int |
month()
获得月份,从0开始计数
|
Fields.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)
构造
|
static LocalDateTime |
of(TemporalAccessor temporalAccessor)
TemporalAccessor转LocalDateTime,使用默认时区 |
DateTime |
offset(Fields.Type type,
int offset)
调整日期和时间
如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见
setMutable(boolean) |
DateTime |
offsetNew(Fields.Type type,
int offset)
调整日期和时间
返回调整后的新
DateTime,不影响原对象 |
int |
quarter()
获得当前日期所属季度,从1开始计数
|
Fields.Quarter |
quarterEnum()
获得当前日期所属季度
|
int |
second()
获得指定日期的秒数部分
|
DateTime |
setField(Fields.Type field,
int value)
设置日期的某个部分
如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见
setMutable(boolean) |
DateTime |
setField(int field,
int value)
设置日期的某个部分
如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见
setMutable(boolean) |
DateTime |
setFirstDayOfWeek(Fields.Week firstDayOfWeek)
设置一周的第一天
JDK的Calendar中默认一周的第一天是周日,将此默认值设置为周一
设置一周的第一天主要影响
weekOfMonth()和weekOfYear() 两个方法 |
DateTime |
setFirstWeekOfDays(int firstWeekOfDays)
设置第一周最少天数
|
DateTime |
setMutable(boolean mutable)
设置对象是否可变 如果为不可变对象,以下方法将返回新方法:
offset(Fields.Type, int)
setField(Fields.Type, 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 |
toDateString()
转为"yyyy-MM-dd" 格式字符串
|
Date |
toJdkDate()
转换为
Date
考虑到很多框架(例如Hibernate)的兼容性,提供此方法返回JDK原生的Date对象 |
String |
toMsString() |
Date |
toSqlDate()
转为
Date |
String |
toString()
转为"yyyy-MM-dd 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 yyyy-MM-dd HH:mm:ss " 格式字符串
如果时区不为
null,会转换为其时区对应的时间,否则转换为当前时间对应的时区 |
Timestamp |
toTimestamp()
|
String |
toTimeString()
转为"HH:mm:ss" 格式字符串
|
int |
weekOfMonth()
获得指定日期是所在月份的第几周
此方法返回值与一周的第一天有关,比如:
2016年1月3日为周日,如果一周的第一天为周日,那这天是第二周(返回2)
如果一周的第一天为周一,那这天是第一周(返回1)
|
int |
weekOfYear()
获得指定日期是所在年份的第几周
此方法返回值与一周的第一天有关,比如:
2016年1月3日为周日,如果一周的第一天为周日,那这天是第二周(返回2)
如果一周的第一天为周一,那这天是第一周(返回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, UTCpublic DateTime()
public DateTime(TimeZone timeZone)
timeZone - 时区public DateTime(Date date)
date - 日期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 - 格式public DateTime(CharSequence dateStr, DateFormat dateFormat)
dateStr - Date字符串dateFormat - 格式化器 SimpleDateFormatpublic DateTime(CharSequence dateStr, DateTimeFormatter formatter)
dateStr - Date字符串formatter - 格式化器,DateTimeFormatterpublic DateTime(CharSequence dateStr, DateParser dateParser)
dateStr - Date字符串dateParser - 格式化器 DateParser,可以使用 FormatBuilderpublic DateTime(CharSequence dateStr, DateParser dateParser, boolean lenient)
dateStr - Date字符串dateParser - 格式化器 DateParser,可以使用 Fieldslenient - 是否宽容模式public static DateTime of(long timeMillis)
timeMillis - 时间戳,毫秒数public static DateTime of(Date date)
date - JDK Datepublic static DateTime of(Calendar calendar)
Calendar 为 DateTimecalendar - Calendarpublic static DateTime of(String dateStr, String format)
dateStr - Date字符串format - 格式DateTimepublic static DateTime now()
public static LocalDateTime of(TemporalAccessor temporalAccessor)
TemporalAccessor转LocalDateTime,使用默认时区temporalAccessor - TemporalAccessorLocalDateTimepublic static int get(TemporalAccessor temporalAccessor, TemporalField field)
temporalAccessor - 需要获取的时间对象field - 需要获取的属性public DateTime offset(Fields.Type type, int offset)
setMutable(boolean)type - 调整的部分 Fields.Typeoffset - 偏移量,正数为向后偏移,负数为向前偏移public DateTime offsetNew(Fields.Type type, int offset)
DateTime,不影响原对象type - 调整的部分 Fields.Typeoffset - 偏移量,正数为向后偏移,负数为向前偏移public int getField(Fields.Type field)
field - 表示日期的哪个部分的枚举 Fields.Typepublic int getField(int field)
field - 表示日期的哪个部分的int值 Calendarpublic DateTime setField(Fields.Type field, int value)
setMutable(boolean)field - 表示日期的哪个部分的枚举 Fields.Typevalue - 值DateTimepublic DateTime setField(int field, int value)
setMutable(boolean)public int year()
public int quarter()
Fields.Quarterpublic Fields.Quarter quarterEnum()
Fields.Quarterpublic int month()
public int monthStartFromOne()
Calendar 中的月份按照0开始计数,导致某些需求容易误解,因此如果想用1表示一月,2表示二月则调用此方法public Fields.Month monthEnum()
Fields.Monthpublic int weekOfYear()
public int weekOfMonth()
public int dayOfMonth()
public int dayOfYear()
public int dayOfWeek()
public int dayOfWeekInMonth()
public Fields.Week dayOfWeekEnum()
Fields.Weekpublic int hour(boolean is24HourClock)
is24HourClock - 是否24小时制public int minute()
public int second()
public int millsecond()
public boolean isAM()
public boolean isPM()
public boolean isWeekend()
public Date toJdkDate()
Date
考虑到很多框架(例如Hibernate)的兼容性,提供此方法返回JDK原生的Date对象Datepublic long between(Date date, Fields.Units units)
date - 对比的日期units - 单位 Fields.Unitspublic 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 Fields.Week getFirstDayOfWeek()
public DateTime setFirstDayOfWeek(Fields.Week firstDayOfWeek)
weekOfMonth()和weekOfYear() 两个方法firstDayOfWeek - 一周的第一天weekOfMonth(),
weekOfYear()public TimeZone getTimeZone()
public DateTime setTimeZone(TimeZone timeZone)
timeZone - 时区public DateTime setFirstWeekOfDays(int firstWeekOfDays)
firstWeekOfDays - 第一周最少天数public ZoneId getZoneId()
public String toDateString()
public String toTimeString()
public String toMsString()
public String toString()
public String toString(TimeZone timeZone)
null,会转换为其时区对应的时间,否则转换为当前时间对应的时区timeZone - 时区public String toString(String format)
format - 日期格式,常用格式见: Fieldspublic String toString(DatePrinter format)
format - DatePrinter 或 FormatBuilderpublic String toString(DateFormat format)
format - SimpleDateFormatCopyright © 2022. All rights reserved.