public class DateUtil extends Object
| 构造器和说明 |
|---|
DateUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static int |
age(Date birthDay,
Date dateToCompare)
计算相对于dateToCompare的年龄,长用于计算指定生日在某年的年龄
|
static int |
ageOfNow(Date birthDay)
生日转为年龄,计算法定年龄
|
static int |
ageOfNow(String birthDay)
生日转为年龄,计算法定年龄
|
static Calendar |
beginOfDay(Calendar calendar)
获取某天的开始时间
|
static DateTime |
beginOfDay(Date date)
获取某天的开始时间
|
static Calendar |
beginOfMonth(Calendar calendar)
获取某月的开始时间
|
static DateTime |
beginOfMonth(Date date)
获取某月的开始时间
|
static Calendar |
beginOfQuarter(Calendar calendar)
获取某季度的开始时间
|
static DateTime |
beginOfQuarter(Date date)
获取某季度的开始时间
|
static Calendar |
beginOfWeek(Calendar calendar)
获取某周的开始时间
|
static Calendar |
beginOfWeek(Calendar calendar,
boolean isMondayAsFirstDay)
获取某周的开始时间,周一定为一周的开始时间
|
static DateTime |
beginOfWeek(Date date)
获取某周的开始时间
|
static Calendar |
beginOfYear(Calendar calendar)
获取某年的开始时间
|
static DateTime |
beginOfYear(Date date)
获取某年的开始时间
|
static long |
between(Date beginDate,
Date endDate,
DateUnit unit)
判断两个日期相差的时长,只保留绝对值
|
static long |
between(Date beginDate,
Date endDate,
DateUnit unit,
boolean isAbs)
判断两个日期相差的时长
|
static long |
betweenDay(Date beginDate,
Date endDate,
boolean isReset)
判断两个日期相差的天数
有时候我们计算相差天数的时候需要忽略时分秒。 |
static long |
betweenMonth(Date beginDate,
Date endDate,
boolean isReset)
计算两个日期相差月数
在非重置情况下,如果起始日期的天小于结束日期的天,月数要少算1(不足1个月) |
static long |
betweenMs(Date beginDate,
Date endDate)
判断两个日期相差的毫秒数
|
static long |
betweenYear(Date beginDate,
Date endDate,
boolean isReset)
计算两个日期相差年数
在非重置情况下,如果起始日期的月小于结束日期的月,年数要少算1(不足1年) |
static Calendar |
calendar(Date date)
转换为Calendar对象
|
static Calendar |
calendar(long millis)
转换为Calendar对象
|
static long |
current(boolean isNano)
当前时间long
|
static long |
currentSeconds()
当前时间秒数
|
static DateTime |
date()
转换为
DateTime对象 |
static DateTime |
date(Calendar calendar)
|
static DateTime |
date(Date date)
|
static DateTime |
date(long date)
Long类型时间转为
DateTime只支持毫秒级别时间戳,如果需要秒级别时间戳,请自行×1000 |
static int |
dayOfMonth(Date date)
获得指定日期是这个日期所在月份的第几天
|
static int |
dayOfWeek(Date date)
获得指定日期是星期几,1表示周日,2表示周一
|
static Week |
dayOfWeekEnum(Date date)
获得指定日期是星期几
|
static Calendar |
endOfDay(Calendar calendar)
获取某天的结束时间
|
static DateTime |
endOfDay(Date date)
获取某天的结束时间
|
static Calendar |
endOfMonth(Calendar calendar)
获取某月的结束时间
|
static DateTime |
endOfMonth(Date date)
获取某月的结束时间
|
static Calendar |
endOfQuarter(Calendar calendar)
获取某季度的结束时间
|
static DateTime |
endOfQuarter(Date date)
获取某季度的结束时间
|
static Calendar |
endOfWeek(Calendar calendar)
获取某周的结束时间,周日定为一周的结束
|
static Calendar |
endOfWeek(Calendar calendar,
boolean isSundayAsLastDay)
获取某周的结束时间
|
static DateTime |
endOfWeek(Date date)
获取某周的结束时间
|
static Calendar |
endOfYear(Calendar calendar)
获取某年的结束时间
|
static DateTime |
endOfYear(Date date)
获取某年的结束时间
|
static String |
format(Date date,
DateFormat format)
根据特定格式格式化日期
|
static String |
format(Date date,
DatePrinter format)
根据特定格式格式化日期
|
static String |
format(Date date,
String format)
根据特定格式格式化日期
|
static String |
formatBetween(Date beginDate,
Date endDate)
格式化日期间隔输出,精确到毫秒
|
static String |
formatBetween(Date beginDate,
Date endDate,
BetweenFormater.Level level)
格式化日期间隔输出
|
static String |
formatBetween(long betweenMs)
格式化日期间隔输出,精确到毫秒
|
static String |
formatBetween(long betweenMs,
BetweenFormater.Level level)
格式化日期间隔输出
|
static String |
formatDate(Date date)
格式化日期部分(不包括时间)
格式 yyyy-MM-dd |
static String |
formatDateTime(Date date)
格式化日期时间
格式 yyyy-MM-dd HH:mm:ss |
static String |
formatHttpDate(Date date)
格式化为Http的标准日期格式
|
static String |
formatTime(Date date)
格式化时间
格式 HH:mm:ss |
static int |
hour(Date date,
boolean is24HourClock)
获得指定日期的小时数部分
|
static boolean |
isAM(Date date)
是否为上午
|
static boolean |
isExpired(Date startDate,
DateField dateField,
int timeLength,
Date checkedDate)
判定给定开始时间经过某段时间后是否过期
|
static boolean |
isIn(Date date,
Date beginDate,
Date endDate)
当前日期是否在日期指定范围内
起始日期和结束日期可以互换 |
static boolean |
isLeapYear(int year)
是否闰年
|
static boolean |
isPM(Date date)
是否为下午
|
static boolean |
isSameDay(Calendar cal1,
Calendar cal2)
比较两个日期是否为同一天
|
static boolean |
isSameDay(Date date1,
Date date2)
比较两个日期是否为同一天
|
static boolean |
isSameTime(Date date1,
Date date2)
是否为相同时间
|
static DateTime |
lastMonth()
上个月
|
static DateTime |
lastWeek()
上周
|
static int |
millsecond(Date date)
获得指定日期的毫秒数部分
|
static int |
minute(Date date)
获得指定日期的分钟数部分
例如:10:04:15.250 =》 4 |
static int |
month(Date date)
获得月份,从0开始计数
|
static Month |
monthEnum(Date date)
获得月份
|
static DateTime |
nextMonth()
下个月
|
static DateTime |
nextWeek()
下周
|
static String |
now()
当前时间,格式 yyyy-MM-dd HH:mm:ss
|
static DateTime |
offset(Date date,
DateField dateField,
int offset)
获取指定日期偏移指定时间后的时间
|
static DateTime |
offsetDate(Date date,
DateField dateField,
int offset)
已过时。
please use
offset(Date, DateField, int) |
static DateTime |
offsetDay(Date date,
int offset)
偏移天
|
static DateTime |
offsetHour(Date date,
int offset)
偏移小时
|
static DateTime |
offsetMillisecond(Date date,
int offset)
偏移毫秒数
|
static DateTime |
offsetMinute(Date date,
int offset)
偏移分钟
|
static DateTime |
offsetMonth(Date date,
int offset)
偏移月
|
static DateTime |
offsetSecond(Date date,
int offset)
偏移秒数
|
static DateTime |
offsetWeek(Date date,
int offset)
偏移周
|
static DateTime |
parse(String dateStr)
将日期字符串转换为
DateTime对象,格式:yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss yyyy.MM.dd HH:mm:ss yyyy年MM月dd日 HH时mm分ss秒 yyyy-MM-dd yyyy/MM/dd yyyy.MM.dd HH:mm:ss HH时mm分ss秒 yyyy-MM-dd HH:mm yyyy-MM-dd HH:mm:ss.SSS yyyyMMddHHmmss yyyyMMddHHmmssSSS yyyyMMdd EEE, dd MMM yyyy HH:mm:ss z EEE MMM dd HH:mm:ss zzz yyyy |
static DateTime |
parse(String dateStr,
DateFormat dateFormat)
构建DateTime对象
|
static DateTime |
parse(String dateStr,
DateParser parser)
构建DateTime对象
|
static DateTime |
parse(String dateStr,
String format)
将特定格式的日期转换为Date对象
|
static DateTime |
parseDate(String dateString)
格式yyyy-MM-dd
|
static DateTime |
parseDateTime(String dateString)
格式yyyy-MM-dd HH:mm:ss
|
static DateTime |
parseTime(String timeString)
解析时间,格式HH:mm:ss,默认为1970-01-01
|
static DateTime |
parseTimeToday(String timeString)
解析时间,格式HH:mm:ss,日期默认为今天
|
static DateTime |
parseUTC(String utcString)
解析UTC时间,格式为:yyyy-MM-dd'T'HH:mm:ss'Z
|
static int |
quarter(Date date)
获得指定日期所属季度,从1开始计数
|
static Quarter |
quarterEnum(Date date)
获得指定日期所属季度
|
static DateRange |
range(Date start,
Date end,
DateField unit)
创建日期范围生成器
|
static List<DateTime> |
rangeToList(Date start,
Date end,
DateField unit)
创建日期范围生成器
|
static int |
season(Date date)
已过时。
|
static int |
second(Date date)
获得指定日期的秒数部分
|
static String |
secondToTime(int seconds)
秒数转为时间格式(HH:mm:ss)
参考:https://github.com/iceroot |
static long |
spendMs(long preTime)
计时,常用于记录某段代码的执行时间,单位:毫秒
|
static long |
spendNt(long preTime)
计时,常用于记录某段代码的执行时间,单位:纳秒
|
static int |
thisDayOfMonth() |
static int |
thisDayOfWeek() |
static Week |
thisDayOfWeekEnum() |
static int |
thisHour(boolean is24HourClock) |
static int |
thisMillsecond() |
static int |
thisMinute() |
static int |
thisMonth() |
static Month |
thisMonthEnum() |
static int |
thisSecond() |
static int |
thisWeekOfMonth() |
static int |
thisWeekOfYear() |
static int |
thisYear() |
static TimeInterval |
timer()
计时器
计算某个过程花费的时间,精确到毫秒 |
static int |
timeToSecond(String timeStr)
时间格式字符串转为秒数
参考:https://github.com/iceroot |
static String |
today()
当前日期,格式 yyyy-MM-dd
|
static int |
toIntSecond(Date date)
格式化成yyMMddHHmm后转换为int型
|
static DateTime |
tomorrow()
明天
|
static int |
weekCount(Date start,
Date end)
计算指定指定时间区间内的周数
|
static int |
weekOfMonth(Date date)
获得指定日期是所在月份的第几周
|
static int |
weekOfYear(Date date)
获得指定日期是所在年份的第几周
|
static int |
year(Date date)
获得年的部分
|
static String |
yearAndQuarter(Date date)
获得指定日期年份和季节
格式:[20131]表示2013年第一季度 |
static LinkedHashSet<String> |
yearAndQuarter(Date startDate,
Date endDate)
获得指定日期区间内的年份和季节
|
static String |
yearAndSeason(Date date)
已过时。
|
static LinkedHashSet<String> |
yearAndSeasons(Date startDate,
Date endDate)
已过时。
|
static DateTime |
yesterday()
昨天
|
public static DateTime date(long date)
DateTimedate - Long类型Date(Unix时间戳)public static Calendar calendar(long millis)
millis - 时间戳public static String now()
public static long current(boolean isNano)
isNano - 是否为高精度时间public static long currentSeconds()
public static String today()
public static int year(Date date)
date - 日期@Deprecated public static int season(Date date)
quarter(Date)date - 日期public static int quarter(Date date)
date - 日期public static Quarter quarterEnum(Date date)
date - 日期public static int month(Date date)
date - 日期public static int weekOfYear(Date date)
date - 日期public static int weekOfMonth(Date date)
date - 日期public static int dayOfMonth(Date date)
date - 日期public static int dayOfWeek(Date date)
date - 日期public static int hour(Date date, boolean is24HourClock)
date - 日期is24HourClock - 是否24小时制public static int minute(Date date)
date - 日期public static int second(Date date)
date - 日期public static int millsecond(Date date)
date - 日期public static boolean isAM(Date date)
date - 日期public static boolean isPM(Date date)
date - 日期public static int thisYear()
public static int thisMonth()
public static int thisWeekOfYear()
public static int thisWeekOfMonth()
public static int thisDayOfMonth()
public static int thisDayOfWeek()
public static int thisHour(boolean is24HourClock)
is24HourClock - 是否24小时制public static int thisMinute()
public static int thisSecond()
public static int thisMillsecond()
@Deprecated public static String yearAndSeason(Date date)
yearAndQuarter(java.util.Date) 代替date - 日期public static String yearAndQuarter(Date date)
date - 日期@Deprecated public static LinkedHashSet<String> yearAndSeasons(Date startDate, Date endDate)
yearAndQuarter(java.util.Date) 代替startDate - 起始日期(包含)endDate - 结束日期(包含)public static LinkedHashSet<String> yearAndQuarter(Date startDate, Date endDate)
startDate - 起始日期(包含)endDate - 结束日期(包含)public static String format(Date date, String format)
date - 被格式化的日期format - 日期格式,常用格式见: DatePatternpublic static String format(Date date, DatePrinter format)
date - 被格式化的日期format - DatePrinter 或 FastDateFormatpublic static String format(Date date, DateFormat format)
date - 被格式化的日期format - SimpleDateFormatpublic static String formatDateTime(Date date)
date - 被格式化的日期public static String formatDate(Date date)
date - 被格式化的日期public static String formatTime(Date date)
date - 被格式化的日期public static String formatHttpDate(Date date)
date - 被格式化的日期public static DateTime parse(String dateStr, DateFormat dateFormat)
dateStr - Date字符串dateFormat - 格式化器 SimpleDateFormatpublic static DateTime parse(String dateStr, DateParser parser)
dateStr - Date字符串parser - 格式化器,FastDateFormatpublic static DateTime parse(String dateStr, String format)
dateStr - 特定格式的日期format - 格式,例如yyyy-MM-ddpublic static DateTime parseDateTime(String dateString)
dateString - 标准形式的时间字符串public static DateTime parseDate(String dateString)
dateString - 标准形式的日期字符串public static DateTime parseTime(String timeString)
timeString - 标准形式的日期字符串public static DateTime parseTimeToday(String timeString)
timeString - 标准形式的日期字符串public static DateTime parseUTC(String utcString)
utcString - UTC时间public static DateTime parse(String dateStr)
DateTime对象,格式:dateStr - 日期字符串public static Calendar beginOfWeek(Calendar calendar, boolean isMondayAsFirstDay)
public static DateTime yesterday()
public static DateTime tomorrow()
public static DateTime lastWeek()
public static DateTime nextWeek()
public static DateTime lastMonth()
public static DateTime nextMonth()
public static DateTime offsetMillisecond(Date date, int offset)
date - 日期offset - 偏移毫秒数,正数向未来偏移,负数向历史偏移public static DateTime offsetSecond(Date date, int offset)
date - 日期offset - 偏移秒数,正数向未来偏移,负数向历史偏移public static DateTime offsetMinute(Date date, int offset)
date - 日期offset - 偏移分钟数,正数向未来偏移,负数向历史偏移public static DateTime offsetHour(Date date, int offset)
date - 日期offset - 偏移小时数,正数向未来偏移,负数向历史偏移public static DateTime offsetDay(Date date, int offset)
date - 日期offset - 偏移天数,正数向未来偏移,负数向历史偏移public static DateTime offsetWeek(Date date, int offset)
date - 日期offset - 偏移周数,正数向未来偏移,负数向历史偏移public static DateTime offsetMonth(Date date, int offset)
date - 日期offset - 偏移月数,正数向未来偏移,负数向历史偏移public static DateTime offset(Date date, DateField dateField, int offset)
date - 基准日期dateField - 偏移的粒度大小(小时、天、月等)DateFieldoffset - 偏移量,正数为向后偏移,负数为向前偏移@Deprecated public static DateTime offsetDate(Date date, DateField dateField, int offset)
offset(Date, DateField, int)date - 基准日期dateField - 偏移的粒度大小(小时、天、月等)DateFieldoffset - 偏移量,正数为向后偏移,负数为向前偏移public static long between(Date beginDate, Date endDate, DateUnit unit)
beginDate - 起始日期endDate - 结束日期unit - 相差的单位:相差 天DateUnit.DAY、小时DateUnit.HOUR 等public static long between(Date beginDate, Date endDate, DateUnit unit, boolean isAbs)
beginDate - 起始日期endDate - 结束日期unit - 相差的单位:相差 天DateUnit.DAY、小时DateUnit.HOUR 等isAbs - 日期间隔是否只保留绝对值正数public static long betweenMs(Date beginDate, Date endDate)
beginDate - 起始日期endDate - 结束日期public static long betweenDay(Date beginDate, Date endDate, boolean isReset)
有时候我们计算相差天数的时候需要忽略时分秒。 比如:2016-02-01 23:59:59和2016-02-02 00:00:00相差一秒 如果isReset为false相差天数为0。 如果isReset为true相差天数将被计算为1
beginDate - 起始日期endDate - 结束日期isReset - 是否重置时间为起始时间public static long betweenMonth(Date beginDate, Date endDate, boolean isReset)
beginDate - 起始日期endDate - 结束日期isReset - 是否重置时间为起始时间(重置天时分秒)public static long betweenYear(Date beginDate, Date endDate, boolean isReset)
beginDate - 起始日期endDate - 结束日期isReset - 是否重置时间为起始时间(重置月天时分秒)public static String formatBetween(Date beginDate, Date endDate, BetweenFormater.Level level)
beginDate - 起始日期endDate - 结束日期level - 级别,按照天、小时、分、秒、毫秒分为5个等级public static String formatBetween(Date beginDate, Date endDate)
beginDate - 起始日期endDate - 结束日期public static String formatBetween(long betweenMs, BetweenFormater.Level level)
betweenMs - 日期间隔level - 级别,按照天、小时、分、秒、毫秒分为5个等级public static String formatBetween(long betweenMs)
betweenMs - 日期间隔public static boolean isIn(Date date, Date beginDate, Date endDate)
date - 被检查的日期beginDate - 起始日期endDate - 结束日期public static boolean isSameTime(Date date1, Date date2)
date1 - 日期1date2 - 日期2public static boolean isSameDay(Date date1, Date date2)
date1 - 日期1date2 - 日期2public static boolean isSameDay(Calendar cal1, Calendar cal2)
cal1 - 日期1cal2 - 日期2public static long spendNt(long preTime)
preTime - 之前记录的时间public static long spendMs(long preTime)
preTime - 之前记录的时间public static int toIntSecond(Date date)
date - 日期public static int weekCount(Date start, Date end)
start - 开始时间end - 结束时间public static TimeInterval timer()
public static int ageOfNow(String birthDay)
birthDay - 生日,标准日期字符串public static int ageOfNow(Date birthDay)
birthDay - 生日public static int age(Date birthDay, Date dateToCompare)
birthDay - 生日dateToCompare - 需要对比的日期public static boolean isLeapYear(int year)
year - 年public static boolean isExpired(Date startDate, DateField dateField, int timeLength, Date checkedDate)
startDate - 开始时间dateField - 时间单位timeLength - 时长checkedDate - 被比较的时间。如果经过时长后的时间晚于被检查的时间,就表示过期public static int timeToSecond(String timeStr)
timeStr - 字符串时分秒(HH:mm:ss)格式public static String secondToTime(int seconds)
seconds - 需要转换的秒数public static DateRange range(Date start, Date end, DateField unit)
start - 起始日期时间end - 结束日期时间unit - 步进单位DateRangeCopyright © 2018. All rights reserved.