LocalDateTime、LocalDate常用工具类及部分方法

98 阅读2分钟

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.Year;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

@Slf4j
public class TimeUtil {

    private TimeUtil() {
    }

    /******************** LocalDateTime ********************/

    /**
     * LocalDateTime常用方法
     * getYear():获取年份
     * getMonth():获取月份
     * getDayOfMonth():获取日期
     * getHour():获取小时数
     * getMinute():获取分钟数
     * getSecond():获取秒数
     * <p>
     * plusYears(long years):获取指定年数后的时间
     * plusMonths(long months):获取指定月数后的时间
     * plusDays(long days):获取指定天数后的时间
     * plusHours(long hours):获取指定小时后的时间
     * plusMinutes(long minutes):获取指定分钟后的时间
     * plusSeconds(long seconds):获取指定秒后的时间
     * minusYears(long years):获取指定年数前的时间
     * minusMonths(long months):获取指定月数前的时间
     * minusDays(long days):获取指定天数前的时间
     * minusHours(long hours):获取指定小时前的时间
     * minusMinutes(long minutes):获取指定分钟前的时间
     * minusSeconds(long seconds):获取指定秒前的时间
     **/

    public static int getNumOfYearFromLdt(LocalDateTime localDateTime) {
        return Year.from(localDateTime).getValue();
    }

    public static int getNumOfMonthFromLdt(LocalDateTime localDateTime) {
        return Month.from(localDateTime).getValue();
    }

    /**
     * @description: 获取指定时间
     * @return: java.time.LocalDateTime
     **/
    public static LocalDateTime getThePointedTime(Integer numOfYear,
                                                  Integer numOfMonth,
                                                  Integer numOfDay,
                                                  Integer numOfHour,
                                                  Integer numOfMinute,
                                                  Integer numOfSecond) {
        return LocalDateTime.of(numOfYear, numOfMonth, numOfDay, numOfHour, numOfMinute, numOfSecond);
    }

    /**
     * @description: 获取指定年份的起始时间  例如指定2022月,则返回 2022-01-01T00:00
     * @return: java.time.LocalDateTime
     **/
    public static LocalDateTime getThePointedYear(Integer numOfYear) {
        return getThePointedTime(numOfYear, 1, 1, 0, 0, 0);
    }

    /**
     * @description: 获取今年指定月份的起始时间  例如现在是2024年,指定5月,则返回 2024-05-01T00:00
     * @return: java.time.LocalDateTime
     **/
    public static LocalDateTime getThePointedMonth(Integer numOfMonth) {
        int numOfYear = getNumOfYearFromLdt(LocalDateTime.now());
        return getThePointedTime(numOfYear, numOfMonth, 1, 0, 0, 0);
    }

    /**
     * @description: 获取今年今月指定号数的起始时间  例如现在是2024年5月,指定10号,则返回 2024-05-10T00:00
     * @return: java.time.LocalDateTime
     **/
    public static LocalDateTime getThePointedDay(Integer numOfDay) {
        int numOfYear = getNumOfYearFromLdt(LocalDateTime.now());
        int numOfMonth = getNumOfMonthFromLdt(LocalDateTime.now());
        return getThePointedTime(numOfYear, numOfMonth, numOfDay, 0, 0, 0);
    }

    /**
     * @description: 获取当前月份的结尾时间
     * @return: java.time.LocalDateTime
     **/
    public static LocalDateTime theLastDayTimeOfMonth(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23)
                .withMinute(59)
                .withSecond(59);
    }

    /**
     * @description: 获取当前月份的开始时间
     * @return: java.time.LocalDateTime
     **/
    public static LocalDateTime theFirstDayTimeOfNextMonth(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.firstDayOfNextMonth())
                .withHour(0)
                .withMinute(0)
                .withSecond(0);
    }

    /**
     * @param dateTime     时间字符串
     * @param patternValue 转换格式 查看枚举 TimeEnumSummary
     * @description: 字符串时间转LocalDateTime
     * @return: java.time.LocalDateTime
     **/
    public static LocalDateTime transferString2LocalDateTime(String dateTime, String patternValue) {
        if (StringUtils.isBlank(dateTime) || StringUtils.isBlank(patternValue)) {
            return null;
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patternValue);
        return LocalDateTime.parse(dateTime, formatter);
    }

    /**
     * @description: Date转LocalDateTime
     * @return: java.time.LocalDateTime
     **/
    public static LocalDateTime transferDate2LocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /******************** LocalDate ********************/

    /**
     * plusDays() 获取指定天数后的日期
     * minusDays() 获取指定天数前的日期
     * minusYears(x) 获取指定年数前的日期
     * minusMonths(x) 获取指定月数前的日期
     * with(TemporalAdjusters.firstDayOfMonth()) 获取指定日期所属月的第一天日期
     * 还有部分可自行查看,可直接使用
     **/

    /**
     * @param dateString:   时间字符串
     * @param patternValue: 转换格式 查看枚举 TimeEnumSummary
     * @description: 时间字符串转LocalDate
     * @return: java.time.LocalDate
     **/
    public static LocalDate transferString2LocalDate(String dateString, String patternValue) {
        if (StringUtils.isBlank(dateString) || StringUtils.isBlank(patternValue)) {
            return null;
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patternValue);
        return LocalDate.parse(dateString, formatter);
    }
}

import lombok.Getter;

@Getter
public enum TimeEnumSummary {

    YYYY_MM_DD("yyyy-MM-dd"),
    YYYY_MM_DD_HH_MM("yyyy-MM-dd HH:mm"),
    YYYY_MM_DD_HH_MM_SS("yyyy-MM-dd HH:mm:ss"),
    ;

    private final String value;

    TimeEnumSummary(String value) {
        this.value = value;
    }
}