public class Dates extends Object
时区:
LocalDateTime:无时区
Date(UTC0):表示自格林威治时间(GMT)1970年1月1日0点经过指定的毫秒数后的时间点
Instant(UTC0):同Date
ZonedDateTime:自带时区
ZoneId子类:ZoneRegion、ZoneOffset
ZoneId.of("Etc/GMT-8") --> Etc/GMT-8
ZoneId.of("GMT+8") --> GMT+08:00
ZoneId.of("UTC+8") --> UTC+08:00
ZoneId.of("Asia/Shanghai") --> Asia/Shanghai
ZoneId.systemDefault() --> Asia/Shanghai
TimeZone子类(不支持UTC):ZoneInfo
TimeZone.getTimeZone("Etc/GMT-8") --> Etc/GMT-8
TimeZone.getTimeZone("GMT+8") --> GMT+08:00
TimeZone.getTimeZone("Asia/Shanghai") --> Asia/Shanghai
TimeZone.getTimeZone(ZoneId.of("GMT+8")) --> GMT+08:00
TimeZone.getDefault() --> Asia/Shanghai
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
DEFAULT_DATE_FORMAT
Default date time format
|
static String |
ZERO_DATE_TIME
Zero time millis: -62170185600000L
|
| 构造器和说明 |
|---|
Dates() |
| 限定符和类型 | 方法和说明 |
|---|---|
static long |
clockDiff(Date start,
Date end)
计算两个日期的时间差(单位:秒)
|
static int |
dayOfMonth(Date date) |
static int |
dayOfWeek(Date date) |
static int |
dayOfYear(Date date) |
static int |
daysBetween(Date start,
Date end)
Returns a days between the two date(end-start)
|
static Date |
endOfDay(Date date)
获取指定日期所在天的结束时间:yyyy-MM-dd 23:59:59
|
static Date |
endOfMonth(Date date)
获取指定日期所在月的结束时间:yyyy-MM-月未 23:59:59
|
static Date |
endOfWeek(Date date)
获取指定日期所在周的结束时间:yyyy-MM-周日 23:59:59
|
static Date |
endOfYear(Date date)
获取指定日期所在月的结束时间:yyyy-12-31 23:59:59
|
static String |
format(Date date)
格式化日期对象,格式为yyyy-MM-dd HH:mm:ss
|
static String |
format(Date date,
String format)
格式化日期对象
|
static String |
format(long mills,
String pattern)
格式化日期对象
|
static int |
hourOfDay(Date date) |
static boolean |
isValidDate(String date)
简单的日期格式校验(yyyy-MM-dd HH:mm:ss)
|
static boolean |
isValidDate(String date,
String pattern)
简单的日期格式校验
|
static boolean |
isZeroDate(Date date)
Check the date is whether zero date
|
static Date |
max(Date a,
Date b)
Returns the greater of two
Date values. |
static Date |
min(Date a,
Date b)
Returns the smaller of two
Date values. |
static Date |
minusDays(Date date,
int numdays)
减少天数
|
static Date |
minusHours(Date date,
int numOfHours)
减少小时
|
static Date |
minusMillis(Date date,
int numOfMillis)
减少毫秒数
|
static Date |
minusMinutes(Date date,
int numOfMinutes)
减少分钟
|
static Date |
minusMonths(Date date,
int numMonths)
减少月份
|
static Date |
minusSeconds(Date date,
int numOfSeconds)
减少秒数
|
static Date |
minusWeeks(Date date,
int numWeeks)
减少周
|
static Date |
minusYears(Date date,
int numYears)
减少年
|
static Date |
now()
获取当前日期对象
|
static String |
now(String format)
获取当前日期字符串
|
static Date |
ofMillis(long timeMillis)
java(毫秒)时间戳
|
static Date |
ofMillis(Long timeMillis) |
static Date |
ofSeconds(long unixTimeSeconds)
unix时间戳
|
static Date |
ofSeconds(Long unixTimeSeconds) |
static Date |
plusDays(Date date,
int numdays)
增加天数
|
static Date |
plusHours(Date date,
int numOfHours)
增加小时
|
static Date |
plusMillis(Date date,
int numOfMillis)
增加毫秒数
|
static Date |
plusMinutes(Date date,
int numOfMinutes)
增加分钟
|
static Date |
plusMonths(Date date,
int numMonths)
增加月份
|
static Date |
plusSeconds(Date date,
int numOfSeconds)
增加秒数
|
static Date |
plusWeeks(Date date,
int numWeeks)
增加周
|
static Date |
plusYears(Date date,
int numYears)
增加年
|
static Date |
random(Date begin,
Date end)
日期随机
|
static Date |
random(long beginMills,
long endMills) |
static Date |
startOfDay(Date date)
获取指定日期所在天的开始时间:yyyy-MM-dd 00:00:00
|
static Date |
startOfMonth(Date date)
获取指定日期所在月的开始时间:yyyy-MM-01 00:00:00
|
static Date |
startOfWeek(Date date)
获取指定日期所在周的开始时间:yyyy-MM-周一 00:00:00
|
static Date |
startOfYear(Date date)
获取指定日期所在月的开始时间:yyyy-01-01 00:00:00
|
static String |
toCronExpression(Date date) |
static String |
toCronExpression(LocalDateTime dateTime)
Converts date time to cron expression
|
static Date |
toDate(LocalDateTime localDateTime) |
static Date |
toDate(String dateStr)
转换日期字符串为日期对象(默认格式: yyyy-MM-dd HH:mm:ss)
|
static Date |
toDate(String dateStr,
String pattern)
转换日期即字符串为Date对象
|
static LocalDateTime |
toLocalDateTime(Date date) |
static Date |
withDayOfMonth(Date date,
int day)
获取指定时间所在月的n号,1<=day<=31
|
static Date |
withDayOfWeek(Date date,
int day)
获取指定时间所在周的周n,1<=day<=7
|
static Date |
withDayOfYear(Date date,
int day)
获取指定时间所在年的n天,1<=day<=366
|
static Date |
zoneConvert(Date date,
ZoneId sourceZone,
ZoneId targetZone)
时区转换
|
static LocalDateTime |
zoneConvert(LocalDateTime localDateTime,
ZoneId sourceZone,
ZoneId targetZone)
时区转换
|
static String |
zoneConvert(String date,
String pattern,
ZoneId sourceZone,
ZoneId targetZone)
时区转换
|
static String |
zoneConvert(String date,
ZoneId sourceZone,
ZoneId targetZone) |
public static final String DEFAULT_DATE_FORMAT
public static boolean isValidDate(String date)
date - 输入日期public static boolean isValidDate(String date, String pattern)
date - 输入日期,如(yyyy-MM-dd)pattern - 日期格式public static boolean isZeroDate(Date date)
date - the datetruepublic static Date now()
public static Date toDate(String dateStr)
dateStr - 日期字符串public static Date toDate(String dateStr, String pattern)
dateStr - 日期字符串pattern - 日期格式public static Date ofMillis(long timeMillis)
timeMillis - 毫秒public static Date ofSeconds(long unixTimeSeconds)
unixTimeSeconds - 秒public static String format(Date date, String format)
date - 日期对象format - 日期格式public static String format(Date date)
date - 日期对象public static String format(long mills, String pattern)
mills - 毫秒pattern - 格式public static Date plusMillis(@Nonnull Date date, int numOfMillis)
date - 时间numOfMillis - 毫秒数public static Date plusSeconds(@Nonnull Date date, int numOfSeconds)
date - 时间numOfSeconds - 秒数public static Date plusMinutes(@Nonnull Date date, int numOfMinutes)
date - 时间numOfMinutes - 分钟数public static Date plusHours(@Nonnull Date date, int numOfHours)
date - 时间numOfHours - 小时数public static Date plusDays(@Nonnull Date date, int numdays)
date - 时间numdays - 天数public static Date plusWeeks(@Nonnull Date date, int numWeeks)
date - 时间numWeeks - 周数public static Date plusMonths(@Nonnull Date date, int numMonths)
date - 时间numMonths - 月数public static Date plusYears(@Nonnull Date date, int numYears)
date - 时间numYears - 年数public static Date minusMillis(@Nonnull Date date, int numOfMillis)
date - 时间numOfMillis - 毫秒数public static Date minusSeconds(@Nonnull Date date, int numOfSeconds)
date - 时间numOfSeconds - 秒数public static Date minusMinutes(@Nonnull Date date, int numOfMinutes)
date - 时间numOfMinutes - 分钟数public static Date minusHours(@Nonnull Date date, int numOfHours)
date - 时间numOfHours - 小时数public static Date minusDays(@Nonnull Date date, int numdays)
date - 时间numdays - 天数public static Date minusWeeks(@Nonnull Date date, int numWeeks)
date - 时间numWeeks - 周数public static Date minusMonths(@Nonnull Date date, int numMonths)
date - 时间numMonths - 月数public static Date minusYears(@Nonnull Date date, int numYears)
date - 时间numYears - 年数public static Date startOfDay(@Nonnull Date date)
date - 时间public static Date endOfDay(@Nonnull Date date)
date - 时间public static Date startOfWeek(@Nonnull Date date)
date - 日期public static Date endOfWeek(@Nonnull Date date)
date - 日期public static Date startOfMonth(@Nonnull Date date)
date - 日期public static Date endOfMonth(@Nonnull Date date)
date - 日期public static Date startOfYear(@Nonnull Date date)
date - 日期public static Date endOfYear(@Nonnull Date date)
date - 日期public static Date withDayOfWeek(@Nonnull Date date, int day)
date - 相对日期day - 1:星期一,2:星期二,...public static Date withDayOfMonth(@Nonnull Date date, int day)
date - day - public static Date withDayOfYear(@Nonnull Date date, int day)
date - day - public static long clockDiff(@Nonnull Date start, @Nonnull Date end)
start - 开始时间end - 结束时间public static int daysBetween(Date start, Date end)
start - the start dateend - the end datepublic static Date random(long beginMills, long endMills)
public static Date min(Date a, Date b)
Date values.a - the first Dateb - the second Datea and bpublic static Date max(Date a, Date b)
Date values.a - the first Dateb - the second Datea and bpublic static LocalDateTime toLocalDateTime(Date date)
public static Date toDate(LocalDateTime localDateTime)
public static Date zoneConvert(Date date, ZoneId sourceZone, ZoneId targetZone)
date - the datesourceZone - the source zone idtargetZone - the target zone idpublic static LocalDateTime zoneConvert(LocalDateTime localDateTime, ZoneId sourceZone, ZoneId targetZone)
localDateTime - the localDateTimesourceZone - the source zone idtargetZone - the target zone idpublic static String zoneConvert(String date, String pattern, ZoneId sourceZone, ZoneId targetZone)
date - the source date stringpattern - the source date formatsourceZone - the source zone idtargetZone - the target zone idpublic static String toCronExpression(LocalDateTime dateTime)
dateTime - the local date timeCopyright © 2023. All rights reserved.