ConstDateUtil.java 6.5 KB
package com.ruoyi.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ConstDateUtil {

    public static String getStringNowLocalDate(){
        // 指定当前日期和时间
        LocalDate currentDate = LocalDate.now();
        LocalTime timeRange = LocalTime.of(2, 50);
        LocalTime now = LocalTime.now();
        if (now.isBefore(timeRange)){
            // 在日期的前一天
            currentDate = currentDate.minusDays(1);
        }
        return currentDate.toString();
    }

    public static String getStringNowLocalDate(String delStr){
        // 指定当前日期和时间
        LocalDate currentDate = LocalDate.now();
        LocalTime timeRange = LocalTime.of(2, 50);
        LocalTime now = LocalTime.now();
        if (now.isBefore(timeRange)){
            // 在日期的前一天
            currentDate = currentDate.minusDays(1);
        }
        return currentDate.toString().replaceAll(delStr,"");
    }


    public static LocalDateTime stringTransformLocalDateTime(String dateString, String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(dateString,formatter);
    }


    public static String formatDate(String pattern){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(new Date());
    }
    public static String formatDate(String pattern,ZoneOffset utc){
        LocalDateTime currentTime = LocalDateTime.now(utc);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return formatter.format(currentTime);
    }

    public static Date parseDate(String date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String formatDate(String pattern,Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    public static String formatDate(String pattern,LocalDateTime date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return date.format(formatter);
    }

    public static String formatDate(String pattern,LocalDate date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }
    public static String formatDate(long time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        return simpleDateFormat.format(new Date(time));
    }
    public static String formatDate(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        return simpleDateFormat.format(date);
    }

    public static Date dateAddition(String dateString,String timeString){
        LocalDate date = LocalDate.parse(dateString);
        LocalTime time = LocalTime.parse(timeString);

        // 将日期和时间相加
        LocalDateTime dateTime = date.atTime(time);

        return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date getTheSpecifiedNumberOfDaysOfTime(Integer amount) {
        // 获取当前日期时间
        Calendar calendar = Calendar.getInstance();
        // 将日期减去一天
        calendar.add(Calendar.DAY_OF_MONTH, amount);
        // 获取昨天的日期时间
        return calendar.getTime();
    }


    public static LocalDateTime getLocalDateTimeByLongTime(long time){
        Instant instant = Instant.ofEpochMilli(time);
        LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime;
    }

    public static List<LocalDate> getDateSetFromTheCurrentDayToTheEndOfTheMonth(){
        LocalDate today = LocalDate.now();
        int year = today.getYear();
        int month = today.getMonthValue();

        // 获取本月的最后一天
        LocalDate endDay;
        if (month == 12) {
            endDay = LocalDate.of(year, month, 31);
        } else {
            endDay = LocalDate.of(year, month + 1, 1).minusDays(1);
        }

        // 生成日期列表
        List<LocalDate> dateList = new ArrayList<>();
        LocalDate currentDay = today;
        while (!currentDay.isAfter(endDay)) {
            dateList.add(currentDay);
            currentDay = currentDay.plusDays(1);
        }

        return dateList;
    }

    public static List<LocalDate> getDateSetFromTheCurrentDayToTheEndOfTheMonth(int year,int month){
        LocalDate today = LocalDate.of(year,month,1);

        // 获取本月的最后一天
        LocalDate endDay;
        if (month == 12) {
            endDay = LocalDate.of(year, month, 31);
        } else {
            endDay = LocalDate.of(year, month + 1, 1).minusDays(1);
        }

        // 生成日期列表
        List<LocalDate> dateList = new ArrayList<>();
        LocalDate currentDay = today;
        while (!currentDay.isAfter(endDay)) {
            dateList.add(currentDay);
            currentDay = currentDay.plusDays(1);
        }

        return dateList;
    }
    public static List<LocalDate> getDateSetFromTheCurrentDayToTheEndOfTheMonth(int year,int month,int day){
        LocalDate today = LocalDate.of(year,month,day);

        // 获取本月的最后一天
        LocalDate endDay;
        if (month == 12) {
            endDay = LocalDate.of(year, month, 31);
        } else {
            endDay = LocalDate.of(year, month + 1, 1).minusDays(1);
        }

        // 生成日期列表
        List<LocalDate> dateList = new ArrayList<>();
        LocalDate currentDay = today;
        while (!currentDay.isAfter(endDay)) {
            dateList.add(currentDay);
            currentDay = currentDay.plusDays(1);
        }

        return dateList;
    }



    /**
     * 获取指定月份所有日期
     */
    public static List<LocalDate> getAllDatesOfTheMonth(int year,int month){
        YearMonth yearMonth = YearMonth.of(year, month);
        int daysInMonth = yearMonth.lengthOfMonth();
        List<LocalDate> dateList = new ArrayList<>(30);
        for (int day = 1; day <= daysInMonth; day++) {
            LocalDate date = LocalDate.of(year, month, day);
            dateList.add(date);
        }

        return dateList;
    }


}