java8时间工具类

298 阅读6分钟
package com.avanline.simple;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class DateUtil {
    /**
     * 时间节点枚举类
     */
    public enum NodeEnum {
        YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND
    }

    /**
     * yyyy-MM-dd
     */
    public static final DateTimeFormatter DF_YMD_SEPARATE =
            DateTimeFormatter.ofPattern("yyyy-MM-dd");
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final DateTimeFormatter DF_SEPARATE =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    /**
     * HH:mm:ss
     */
    public static final DateTimeFormatter DF_HMS =
            DateTimeFormatter.ofPattern("HH:mm:ss");

    /**
     * yyyy年MM月dd日
     */
    public static final DateTimeFormatter DF_YMD_CN =
            DateTimeFormatter.ofPattern("yyyy年MM月dd日");
    /**
     * yyyy年MM月dd日HH时mm分ss秒
     */
    public static final DateTimeFormatter DF_SEPARATE_CN =
            DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒");

    /**
     * HH时mm分ss秒
     */
    public static final DateTimeFormatter DF_HM_CN =
            DateTimeFormatter.ofPattern("HH时mm分ss秒");

    /**
     * 获取当前时间戳 带毫秒
     *
     * @return
     */
    public static long getMilli() {
        return Instant.now().toEpochMilli();

    }

    /**
     * 获取当前时间戳 不带毫秒
     *
     * @return
     */
    public static long getSecond() {
        return Instant.now().getEpochSecond();
    }

    /**
     * LocalDate类型转换为String类型
     *
     * @param pDate      LocalDate类型时间
     * @param pFormatter 格式
     * @return String类型时间
     */
    public static String dateToString(LocalDate pDate
            , DateTimeFormatter pFormatter) {
        return dateTimeToString(pDate.atStartOfDay(), pFormatter);
    }

    /**
     * LocalDateTime类型转换为String类型
     *
     * @param pDateTime  LocalDateTime类型时间
     * @param pFormatter 格式
     * @return String类型时间
     */
    public static String dateTimeToString(LocalDateTime pDateTime
            , DateTimeFormatter pFormatter) {
        return pDateTime.format(pFormatter);
    }

    /**
     * 毫秒时间戳类型转换String类型时间
     *
     * @param currentTime 要转换的long类型的时间
     * @param pFormatter  格式
     * @return String类型时间
     */
    public static String milliToString(long currentTime
            , DateTimeFormatter pFormatter) {
        return pFormatter.format(
                milliToDateTime(currentTime)
        );
    }

    /**
     * 秒时间戳类型转换String类型时间
     *
     * @param currentTime 要转换的long类型的时间
     * @param pFormatter  格式
     * @return String类型时间
     */
    public static String secondToString(long currentTime
            , DateTimeFormatter pFormatter) {
        return pFormatter.format(
                secondToDateTime(currentTime)
        );
    }

    /**
     * 不带毫秒时间戳类型转换LocalDateTime类型时间
     *
     * @param currentTime 要转换的long类型的时间
     * @return String类型时间
     */
    public static LocalDateTime secondToDateTime(long currentTime) {
        return LocalDateTime.ofInstant(
                Instant.ofEpochSecond(currentTime), ZoneId.systemDefault()
        );
    }

    /**
     * 带毫秒时间戳类型转换LocalDateTime类型时间
     *
     * @param currentTime 要转换的long类型的时间
     * @return String类型时间
     */
    public static LocalDateTime milliToDateTime(long currentTime) {
        return LocalDateTime.ofInstant(
                Instant.ofEpochMilli(currentTime), ZoneId.systemDefault()
        );
    }

    /**
     * string类型转换为date类型 strTime的时间格式必须要与formatType的时间格式相同
     *
     * @param strTime    要转换的string类型的时间
     * @param pFormatter 要转换的格式
     * @return LocalDateTime 类型时间
     */
    public static LocalDateTime stringToDateTime(String strTime
            , DateTimeFormatter pFormatter) {
        return LocalDateTime.parse(strTime, pFormatter);
    }


    /**
     * string类型转换为毫秒时间戳
     * strTime的时间格式和formatType的时间格式必须相同
     *
     * @param strTime    要转换的String类型的时间
     * @param pFormatter 时间格式
     * @return 带毫秒值的时间戳
     */
    public static long stringToMilli(String strTime
            , DateTimeFormatter pFormatter) {
        return stringToDateTime(strTime, pFormatter)
                .atZone(
                        ZoneId.systemDefault()
                ).toInstant().toEpochMilli();
    }

    /**
     * string类型转换为毫秒时间戳
     * strTime的时间格式和formatType的时间格式必须相同
     *
     * @param strTime    要转换的String类型的时间
     * @param pFormatter 时间格式
     * @return 带毫秒值的时间戳
     */
    public static long stringToSecond(String strTime
            , DateTimeFormatter pFormatter) {
        return stringToDateTime(strTime, pFormatter)
                .atZone(
                        ZoneId.systemDefault()
                ).toInstant().getEpochSecond();
    }

    /**
     * date类型转换为毫秒时间戳
     *
     * @param pDateTime 要转换的LocalDateTime类型的时间
     * @return 毫秒时间戳
     */
    public static long dateTimeToMilli(LocalDateTime pDateTime) {
        return pDateTime.atZone(
                ZoneId.systemDefault()
        ).toInstant().toEpochMilli();
    }

    /**
     * date类型转换为毫秒时间戳
     *
     * @param pDateTime 要转换的LocalDateTime类型的时间
     * @return 毫秒时间戳
     */
    public static long dateTimeToSecond(LocalDateTime pDateTime) {
        return pDateTime.atZone(
                ZoneId.systemDefault()
        ).toInstant().getEpochSecond();
    }

    /**
     * 当前时间的 {@link NodeEnum}之后(之前)的时刻
     *
     * @param num       之前(之后)多久时,分,秒(+:之后,-:之前)
     * @param pNodeEnum {@link NodeEnum}
     * @return 更新后的LocalDateTime
     */
    public static LocalDateTime getAfterOrPreNowTime(int num, NodeEnum pNodeEnum) {
        return getAfterOrPreNowTime(num, pNodeEnum, LocalDateTime.now());
    }

    /**
     * 指定时间的 {@link NodeEnum}之后(之前)的时刻
     *
     * @param num       之前(之后)多久时,分,秒(+:之后,-:之前)
     * @param pNodeEnum {@link NodeEnum}.day 天前后 .year 年前后 week 周前后
     * @param pDateTime 指定时间
     * @return 更新后的LocalDateTime
     */
    public static LocalDateTime getAfterOrPreNowTime(int num, NodeEnum pNodeEnum,
                                                     LocalDateTime pDateTime) {
        LocalDateTime mLocalDateTime = null;
        switch (pNodeEnum) {
            case YEAR:
                mLocalDateTime = pDateTime.plusYears(num);
                break;
            case MONTH:
                mLocalDateTime = pDateTime.plusMonths(num);
                break;
            case DAY:
                mLocalDateTime = pDateTime.plusDays(num);
                break;
            case HOUR:
                mLocalDateTime = pDateTime.plusHours(num);
                break;
            case MINUTE:
                mLocalDateTime = pDateTime.plusMinutes(num);
                break;
            case SECOND:
                mLocalDateTime = pDateTime.plusSeconds(num);
                break;
            case WEEK:
                mLocalDateTime = pDateTime.plusWeeks(num);
                break;
        }
        return mLocalDateTime;
    }

    /**
     * 获取一天中的最大值
     *
     * @return
     */
    public static LocalDateTime getMaxNowDate() {
        return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
    }

    /**
     * 获取一天中的最小值
     *
     * @return
     */
    public static LocalDateTime getMinNowDate() {
        return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
    }

    /**
     * 获取一天的起始时间 LocalDateTime类型
     *
     * @param pDateTime LocalDateTime类型日期
     * @return
     */
    public static LocalDateTime getMinTime(LocalDateTime pDateTime) {
        return pDateTime.with(LocalTime.MIN);
    }

    /**
     * 获取一天的结束时间 LocalDateTime类型
     *
     * @param pDateTime LocalDateTime类型日期
     * @return
     */
    public static LocalDateTime getMaxTime(LocalDateTime pDateTime) {
        return pDateTime.with(LocalTime.MAX);
    }

    /**
     * 格式化时间
     *
     * @param format
     * @return
     */
    public static DateTimeFormatter getDateTimeFormatter(String format) {
        return DateTimeFormatter.ofPattern(format);
    }

    /**  
    * 获取指定日期是周几  
    *  
    * @param date 时间  
    * @return String  
    */  
    public static String getWeekOfDate(LocalDateTime date) {  
        return getWeekOfDate(date, TextStyle.SHORT);  
    }  

    /**  
    * 获取指定日期是星期几  
    *  
    * @param date 时间  
    * @param textStyle 返回格式  
    * @return String  
    */  
    public static String getWeekOfDate(LocalDateTime date, TextStyle textStyle) {  
        return getWeekOfDate(date, textStyle, Locale.CHINA);  
    }  

    /**  
    * 获取指定日期是星期几  
    *  
    * @param date 时间  
    * @param textStyle 返回格式  
    * @param locale 语言  
    * @return String  
    */  
    public static String getWeekOfDate(LocalDateTime date, TextStyle textStyle, Locale locale) {  
        DayOfWeek dayOfWeek = date.getDayOfWeek();  
        return dayOfWeek.getDisplayName(textStyle, locale);  
    }

    /**
     * 将日期信息转换成今天 几分钟 几小时以前、昨天、日期
     *
     * @param pDateTime
     * @return
     */
    public static String getDateDetail(LocalDateTime pDateTime) {
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(pDateTime, now);
        //非2天以内
        if (duration.toDays() > 2) {
            return dateTimeToString(pDateTime, DF_YMD_SEPARATE);
        }
        //大于今天
        if (pDateTime.isAfter(now)) {
            return dateTimeToString(pDateTime, DF_YMD_SEPARATE);
        }
        if (pDateTime.getDayOfMonth() - now.getDayOfMonth() == -1) {
            return "昨天";
        }
        if (pDateTime.getDayOfMonth() - now.getDayOfMonth() == -2) {
            return "前天";
        }

        if (duration.toHours() > 0) {
            return duration.toHours() + "小时以前";
        }
        if (duration.toMinutes() == 0) {
            return "刚刚";
        }
        return duration.toMinutes() + "分钟以前";
    }

    public static void main(String[] args) {
        //LocalDate转string
        System.out.println("LocalDate转string:"
                + dateToString(LocalDate.now(), DF_SEPARATE));

        //LocalDateTime转string
        System.out.println("LocalDateTime转string:"
                + dateTimeToString(LocalDateTime.now(), DF_SEPARATE));

        //string转LocalDateTime
        System.out.println("string转LocalDateTime:"
                + stringToDateTime("2020-10-29 17:00:59", DF_SEPARATE));

        //带毫秒的时间戳转string
        System.out.println("带毫秒的时间戳转string:"
                + milliToString(getMilli(), DF_SEPARATE));

        //不带毫秒的时间戳转string
        System.out.println("不带毫秒的时间戳转string:"
                + secondToString(getSecond(), DF_SEPARATE));

        //string转毫秒时间戳
        System.out.println("string转毫秒时间戳:" +
                stringToMilli("2020-10-29 12:20:40", DF_SEPARATE));

        //string转不带毫秒时间戳
        System.out.println("string转不带毫秒时间戳:" +
                stringToSecond("2020-10-29 12:20:40", DF_SEPARATE));

        //LocalDateTime转毫秒时间戳
        System.out.println("LocalDateTime转毫秒时间戳:"
                + dateTimeToMilli(LocalDateTime.now()));

        //LocalDateTime转秒级时间戳
        System.out.println("LocalDateTime转秒级时间戳:"
                + dateTimeToSecond(LocalDateTime.now()));

        //获取当前时间 一天前的LocalDateTime
        System.out.println("获取当前时间 一天前的LocalDateTime:"
                + getAfterOrPreNowTime(-1, NodeEnum.DAY));

        System.out.println("获取指定时间周几:"
                + getWeekOfDate(stringToDateTime(
                "2020-10-25 12:20:40", DF_SEPARATE)));
        //获取一天前的时间
        LocalDateTime mLocalDateTime = getAfterOrPreNowTime(-1, NodeEnum.DAY);
        System.out.println("时间转化:" + getDateDetail(mLocalDateTime));
        //获取2天前的时间
        mLocalDateTime = getAfterOrPreNowTime(-2, NodeEnum.DAY);
        System.out.println("时间转化:" + mLocalDateTime + "--------"
                + getDateDetail(mLocalDateTime));
        //获取1月前的时间
        mLocalDateTime = getAfterOrPreNowTime(-1, NodeEnum.MONTH);
        System.out.println("时间转化:" + mLocalDateTime + "--------"
                + getDateDetail(mLocalDateTime));
        //获取一分钟以前的时间
        mLocalDateTime = getAfterOrPreNowTime(-1, NodeEnum.MINUTE);
        System.out.println("时间转化:" + mLocalDateTime + "--------"
                + getDateDetail(mLocalDateTime));
        //获取一小时前的时间
        mLocalDateTime = getAfterOrPreNowTime(-1, NodeEnum.HOUR);
        System.out.println("时间转化:" + mLocalDateTime + "--------"
                + getDateDetail(mLocalDateTime));
        //获取23小时前的时间
        mLocalDateTime = getAfterOrPreNowTime(-23, NodeEnum.HOUR);
        System.out.println("时间转化:" + mLocalDateTime + "--------"
                + getDateDetail(mLocalDateTime));

        System.out.println("获取今日最小时间:" + getMinNowDate());
        System.out.println("获取今日最大时间:" + getMaxNowDate());
        System.out.println("获取指定日期的最小时间:"
                + mLocalDateTime + "--------" + getMinTime(mLocalDateTime));
        System.out.println("获取指定日期的最大时间:"
                + mLocalDateTime + "--------" + getMaxTime(mLocalDateTime));
    }
}
打印日志:
LocalDate转string2020-10-29 00:00:00
LocalDateTime转string2020-10-29 18:43:22
string转LocalDateTime:2020-10-29T17:00:59
带毫秒的时间戳转string2020-10-29 18:43:22
不带毫秒的时间戳转string2020-10-29 18:43:22
string转毫秒时间戳:1603945240000
string转不带毫秒时间戳:1603945240
LocalDateTime转毫秒时间戳:1603968202339
LocalDateTime转秒级时间戳:1603968202
获取当前时间 一天前的LocalDateTime:2020-10-28T18:43:22.340
获取指定时间周几:星期日
时间转化:昨天
时间转化:2020-10-27T18:43:22.341--------前天
时间转化:2020-09-29T18:43:22.341--------2020-09-29
时间转化:2020-10-29T18:42:22.341--------1分钟以前
时间转化:2020-10-29T17:43:22.341--------1小时以前
时间转化:2020-10-28T19:43:22.341--------昨天
获取今日最小时间:2020-10-29T00:00
获取今日最大时间:2020-10-29T23:59:59.999999999
获取指定日期的最小时间:2020-10-28T19:43:22.341--------2020-10-28T00:00
获取指定日期的最小=大时间:2020-10-28T19:43:22.341--------2020-10-28T23:59:59.999999999