import java.time.*;importjava.time.format.DateTimeFormatter;importjava.time.temporal.TemporalUnit;importjava.util.Date;/*** Java8 时间处理
*@author: BuZheng
* @date: 2020-01-16 下午 3:25
**/
public classDateTool {/*** locaDate判断两个时间是否相等
*
*@paramstartTime 时间1
*@paramendTime 时间2
*@return: boolean
*@author: BuZheng
**/
public static booleanlocalDateEquals(LocalDate startTime, LocalDate endTime){if(startTime.equals(endTime)){return true;
}return false;
}/*** locaDateTime判断两个时间是否相等
*
*@paramstartTime 时间1
*@paramendTime 时间2
*@return: boolean
*@author: BuZheng
**/
public static booleanlocaDateTimeEquals(LocalDateTime startTime, LocalDateTime endTime){if(startTime.equals(endTime)){return true;
}return false;
}/*** 判断是否是生日 ( Date )
*
*@parambirthday 生日
*@paramreferDay 参照时间
*@return: boolean
*@author: BuZheng
**/
public static booleanisBirthdayByDate(Date birthday, Date referDay){
LocalDate localDateBirthday=date2LocalDate(birthday);
LocalDate localDateReferDay=date2LocalDate(referDay);//生日时间 x月x日
MonthDay birthdayTime =MonthDay.of(localDateBirthday.getMonth(), localDateBirthday.getDayOfMonth());//参照时间 x月x日
MonthDay referMonthDay =MonthDay.from(localDateReferDay);if(birthdayTime.equals(referMonthDay)){return true;
}return false;
}/*** 判断是否是生日 ( LocalDate )
*
*@parambirthday 生日
*@paramreferDay 参照时间
*@return: boolean
*@author: BuZheng
**/
public static booleanisBirthdayByLocalDate(LocalDate birthday, LocalDate referDay){//生日时间 x月x日
MonthDay birthdayTime =MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());//参照时间 x月x日
MonthDay referMonthDay =MonthDay.from(referDay);if(birthdayTime.equals(referMonthDay)){return true;
}return false;
}/*** 时间转换(LocalDateTime ==》 Date)
*@paramnowDayStart 当前时间
*@return: java.util.Date
*@author: BuZheng
**/
public staticDate localDateTime2Date(LocalDateTime nowDayStart) {
ZoneId zoneId=ZoneId.systemDefault();
ZonedDateTime zdt=nowDayStart.atZone(zoneId);returnDate.from(zdt.toInstant());
}/*** 时间转换(LocalDate ==》 Date)
*
*@paramlocalDate
*@return: java.util.Date
*@author: BuZheng
**/
public staticDate localDate2Date(LocalDate localDate) {if(null ==localDate) {return null;
}
ZonedDateTime zonedDateTime=localDate.atStartOfDay(ZoneId.systemDefault());returnDate.from(zonedDateTime.toInstant());
}/*** 时间转换(Date ==》 LocalDateTime)
*
*@paramdate 时间
*@return: java.time.LocalDateTime
*@author: BuZheng
**/
public staticLocalDateTime date2LocalDateTime(Date date){//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
Instant instant =date.toInstant();//A time-zone ID, such as {@code Europe/Paris}.(时区)
ZoneId zoneId =ZoneId.systemDefault();
LocalDateTime localDateTime=instant.atZone(zoneId).toLocalDateTime();returnlocalDateTime;
}/*** 时间转换(Date ==》 LocalDate)
*
*@paramdate 时间
*@return: java.time.LocalDateTime
*@author: BuZheng
**/
public staticLocalDate date2LocalDate(Date date){//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
Instant instant =date.toInstant();//A time-zone ID, such as {@code Europe/Paris}.(时区)
ZoneId zoneId =ZoneId.systemDefault();
LocalDate localDate=instant.atZone(zoneId).toLocalDate();returnlocalDate;
}/*** 时间增加
*
*@paramdate 开始时间
*@paramnum 数量
*@paramtemporalUnit 以什么形式相加( ChronoUnit.WEEKS/DAYS)
*@return: java.util.Date
*@author: BuZheng
**/
public static Date datePlus(Date date, intnum, TemporalUnit temporalUnit){
LocalDateTime localDateTime=date2LocalDateTime(date);returnlocalDateTime2Date(localDateTime.plus(num, temporalUnit));
}/*** 时间减少
*
*@paramdate 开始时间
*@paramnum 数量
*@paramtemporalUnit 以什么形式减少( ChronoUnit.WEEKS/DAYS)
*@return: java.util.Date
*@author: BuZheng
**/
public static Date dateMinus(Date date, intnum, TemporalUnit temporalUnit){
LocalDateTime localDateTime=date2LocalDateTime(date);returnlocalDateTime2Date(localDateTime.minus(num, temporalUnit));
}/*** 获取当前时间戳
*@author: BuZheng*/
public static longnowMillis(){returnClock.systemUTC().millis();
}/*** 获取昨天日期
*@paramtoDay 日期
*@return: java.util.Date
*@author: BuZheng*/
public staticDate getYesterday(Date toDay){return localDateTime2Date(date2LocalDateTime(toDay).minusDays(1));
}/*** 判断是否是闰年
*@paramtime 日期
*@return: boolean
*@author: BuZheng
**/
public static booleanisLeapYear(Date time){returndate2LocalDate(time).isLeapYear();
}/*** 获取相差的时间(通过Period获取年月日)
*
*@paramstartDate
*@paramendDate
*@return: java.time.Period
*@author: BuZheng
**/
public staticPeriod getDiffer(Date startDate, Date endDate){returnPeriod.between(date2LocalDate(endDate), date2LocalDate(startDate));
}/*** 字符串转成日期(年-月-日)
* 字符串格式必须与需转格式一致!
*@paramtimeStr 日期字符串
*@parampattern 需转换的日期格式
*@return: java.util.Date
*@author: BuZheng
**/
public staticDate string2Date(String timeStr, String pattern){
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern(pattern);
LocalDate parse=LocalDate.parse(timeStr, dateTimeFormatter);returnlocalDate2Date(parse);
}/*** 字符串转成日期(年-月-日 时-分-秒)
* 字符串格式必须与需转格式一致!
*@paramtimeStr 日期字符串
*@parampattern 需转换的日期格式
*@return: java.util.Date
*@author: BuZheng
**/
public staticDate string2DateTime(String timeStr, String pattern){
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern(pattern);
LocalDateTime parse=LocalDateTime.parse(timeStr, dateTimeFormatter);returnlocalDateTime2Date(parse);
}/*** 日期转字符串(年-月-日)
* 字符串格式必须与需转格式一致!
*
*@paramdate 日期
*@parampattern 需转换的日期格式
*@return: java.lang.String
*@author: BuZheng
**/
public staticString date2String(Date date, String pattern){
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern(pattern);
String format=date2LocalDate(date).format(dateTimeFormatter);returnformat;
}/*** 日期转字符串(年-月-日 时-分-秒)
* 字符串格式必须与需转格式一致!
*
*@paramdate 日期
*@parampattern 需转换的日期格式
*@return: java.lang.String
*@author: BuZheng
**/
public staticString dateTime2String(Date date, String pattern){
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern(pattern);
String format=date2LocalDateTime(date).format(dateTimeFormatter);returnformat;
}/*** 获取传入日期最小时间
*
*@paramdate 日期
*@return: java.util.Date
*@author: BuZheng
**/
public staticDate getDateMin(Date date){
LocalDateTime localDateTime=date2LocalDate(date).atTime(LocalTime.MIN);returnlocalDateTime2Date(localDateTime);
}/*** 获取传入日期最大时间
*
*@paramdate 日期
*@return: java.util.Date
*@author: BuZheng
**/
public staticDate getDateMax(Date date){
LocalDateTime localDateTime=date2LocalDate(date).atTime(LocalTime.MAX);returnlocalDateTime2Date(localDateTime);
}/*** 时间戳转时间(Date)
*
*@paramtime 时间
*@return: java.util.Date
*@author: BuZheng
**/
public staticDate timestamp2Date(String time){
Instant timestamp=Instant.ofEpochMilli(Long.parseLong(time));
ZonedDateTime losAngelesTime= timestamp.atZone(ZoneId.of("Asia/Shanghai"));
LocalDateTime localDateTime=losAngelesTime.toLocalDateTime();returnlocalDateTime2Date(localDateTime);
}/*** 两个时间戳相差几天
*
*@paramstartTime 开始时间
*@paramendTime 结束时间
*@return: int
*@author: BuZheng
**/
public static longdifferDays(Long startTime, Long endTime){//获取相差时间戳
long abs = Math.abs(endTime -startTime);long days = abs / (3600 * 24 * 1000);returndays;
}
}