whyclxw
10 小时以前 cae57767c58673966d05a4d5d5855216fa3f07d4
src/main/java/com/whyc/util/DateUtil.java
@@ -2,6 +2,10 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
/**
@@ -704,33 +708,69 @@
    }
    public static void main(String[] args) throws ParseException {
        //Date d1 = YYYY_MM_DD_HH_MM_SS.parse("2023-01-22 14:12:22");
        //Date d2 = YYYY_MM_DD_HH_MM_SS.parse("2023-08-24 16:45:22");
        //Map<String, List<Date>> monthListDesc = getQueryTimeForSubTablesByMonthDesc(d1, d2);
        //
        //Set<Map.Entry<String, List<Date>>> entries = monthListDesc.entrySet();
        //for (Map.Entry<String, List<Date>> entry : entries) {
        //    System.out.println(entry);
        //}
        //Map<String,String> map = new HashMap<>();
        //map.put("2022_01","");
        //map.put("2022_02","");
        //map.put("2022_10","");
        //map.put("2022_12","");
        //map.put("2021_03","");
        //map.put("2023_01","");
        //map.put("2023_05","");
        //map.put("2021_10","");
        //Set<String> keySet = map.keySet();
        //List<String> keySetDesc = keySet.stream().sorted(Comparator.comparing(String::hashCode).reversed()).collect(Collectors.toList());
        String t1 = "2024-01-25 12:22:29";
        String t2 = "2024-01-25 12:32:24";
        String t3 = "2024-01-25 12:42:26";
        Date maxTime = getMaxTime(t1, t2, t3);
        System.out.println(maxTime.toString());
    /**
     * 获取本年的开始时间
     */
    public static LocalDateTime getStartOfYear() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.firstDayOfYear()).with(LocalDateTime.MIN.toLocalTime());
    }
    /**
     * 获取本季度的开始时间
     */
    public static LocalDateTime getStartOfQuarter() {
        LocalDateTime now = LocalDateTime.now();
        int currentMonthValue = now.getMonthValue();
        // 计算当前季度的第一个月
        int quarterStartMonth = 3 * ((currentMonthValue - 1) / 3) + 1;
        return LocalDate.of(now.getYear(), quarterStartMonth, 1).atStartOfDay();
    }
    /**
     * 获取本月的开始时间
     */
    public static LocalDateTime getStartOfMonth() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.firstDayOfMonth()).with(LocalDateTime.MIN.toLocalTime());
    }
    /**
     * Date 转 LocalDateTime 工具方法
     */
    public static LocalDateTime convertToLocalDateTime(java.util.Date date) {
        return date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
    }
    /**
     * LocalDateTime转 Date
     */
    public static Date convertToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
    //1月/2季度/3年得到开始时间到当前月的结束时间
    public static Map<String,Date> getStartAndEndTime(Integer sticTime) throws ParseException {
        Map<String,Date> map=new HashMap();
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDate=now;//默认
        // 获取本月的最后一天
        LocalDateTime endtDate=now.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59).withNano(0);
        //指定年月获取这个月的开始时间和结束时间
        if(sticTime==1){
            startDate=DateUtil.getStartOfMonth();
        }
        if(sticTime==2){
            startDate=DateUtil.getStartOfQuarter();
        }
        if(sticTime==3){
            startDate=DateUtil.getStartOfYear();
        }
        map.put("startTime",DateUtil.convertToDate(startDate));
        map.put("endTime",DateUtil.convertToDate(endtDate));
        return map;
    }
}