LocalDateUtils.java 7.25 KB
package com.ruoyi.common.utils;

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

/**
 * @Author: ly
 * 时间工具类
 */
public class LocalDateUtils {
    private boolean isNull=false;
    public static enum TimeUnit{
        Year,
        Month,
        Week,
        Day,
        Hour,
        Minute,
        Second
    }
    public LocalDateTime localDate= LocalDateTime.now();
    public static LocalDateUtils of(){return new LocalDateUtils();}


    public static void main(String[] args) {
        LocalDateUtils localDateUtils = LocalDateUtils.of(new Date());
        System.out.println(localDateUtils.time_addOrSub(-3, TimeUnit.Year).getFormatStr("yyyy"));
    }
    public static LocalDateUtils of(Object object){
        LocalDateUtils localDateUtils = new LocalDateUtils();
        if (object instanceof Date){
            Date date=(Date)object;
            localDateUtils.localDate=date.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
            return localDateUtils;
        }else if(object instanceof LocalDateTime){
            localDateUtils.localDate=(LocalDateTime)object;
        }else if (object instanceof String){
            String dateStr = object.toString();
            if (!dateStr.contains("-")&dateStr.matches("\\d+")){
                localDateUtils.localDate=Instant.ofEpochMilli(Long.parseLong(dateStr)).atZone(ZoneId.systemDefault()).toLocalDateTime();
            }else{
                dateStr=(!dateStr.contains(" ")&&dateStr.contains("-"))?dateStr+" 00:00:00":dateStr;
                DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                localDateUtils.localDate=LocalDateTime.parse(dateStr, df);
            }
        } else if (object instanceof Long) {
            localDateUtils.localDate=Instant.ofEpochMilli((Long)object).atZone(ZoneId.systemDefault()).toLocalDateTime();
        }else{
            localDateUtils.isNull=true;
        }
        return localDateUtils;
    }
    public String getYMD(){
        if (isNull)return null;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = dateTimeFormatter.format(localDate);
        return format;
    }
    public String getYMD_HMS(){
        if (isNull)return null;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(localDate);
        return format;
    }

    /**
     * 获取Date对象
     * @return
     */
    public Date getDate(){
        if (isNull)return null;
        Date from = Date.from(localDate.atZone(ZoneOffset.systemDefault()).toInstant());
        return from;
    }
    public String getFormatStr(String formatDateStr){
        if (isNull)return null;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatDateStr);
        String format = dateTimeFormatter.format(localDate);
        return format;
    }

    /**
     * 日期加减
     * @param num
     * @param timeUnit
     * @return
     */
    public LocalDateUtils time_addOrSub(long num,TimeUnit timeUnit){
        if (isNull)return null;
        if (num>0){
            if (TimeUnit.Year==timeUnit){localDate=localDate.plusYears(num);}else
            if (TimeUnit.Month==timeUnit){localDate=localDate.plusMonths(num);}else
            if (TimeUnit.Week==timeUnit){localDate=localDate.plusWeeks(num);}else
            if (TimeUnit.Day==timeUnit){localDate=localDate.plusDays(num);}else
            if (TimeUnit.Hour==timeUnit){localDate=localDate.plusHours(num);}else
            if (TimeUnit.Minute==timeUnit){localDate=localDate.plusMinutes(num);}else
            if (TimeUnit.Second==timeUnit){localDate=localDate.plusSeconds(num);}
        }else{
            num=Math.abs(num);
            if (TimeUnit.Year==timeUnit){localDate=localDate.minusYears(num);}else
            if (TimeUnit.Month==timeUnit){localDate=localDate.minusMonths(num);}else
            if (TimeUnit.Week==timeUnit){localDate=localDate.minusWeeks(num);}else
            if (TimeUnit.Day==timeUnit){localDate=localDate.minusDays(num);}else
            if (TimeUnit.Hour==timeUnit){localDate=localDate.minusHours(num);}else
            if (TimeUnit.Minute==timeUnit){localDate=localDate.minusMinutes(num);}else
            if (TimeUnit.Second==timeUnit){localDate=localDate.minusSeconds(num);}
        }
        return this;
    }

    /**
     * 获取毫秒数 时间戳
     * @return
     */
    public Long getMilli(){
        if (isNull)return null;
        return localDate.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }
    //指定具体时间
    public LocalDateUtils withTime(int num ,TimeUnit timeUnit){
        if (isNull)return null;
        if (TimeUnit.Year==timeUnit)localDate=localDate.withYear(num);
        if (TimeUnit.Month==timeUnit)localDate=localDate.withMonth(num);
        if (TimeUnit.Week==timeUnit)throw new RuntimeException("不支持指定星期");
        if (TimeUnit.Day==timeUnit)localDate=localDate.withDayOfMonth(num);
        if (TimeUnit.Hour==timeUnit)localDate=localDate.withHour(num);
        if (TimeUnit.Minute==timeUnit)localDate=localDate.withMinute(num);
        if (TimeUnit.Second==timeUnit)localDate=localDate.withSecond(num);
        return this;
    }
    public static Boolean equal(Date date1,Date date2){
        LocalDateTime stL=date1.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
        LocalDateTime edL=date2.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
        return stL.equals(edL);
    }

    /**
     * 获取时间单独位
     * @param timeUnit
     * @return
     */
    public Integer dateInfo(TimeUnit timeUnit){
        if (isNull)return null;
        //年
        int year = localDate.getYear();
        //直接获取
        int monthValue = localDate.getMonthValue();
        //日
        int dayOfMonth1 = localDate.getDayOfMonth();
        //时
        int hour = localDate.getHour();
        //分
        int minute = localDate.getMinute();
        //秒
        int second = localDate.getSecond();
        if (TimeUnit.Year==timeUnit)return year;
        if (TimeUnit.Month==timeUnit)return monthValue;
        if (TimeUnit.Day==timeUnit)return dayOfMonth1;
        if (TimeUnit.Hour==timeUnit)return hour;
        if (TimeUnit.Minute==timeUnit)return minute;
        if (TimeUnit.Second==timeUnit)return second;
        return 0;
    }

    /**
     * 两日期间隔多少天
     * @param start
     * @param end
     * @return
     */
    public static Long betweenDays(Date start,Date end){
        LocalDateTime stL=start.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
        LocalDateTime edL=end.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
        Duration between = Duration.between(stL, edL);
        return between.toDays();
    }
    public static Long betweenDays(LocalDateTime start,LocalDateTime end){
        long l = ChronoUnit.DAYS.between(start, end);
        return l;
    }





    /**
     * LocalDate 转 Date
     * @param localDate
     * @return
     */
    public static Date convertLocalDateToDate(LocalDateTime localDate) {
        ZoneId zoneId = ZoneId.systemDefault();
        return Date.from(localDate.atZone(zoneId).toInstant());
    }
}