Java8 时间工具类


import lombok.Getter;

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

/**
 * 时间工具类
 *
 * @Author: taozhengzhi
 * @Date: 2019/4/4 上午10:10
 * @Version 1.0
 */
public class DateUntils {
    /**
     * 获取当前日期 格式为 (xxxx-xx-xx)
     *
     * @return
     */
    public static LocalDate getCurrentDate() {
        return LocalDate.now();
    }
    
    /**
     * 获取当前时间 格式为 (xxxx-xx-xx xx:xx:xx.xxx)
     *
     * @return
     */
    public static LocalDateTime getLocalDateTime() {
        return LocalDateTime.now();
    }
    
    /**
     * Date 转化为 格式化时间
     *
     * @param date    待转化的date
     * @param pattern 时间格式
     * @return
     */
    public static String format(Date date, DateTimeFormatterEnum pattern) {
        return format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()), pattern);
    }
    
    /**
     * LocalDateTime 转换为格式化时间
     *
     * @param localDateTime 待转化的localDateTime
     * @param pattern       时间格式
     * @return
     */
    public static String format(LocalDateTime localDateTime, DateTimeFormatterEnum pattern) {
        return localDateTime.format(pattern.formatter);
    }
    
    /**
     * 字符串转为格式化Date
     *
     * @param date    时间字符串
     * @param pattern 时间格式
     * @return
     */
    public static Date parseDate(String date, DateTimeFormatterEnum pattern) {
        return Date.from(parseLocalDateTime(date, pattern).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 字符串转格式化LocalDateTime
     *
     * @param date    时间字符串
     * @param pattern 时间格式
     * @return
     */
    public static LocalDateTime parseLocalDateTime(String date, DateTimeFormatterEnum pattern) {
        return LocalDateTime.parse(date, pattern.formatter);
    }
    
    /**
     * 今天的开始
     *
     * @return
     */
    public static Date todayBegin() {
        return Date.from(LocalDateTime.of(getCurrentDate(), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 今天的结束
     *
     * @return
     */
    public static Date todayEnd() {
        return Date.from(LocalDateTime.of(getCurrentDate(), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 昨天的开始
     *
     * @return
     */
    public static Date yesterdayBegin() {
        return Date.from(LocalDateTime.of(getCurrentDate().plusDays(-1L), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 昨天的结束
     *
     * @return
     */
    public static Date yesterdayEnd() {
        return Date.from(LocalDateTime.of(getCurrentDate().plusDays(-1L), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 明天的开始
     *
     * @return
     */
    public static Date tomorrowBegin() {
        return Date.from(LocalDateTime.of(getCurrentDate().plusDays(1L), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 明天的结束
     *
     * @return
     */
    public static Date tomorrowEnd() {
        return Date.from(LocalDateTime.of(getCurrentDate().plusDays(1L), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 指定天的开始
     *
     * @param date 指定日期
     * @return
     */
    public static Date startTime(Date date) {
        return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate(), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 指定天的结束
     *
     * @param date
     * @return
     */
    public static Date endTime(Date date) {
        return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate(), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 当月的第一天
     *
     * @return
     */
    public static Date monthBegin() {
        return Date.from(LocalDateTime.of(getCurrentDate().with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 当月的最后一天
     *
     * @return
     */
    public static Date monthEnd() {
        return Date.from(LocalDateTime.of(getCurrentDate().with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 指定月的第一天
     *
     * @param date
     * @return
     */
    public static Date theMonthBegin(Date date) {
        return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.firstDayOfMonth()).toLocalDate(), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 指定月的最后一天
     *
     * @param date
     * @return
     */
    public static Date theMonthEnd(Date date) {
        return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.lastDayOfMonth()).toLocalDate(), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 指定年的第一天
     *
     * @param date
     * @return
     */
    public static Date theYearBegin(Date date) {
        return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.firstDayOfYear()).toLocalDate(), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 指定年的第一天
     *
     * @param date
     * @return
     */
    public static Date theYearEnd(Date date) {
        return Date.from(LocalDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(TemporalAdjusters.lastDayOfYear()).toLocalDate(), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 判断传入时间是否是今天
     *
     * @param date
     * @return
     */
    public static Boolean checkIsToday(Date date) {
        return getCurrentDate().equals(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate());
    }
    
    /**
     * 计算一段时间的间隔
     * duration.toDays() 计算这段时间的总天数(满24小时为1天)
     * duration.toHours() 计算这段时间的总小时数(满60分钟为1小时)
     * duration.toMinutes() 计算这段时间的总分钟数(满60秒为1分钟)
     * duration.getSeconds() 计算这段时间的总秒数(满1000毫秒为1秒)
     * duration.toMillis() 计算这段时间的总毫秒数
     * duration.toNanos() 计算这段时间的总纳秒数
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static Duration between(Date beginTime, Date endTime) {
        LocalDateTime beginLocalDateTime = LocalDateTime.ofInstant(beginTime.toInstant(), ZoneId.systemDefault());
        LocalDateTime endLocalDateTime = LocalDateTime.ofInstant(endTime.toInstant(), ZoneId.systemDefault());
        return Duration.between(beginLocalDateTime, endLocalDateTime);
    }
    
    /**
     * 指定日期的几天前
     * @param date 指定日期
     * @param num 向前的天数
     * @return
     */
    public static Date theDayBefor(Date date, long num) {
        return Date.from(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).minusDays(num).atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 指定日期的几天后
     * @param date 指定日期
     * @param num 向后的天数
     * @return
     */
    public static Date theDayLater(Date date, long num){
        return Date.from(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).plusDays(num).atZone(ZoneId.systemDefault()).toInstant());
    }

    
    
    /**
     * 日期格式枚举(如果有需要自己加)
     */
    public enum DateTimeFormatterEnum {
       
        /**
         * yyyy-MM-dd HH:mm:ss
         */
        FORMAT_DATE_TIME_STYLE_1("yyyy-MM-dd HH:mm:ss", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        
        @Getter
        private String value;
        @Getter
        private DateTimeFormatter formatter;
        
        DateTimeFormatterEnum(String value, DateTimeFormatter formatter) {
            this.value = value;
            this.formatter = formatter;
        }
    }
}

 

 

 

推荐:
服务器最低86元/年

 拼团链接:  [阿里云点击进入](https://www.aliyun.com/1111/2019/group-buying-share?ptCode=7615808881909C8309826E477DF711E6647C88CF896EF535&userCode=h22tgxqt&share_source=copy_link)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值