LocalDateTime/LocalDate处理工具类-LDTUtils

介绍

处理 java.time 的工具类,常用的 LocalDateTime LocalDate 操作方法封装。

代码

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

/**
 * LocalDateTime/LocalDate 处理工具类
 * @Author JustHuman
 */
public class LDTUtils {
    
    public static final String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String NORM_DATE_PATTERN = "yyyy-MM-dd";

    // ***********   parse ↓↓↓  ************

    /**
     * 字符串转LocalDateTime
     * yyyy-MM-dd HH:mm:ss
     * @param timeStr String
     * @return LocalDateTime
     */
    public static LocalDateTime parseLocalDateTime(String timeStr){
        return parseLocalDateTime(timeStr, NORM_DATETIME_PATTERN);
    }

    /**
     * 字符串转LocalDateTime
     * @param timeStr String
     * @param pattern String
     * @return LocalDateTime
     */
    public static LocalDateTime parseLocalDateTime(String timeStr, String pattern){
        return LocalDateTime.parse(timeStr,DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 字符串转LocalDate
     * yyyy-MM-dd
     * @param timeStr String
     * @return LocalDate
     */
    public static LocalDate parseLocalDate(String timeStr){
        return parseLocalDate(timeStr,NORM_DATE_PATTERN);
    }

    /**
     * 字符串转LocalDate
     * @param timeStr String
     * @param pattern String
     * @return LocalDate
     */
    public static LocalDate parseLocalDate(String timeStr, String pattern){
        return LocalDate.parse(timeStr,DateTimeFormatter.ofPattern(pattern));
    }

    // ***********   parse ↑↑↑  ************

    // ***********   format ↓↓↓  ************
    /**
     * 将LocalDateTime格式化为字符串
     * @param time LocalDateTime
     * @param pattern String
     * @return String
     */
    public static String formatTime(LocalDateTime time,String pattern) {
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 将LocalDateTime格式化为字符串
     * yyyy-MM-dd HH:mm:ss
     * @param time LocalDateTime
     * @return String
     */
    public static String formatTime(LocalDateTime time) {
        return time.format(DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN));
    }

    /**
     * 将当前时间格式化为字符串
     * @param pattern String
     * @return String
     */
    public static String formatNow(String pattern) {
        return  formatTime(LocalDateTime.now(), pattern);
    }

    /**
     * 将当前时间格式化为字符串
     * yyyy-MM-dd HH:mm:ss
     * @return String
     */
    public static String formatNow() {
        return  formatTime(LocalDateTime.now());
    }

    /**
     * 将LocalDate格式化为字符串
     * @param day LocalDate
     * @param pattern String
     * @return String
     */
    public static String formatDay(LocalDate day, String pattern) {
        return day.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 将LocalDate格式化为字符串
     * yyyy-MM-dd
     * @param day LocalDate
     * @return String
     */
    public static String formatDay(LocalDate day) {
        return day.format(DateTimeFormatter.ofPattern(NORM_DATE_PATTERN));
    }

    /**
     * 将当前日期格式化为字符串
     * @param pattern String
     * @return String
     */
    public static String formatToday(String pattern) {
        return  formatDay(LocalDate.now(), pattern);
    }

    /**
     * 将当前日期格式化为字符串
     * yyyy-MM-dd
     * @return String
     */
    public static String formatToday() {
        return  formatDay(LocalDate.now());
    }

    // ***********   format ↑↑↑  ************

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

    /**
     * LocalDateTime转换为Date
     * @param time LocalDateTime
     * @return Date
     */
    public static Date convertLDTToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取指定日期的毫秒
     * @param day LocalDate
     * @return Long
     */
    public static Long getMilliByTime(LocalDate day) {
        return getMilliByTime(getDayStart(day));
    }

    /**
     * 获取指定时间的毫秒
     * @param time LocalDateTime
     * @return Long
     */
    public static Long getMilliByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 获取指定时间的秒
     * @param day LocalDate
     * @return Long
     */
    public static Long getSecondsByTime(LocalDate day) {
        return getSecondsByTime(getDayStart(day));
    }

    /**
     * 获取指定时间的秒
     * @param time LocalDateTime
     * @return Long
     */
    public static Long getSecondsByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    /**
     * 时间加上一个数,根据field不同加不同值
     * @param time LocalDateTime
     * @param number long
     * @param field ChronoUnit
     * @return LocalDateTime
     */
    public static LocalDateTime plus(LocalDateTime time, long number, ChronoUnit field) {
        return time.plus(number, field);
    }

    /**
     * 时间减去一个数,根据field不同减不同值
     * @param time LocalDateTime
     * @param number long
     * @param field ChronoUnit
     * @return LocalDateTime
     */
    public static LocalDateTime minus(LocalDateTime time, long number, ChronoUnit field){
        return time.minus(number,field);
    }

    /**
     * 获取两个时间的差
     * @param startTime LocalDateTime
     * @param endTime LocalDateTime
     * @param field ChronoUnit
     * @return long
     */
    public static long timeDifference(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
        Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
        if (field == ChronoUnit.YEARS) return period.getYears();
        if (field == ChronoUnit.MONTHS) return period.getYears() * 12L + period.getMonths();
        return field.between(startTime, endTime);
    }

    /**
     * 获取两个日期的差
     * @param startDay LocalDate
     * @param endDay LocalDate
     * @param field ChronoUnit
     * @return long
     */
    public static long timeDifference(LocalDate startDay, LocalDate endDay, ChronoUnit field) {
        return timeDifference(startDay.atStartOfDay(), endDay.atStartOfDay(), field);
    }

    /**
     * 获取一天的开始时间
     * yyyy-MM-dd 00:00:00
     * @param time LocalDateTime
     * @return LocalDateTime
     */
    public static LocalDateTime getDayStart(LocalDateTime time) {
        return time.with(LocalTime.MIN);
    }

    /**
     * 获取一天的开始时间
     * yyyy-MM-dd 00:00:00
     * @param day LocalDate
     * @return LocalDateTime
     */
    public static LocalDateTime getDayStart(LocalDate day) {
        return day.atStartOfDay();
    }

    /**
     * 获取一天的结束时间
     * yyyy-MM-dd 23:59:59.999999999
     * @param time LocalDateTime
     * @return LocalDateTime
     */
    public static LocalDateTime getDayEnd(LocalDateTime time) {
        return time.with(LocalTime.MAX);
    }

    /**
     * 获取一天的结束时间
     * yyyy-MM-dd 23:59:59.999999999
     * @param day LocalDate
     * @return LocalDateTime
     */
    public static LocalDateTime getDayEnd(LocalDate day) {
        return day.atStartOfDay().with(LocalTime.MAX);
    }

    /**
     * 获取本周开始时间
     * @param time LocalDateTime
     * @return LocalDateTime
     */
    public static LocalDateTime getWeekStart(LocalDateTime time) {
        int dayOfWeek = time.getDayOfWeek().getValue();
        return time.minusDays(dayOfWeek - 1).with(LocalTime.MIN);
    }

    /**
     * 获取本周开始时间
     * @param day LocalDate
     * @return LocalDateTime
     */
    public static LocalDateTime getWeekStart(LocalDate day) {
        return getWeekStart(day.atStartOfDay());
    }

    /**
     * 获取本周结束时间
     * @param time LocalDateTime
     * @return LocalDateTime
     */
    public static LocalDateTime getWeekEnd(LocalDateTime time) {
        int dayOfWeek = time.getDayOfWeek().getValue();
        return time.plusDays(7 - dayOfWeek).with(LocalTime.MAX);
    }

    /**
     * 获取本周结束时间
     * @param day LocalDate
     * @return LocalDateTime
     */
    public static LocalDateTime getWeekEnd(LocalDate day) {
        return getWeekEnd(day.atStartOfDay());
    }

    /**
     * 获取月份开始时间
     * @param time LocalDateTime
     * @return LocalDateTime
     */
    public static LocalDateTime getMonthStart(LocalDateTime time) {
        return time.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
    }

    /**
     * 获取月份开始时间
     * @param day LocalDate
     * @return LocalDateTime
     */
    public static LocalDateTime getMonthStart(LocalDate day) {
        return getMonthStart(day.atStartOfDay());
    }

    /**
     * 获取月份结束时间
     * @param time LocalDateTime
     * @return LocalDateTime
     */
    public static LocalDateTime getMonthEnd(LocalDateTime time) {
        return time.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
    }

    /**
     * 获取月份结束时间
     * @param day LocalDate
     * @return LocalDateTime
     */
    public static LocalDateTime getMonthEnd(LocalDate day) {
        return getMonthEnd(day.atStartOfDay());
    }

    /**
     * 获取年份开始时间
     * @param time LocalDateTime
     * @return LocalDateTime
     */
    public static LocalDateTime getYearStart(LocalDateTime time) {
        return time.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
    }

    /**
     * 获取年份开始时间
     * @param day LocalDate
     * @return LocalDateTime
     */
    public static LocalDateTime getYearStart(LocalDate day) {
        return getYearStart(day.atStartOfDay());
    }

    /**
     * 获取年份结束时间
     * @param time LocalDateTime
     * @return LocalDateTime
     */
    public static LocalDateTime getYearEnd(LocalDateTime time) {
        return time.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
    }

    /**
     * 获取年份结束时间
     * @param day LocalDate
     * @return LocalDateTime
     */
    public static LocalDateTime getYearEnd(LocalDate day) {
        return getYearEnd(day.atStartOfDay());
    }

    /**
     * 判断两段日期是否有交集
     * @param firstStart LocalDate
     * @param firstEnd LocalDate
     * @param secondStart LocalDate
     * @param secondEnd LocalDate
     * @return Boolean
     */
    public static Boolean isInterSection(LocalDate firstStart, LocalDate firstEnd, LocalDate secondStart, LocalDate secondEnd) {
        return isInterSection(
                getDayStart(firstStart.atStartOfDay()),
                getDayEnd(firstEnd.atStartOfDay()),
                getDayStart(secondStart.atStartOfDay()),
                getDayEnd(secondEnd.atStartOfDay()));
    }

    /**
     * 判断两段时间是否有交集
     * @param firstStart LocalDateTime
     * @param firstEnd LocalDateTime
     * @param secondStart LocalDateTime
     * @param secondEnd LocalDateTime
     * @return Boolean
     */
    public static Boolean isInterSection(LocalDateTime firstStart, LocalDateTime firstEnd, LocalDateTime secondStart, LocalDateTime secondEnd) {
        LocalDateTime maxStartDate = firstStart.isAfter(secondStart) ? firstStart : secondStart;
        LocalDateTime minEndDate = firstEnd.isBefore(secondEnd) ? firstEnd : secondEnd;
        return maxStartDate.isBefore(minEndDate) || (maxStartDate.equals(minEndDate));
    }

文档

1. parseLocalDateTime

字符串转 LocalDateTime

LocalDateTime time1 = parseLocalDateTime("2024-03-28 10:23:32");
LocalDateTime time2 = parseLocalDateTime("2024-03-28 10:23", "yyyy-MM-dd HH:mm");

2024-03-28T10:23:32
2024-03-28T10:23

2. parseLocalDate

字符串转 LocalDate

LocalDate day1 = parseLocalDate("2024-03-28");
LocalDate day2 = parseLocalDate("2024.03.28", "yyyy.MM.dd");

2024-03-28
2024-03-28

3. formatTime

将 LocalDateTime 格式化为字符串

String timeStr1 = formatTime(LocalDateTime.now());
String timeStr2 = formatTime(LocalDateTime.now(), "yyyy.MM.dd HH:mm");

2024-03-28 19:34:35
2024.03.28 19:34

4. formatNow

将当前时间格式化为字符串

String timeStr1 = formatNow();
String timeStr2 = formatNow("yyyy.MM.dd HH:mm");

2024-03-28 19:34:35
2024.03.28 19:34

5. formatDay

将 LocalDate 格式化为字符串

String day1 = formatDay(LocalDate.now());
String day2 = formatDay(LocalDate.now(), "yyyy.MM.dd");

2024-03-28
2024.03.28

6. formatToday

将当前日期格式化为字符串

String day1 = formatToday();
String day2 = formatToday("yyyy.MM.dd");

2024-03-28
2024.03.28

7. convertDateToLDT

Date 转换为 LocalDateTime

LocalDateTime time = convertDateToLDT(new Date());

2024-03-28T19:42:21.381

8. convertLDTToDate

LocalDateTime转换为Date

Date time = convertLDTToDate(LocalDateTime.now());

Thu Mar 28 19:44:19 CST 2024

9. getMilliByTime

获取指定日期的毫秒

Long milli1 = getMilliByTime(parseLocalDate("2023-02-15"));
Long milli2 = getMilliByTime(parseLocalDateTime("2023-02-15 20:17:52"));

1676390400000
1676463472000

10. getSecondsByTime

获取指定时间的秒

Long seconds1 = getSecondsByTime(parseLocalDate("2023-02-15"));
Long seconds2 = getSecondsByTime(parseLocalDateTime("2023-02-15 20:17:52"));

1676390400
1676463472

11. timeDifference

获取两个时间的差

LocalDateTime time1 = parseLocalDateTime("2023-02-15 20:28:36");
LocalDateTime time2 = parseLocalDateTime("2024-03-16 08:30:21");

Long years = timeDifference(time1, time2, ChronoUnit.YEARS);
Long months = timeDifference(time1, time2, ChronoUnit.MONTHS);
Long days = timeDifference(time1, time2, ChronoUnit.DAYS);
Long hours = timeDifference(time1, time2, ChronoUnit.HOURS);
Long minutes = timeDifference(time1, time2, ChronoUnit.MINUTES);
Long seconds = timeDifference(time1, time2, ChronoUnit.SECONDS);
Long millis = timeDifference(time1, time2, ChronoUnit.MILLIS);

1
13
394
9468
568081
34084905
34084905000

获取两个日期的差

LocalDate day1 = parseLocalDate("2023-02-15");
LocalDate day2 = parseLocalDate("2024-03-16");

Long years = timeDifference(day1, day2, ChronoUnit.YEARS);
Long months = timeDifference(day1, day2, ChronoUnit.MONTHS);
Long days = timeDifference(day1, day2, ChronoUnit.DAYS);
Long hours = timeDifference(day1, day2, ChronoUnit.HOURS);
Long minutes = timeDifference(day1, day2, ChronoUnit.MINUTES);
Long seconds = timeDifference(day1, day2, ChronoUnit.SECONDS);
Long millis = timeDifference(day1, day2, ChronoUnit.MILLIS);

1
13
395
9480
568800
34128000
34128000000

12. getDayStart

获取一天的开始时间

LocalDateTime time1 = getDayStart(parseLocalDateTime("2023-02-15 20:17:52"));
LocalDateTime time2 = getDayStart(parseLocalDate("2023-02-15"));

2023-02-15T00:00
2023-02-15T00:00

13. getDayEnd

获取一天的结束时间

LocalDateTime time1 = getDayEnd(parseLocalDateTime("2023-02-15 20:17:52"));
LocalDateTime time2 = getDayEnd(parseLocalDate("2023-02-15"));

2023-02-15T23:59:59.999999999
2023-02-15T23:59:59.999999999

14. getWeekStart

获取本周开始时间

LocalDateTime time1 = getWeekStart(parseLocalDateTime("2023-02-15 20:17:52"));
LocalDateTime time2 = getWeekStart(parseLocalDate("2023-02-15"));

2023-02-13T00:00
2023-02-13T00:00

15. getWeekEnd

获取本周结束时间

LocalDateTime time1 = getWeekEnd(parseLocalDateTime("2023-02-15 20:17:52"));
LocalDateTime time2 = getWeekEnd(parseLocalDate("2023-02-15"));

2023-02-19T23:59:59.999999999
2023-02-19T23:59:59.999999999

16. getMonthStart

获取月份开始时间

LocalDateTime time1 = getMonthStart(parseLocalDateTime("2023-02-15 20:17:52"));
LocalDateTime time2 = getMonthStart(parseLocalDate("2023-02-15"));

2023-02-01T00:00
2023-02-01T00:00

17. getMonthEnd

获取月份结束时间

LocalDateTime time1 = getMonthEnd(parseLocalDateTime("2023-02-15 20:17:52"));
LocalDateTime time2 = getMonthEnd(parseLocalDate("2023-02-15"));

2023-02-28T23:59:59.999999999
2023-02-28T23:59:59.999999999

18. getYearStart

获取年份开始时间

LocalDateTime time1 = getYearStart(parseLocalDateTime("2023-02-15 20:17:52"));
LocalDateTime time2 = getYearStart(parseLocalDate("2023-02-15"));

2023-01-01T00:00
2023-01-01T00:00

19. getYearEnd

获取年份结束时间

LocalDateTime time1 = getYearEnd(parseLocalDateTime("2023-02-15 20:17:52"));
LocalDateTime time2 = getYearEnd(parseLocalDate("2023-02-15"));

2023-12-31T23:59:59.999999999
2023-12-31T23:59:59.999999999

20. isInterSection

判断两段日期是否有交集

LocalDate firstStart = parseLocalDate("2024-03-01");
LocalDate firstEnd = parseLocalDate("2024-03-14");
LocalDate secondStart = parseLocalDate("2024-03-15");
LocalDate secondEnd = parseLocalDate("2024-03-31");
Boolean result = isInterSection(firstStart, firstEnd, secondStart, secondEnd);

false

LocalDate firstStart = parseLocalDate("2024-03-01");
LocalDate firstEnd = parseLocalDate("2024-03-18");
LocalDate secondStart = parseLocalDate("2024-03-11");
LocalDate secondEnd = parseLocalDate("2024-03-31");
Boolean result = isInterSection(firstStart, firstEnd, secondStart, secondEnd);

true

判断两段时间是否有交集

LocalDateTime firstStart = parseLocalDateTime("2024-03-01 08:20:21");
LocalDateTime firstEnd = parseLocalDateTime("2024-03-02 08:20:21");
LocalDateTime secondStart = parseLocalDateTime("2024-03-03 09:20:21");
LocalDateTime secondEnd = parseLocalDateTime("2024-03-04 08:20:21");
Boolean result = isInterSection(firstStart, firstEnd, secondStart, secondEnd);

false

LocalDateTime firstStart = parseLocalDateTime("2024-03-03 07:20:21");
LocalDateTime firstEnd = parseLocalDateTime("2024-03-03 09:20:21");
LocalDateTime secondStart = parseLocalDateTime("2024-03-02 09:20:21");
LocalDateTime secondEnd = parseLocalDateTime("2024-03-03 10:20:21");
Boolean result = isInterSection(firstStart, firstEnd, secondStart, secondEnd);

true

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值