Java时间API整理的工具类-DateUtil.java

系统性整理了下Java8的时间API,编写了个工具类

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
* 日期工具类
*
* @author turbo
* @create 2019-08-02 上午10:45
**/
public class DateUtil {

public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss:SSS";
public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

public static final ZoneOffset ZONE_OFFSET = ZoneOffset.of("+8");
public static final ZoneId ZONE_ID = ZoneId.systemDefault();


/**
* 获取当前时间戳
* @return Long
*/
public static Long getTimeStamp(){

  //Instant.now().toEpochMilli()
  return Clock.systemDefaultZone().millis()
}
/**
* 解析日期 - 支持的格式:YYYY_MM_DD
* @param str
* @return
*/
public static LocalDateTime parseLocalDate(String str){
return LocalDateTime.of(LocalDate.parse(str, DateTimeFormatter.ofPattern(YYYY_MM_DD)), LocalTime.now());
}


/**
* 通过字符串获取 LocalDateTime
* @param str 支持的格式:YYYY_MM_DD_T_HH_MM_SS_SSS 默认支持
* @return
*/
public static LocalDateTime parseTDate(String str){
return LocalDateTime.parse(str);
}

/**
* 通过字符串获取 LocalDateTime
* @param str 支持的格式:YYYY_MM_DD_HH_MM_SS
* @return
*/
public static LocalDateTime parseDate(String str){
return parseDate(str,YYYY_MM_DD_HH_MM_SS);
}

/**
* 通过字符串获取 LocalDateTime
* @param str
* @return
*/
public static LocalDateTime parseDate(String str,String pattern){
return LocalDateTime.parse(str, DateTimeFormatter.ofPattern(pattern));
}

/**
* 通过localDateTime 格式化时间
* @param localDateTime
* @return
*/
public static String formartDate(LocalDateTime localDateTime){
return formartDate(localDateTime,YYYY_MM_DD_HH_MM_SS);
}

/**
* 通过localDateTime 格式化时间
* @param localDateTime
* @return
*/
public static String formartDate(LocalDateTime localDateTime,String parttern){
return localDateTime.format(DateTimeFormatter.ofPattern(parttern));
}

/**
* 获取毫秒
* @param localDateTime
* @return
*/
public static Long getEpochMilli(LocalDateTime localDateTime){
return localDateTime.toInstant(ZONE_OFFSET).toEpochMilli();
}

public static Long getEpochMilli(){
return getEpochMilli(LocalDateTime.now());
}

/**
* 获取秒
* @param localDateTime
* @return
*/
public static Long getEpochSecond(LocalDateTime localDateTime){
return localDateTime.toEpochSecond(ZONE_OFFSET);
}

/**
* 通过毫秒获取 LocalDateTime
* @param epochMilli
* @return
*/
public static LocalDateTime getLocalDateTimeByMilli(Long epochMilli){
return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZONE_ID);
}


/**
* 通过秒获取 LocalDateTime
* @param epochSecond
* @return
*/
public static LocalDateTime getLocalDateTimeBySecond(Long epochSecond){
LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), ZONE_ID);
return LocalDateTime.ofEpochSecond(epochSecond, 0, ZONE_OFFSET);
}

/**
* 获取当前天的最大时间
* @return Long
*/
public static Long getMaxLongTime(){
return getEpochMilli(LocalDateTime.now());
}

/**
* 获取当前天的最小时间
* @return Long
*/
public static Long getMinLongTime(){
return getEpochMilli(LocalDateTime.now());
}

/**
* 获取最大时间
* @param localDateTime
* @return LocalDateTime
*/
public static LocalDateTime getMaxTime(LocalDateTime localDateTime){
return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MAX);
}

/**
* 获取最小时间
* @param localDateTime
* @return LocalDateTime
*/
public static LocalDateTime getMinTime(LocalDateTime localDateTime){
return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
}

//天数加1
public static LocalDateTime plusDays(LocalDateTime localDateTime,long days){
return localDateTime.plusDays(days);
}

public static LocalDateTime plusDays(long days){
return plusDays(LocalDateTime.now(),1);
}

public static LocalDateTime minusDays(LocalDateTime localDateTime,long days){
return localDateTime.minusDays(days);
}

public static LocalDateTime minusDays(long days){
return minusDays(LocalDateTime.now(),1);
}


/**
* date转换为LocalDateTime
* @param date
* @return LocalDateTime
*/
public static LocalDateTime date2LocalDate(Date date){
return LocalDateTime.ofInstant(date.toInstant(),ZONE_ID);
}

/**
* LocalDateTime转换为Date
* @param localDateTime
* @return Date
*/
public static Date LocalDate2date(LocalDateTime localDateTime){
return Date.from(localDateTime.toInstant(ZONE_OFFSET));
}

/**
* 计算两个日期之间的天数差
* @param a
* @param b
* @return
*/
public static int periodLocalDate(LocalDateTime a,LocalDateTime b){
return Period.between(a.toLocalDate(),b.toLocalDate()).getDays();
}

public static long chronoUnitLocalDate(LocalDateTime a,LocalDateTime b){
return ChronoUnit.DAYS.between(a.toLocalDate(),b.toLocalDate());
}

/**
* 计算连个时间的秒差
* @param a
* @param b
* @return
*/
public static long DurationLocalTime(LocalDateTime a,LocalDateTime b){
return Duration.between(a.toLocalTime(),b.toLocalTime()).getSeconds();
}

public static long chronoUnitLocalTime(LocalDateTime a,LocalDateTime b){
return ChronoUnit.SECONDS.between(a.toLocalTime(),b.toLocalTime());
}

}

 

参考地址:

https://blog.csdn.net/h_xiao_x/article/details/79729507

https://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247487938&idx=2&sn=27b13de326980d2cb6e12e30139cf48e&chksm=9bd0bc5aaca7354c2333f5b8217afaa8c54a8d4e1fd17c62a86be8643642dc891b09fa167962&mpshare=1&scene=1&srcid=0802n9albEVq35htEcKS2a7D&sharer_sharetime=1564713535525&sharer_shareid=e37b5d4dcf3cf846267a866ae689756b#rd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值