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 |
DATE_PATTERN
Date pattern
|
static String |
DATE_TO_STRING_PATTERN
Date format of
Date.toString() |
static String |
DATEFULL_PATTERN
Full datetime pattern
|
static org.apache.commons.lang3.time.FastDateFormat |
DATETIME_FORMAT
Fast date format for datetime pattern
|
static String |
DATETIME_PATTERN
Datetime pattern
|
| 构造器和说明 |
|---|
Dates() |
| 限定符和类型 | 方法和说明 |
|---|---|
static long |
clockDiff(Date start,
Date end)
计算两个日期的时间差(单位:秒)
|
static long |
currentUnixTimestamp() |
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 LocalDateTime |
endOfDay(LocalDateTime dateTime) |
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 pattern)
格式化日期对象
|
static String |
format(long timeMillis,
String pattern)
格式化日期对象
|
static int |
hourOfDay(Date date) |
static boolean |
isValidDate(String dateStr,
String pattern)
简单的日期格式校验
|
static Date |
max(Date a,
Date b)
Returns the greater of two
Date values. |
static Date |
max(Date a,
Date b,
Date c) |
static Date |
min(Date a,
Date b)
Returns the smaller of two
Date values. |
static Date |
minusDays(Date date,
long days)
减少天数
|
static Date |
minusHours(Date date,
long hours)
减少小时
|
static Date |
minusMillis(Date date,
long millis)
减少毫秒数
|
static Date |
minusMinutes(Date date,
long minutes)
减少分钟
|
static Date |
minusMonths(Date date,
long months)
减少月份
|
static Date |
minusSeconds(Date date,
long seconds)
减少秒数
|
static Date |
minusWeeks(Date date,
long weeks)
减少周
|
static Date |
minusYears(Date date,
long years)
减少年
|
static Date |
now()
获取当前日期对象
|
static String |
now(String pattern)
获取当前日期字符串
|
static Date |
ofTimeMillis(long timeMillis)
java(毫秒)时间戳
|
static Date |
ofTimeMillis(Long timeMillis) |
static Date |
ofUnixTimestamp(long unixTimestamp)
unix时间戳
|
static Date |
ofUnixTimestamp(Long unixTimestamp) |
static Date |
plusDays(Date date,
long days)
增加天数
|
static Date |
plusHours(Date date,
long hours)
增加小时
|
static Date |
plusMillis(Date date,
long millis)
增加毫秒数
|
static Date |
plusMinutes(Date date,
long minutes)
增加分钟
|
static Date |
plusMonths(Date date,
long months)
增加月份
|
static Date |
plusSeconds(Date date,
long seconds)
增加秒数
|
static Date |
plusWeeks(Date date,
long weeks)
增加周
|
static Date |
plusYears(Date date,
long years)
增加年
|
static Date |
random(Date begin,
Date end)
日期随机
|
static Date |
random(long beginTimeMills,
long endTimeMills) |
static Date |
startOfDay(Date date)
获取指定日期所在天的开始时间:yyyy-MM-dd 00:00:00
|
static LocalDateTime |
startOfDay(LocalDateTime dateTime) |
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(LocalDate localDate) |
static Date |
toDate(LocalDateTime localDateTime) |
static Date |
toDate(String dateStr,
String pattern)
转换日期即字符串为Date对象
|
static LocalDate |
toLocalDate(Date date) |
static LocalDateTime |
toLocalDateTime(Date date) |
static Date |
withDayOfMonth(Date date,
int dayOfMonth)
获取指定时间所在月的n号,1<=day<=31
|
static Date |
withDayOfWeek(Date date,
int dayOfWeek)
获取指定时间所在周的周n,1<=day<=7
|
static Date |
withDayOfYear(Date date,
int dayOfYear)
获取指定时间所在年的n天,1<=day<=366
|
static Date |
zoneConvert(Date date,
ZoneId sourceZone,
ZoneId targetZone)
时区转换
|
static LocalDateTime |
zoneConvert(LocalDateTime localDateTime,
ZoneId sourceZone,
ZoneId targetZone)
时区转换
|
public static final String DATE_TO_STRING_PATTERN
Date.toString()public static final org.apache.commons.lang3.time.FastDateFormat DATETIME_FORMAT
public static boolean isValidDate(String dateStr, String pattern)
dateStr - 输入日期,如(yyyy-MM-dd)pattern - 日期格式public static Date now()
public static Date toDate(String dateStr, String pattern)
dateStr - 日期字符串pattern - 日期格式public static Date ofTimeMillis(long timeMillis)
timeMillis - 毫秒时间戳public static long currentUnixTimestamp()
public static Date ofUnixTimestamp(long unixTimestamp)
unixTimestamp - unix时间戳public static String format(Date date, String pattern)
date - 日期对象pattern - 日期格式public static String format(Date date)
date - 日期对象public static String format(long timeMillis, String pattern)
timeMillis - 毫秒pattern - 格式public static Date plusMillis(Date date, long millis)
date - 时间millis - 毫秒数public static Date plusSeconds(Date date, long seconds)
date - 时间seconds - 秒数public static Date plusMinutes(Date date, long minutes)
date - 时间minutes - 分钟数public static Date plusMonths(Date date, long months)
date - 时间months - 月数public static Date minusMillis(Date date, long millis)
date - 时间millis - 毫秒数public static Date minusSeconds(Date date, long seconds)
date - 时间seconds - 秒数public static Date minusMinutes(Date date, long minutes)
date - 时间minutes - 分钟数public static Date minusHours(Date date, long hours)
date - 时间hours - 小时数public static Date minusMonths(Date date, long months)
date - 时间months - 月数public static Date startOfDay(Date date)
date - 时间public static Date endOfDay(Date date)
date - 时间public static Date startOfWeek(Date date)
date - 日期public static Date endOfWeek(Date date)
date - 日期public static Date startOfMonth(Date date)
date - 日期public static Date endOfMonth(Date date)
date - 日期public static Date startOfYear(Date date)
date - 日期public static Date endOfYear(Date date)
date - 日期public static Date withDayOfWeek(Date date, int dayOfWeek)
date - 相对日期dayOfWeek - 1-星期一;2-星期二;...public static Date withDayOfMonth(Date date, int dayOfMonth)
date - the datedayOfMonth - the day of monthpublic static Date withDayOfYear(Date date, int dayOfYear)
date - the datedayOfYear - the day of yearpublic static int dayOfYear(Date date)
public static int dayOfMonth(Date date)
public static int dayOfWeek(Date date)
public static int hourOfDay(Date date)
public static long clockDiff(Date start, Date end)
start - 开始时间end - 结束时间public static int daysBetween(Date start, Date end)
start - the start dateend - the end dateChronoUnit.between(Temporal, Temporal)public static Date random(long beginTimeMills, long endTimeMills)
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 LocalDateTime startOfDay(LocalDateTime dateTime)
public static LocalDateTime endOfDay(LocalDateTime dateTime)
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 toCronExpression(LocalDateTime dateTime)
dateTime - the local date timeCopyright © 2024. All rights reserved.