nickchange
2023-10-10 ee9cb0da4a43bcf523ebb157678f64a2895fba1a
cloud-server-course/src/main/java/com/dsh/course/util/DateTimeHelper.java
@@ -2,9 +2,8 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
@@ -1439,6 +1438,24 @@
        return weekDays[w];
    }
    /**
     * 获取当前日期是星期几<br>
     * Obtain the day of the week for the current date.
     * @param dt
     * @return 当前日期是星期几
     */
    public static int getWeekNumOfDate(Date dt) {
        Integer[] weekDays = {7, 1, 2, 3, 4, 5, 6}; // 将周一的数值设为2,依次递增表示周二到周日
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 6; // 如果为负数,将其设为6,表示周日
        }
        return weekDays[w];
    }
    /**
     * 获取今天时间 2017-11-20 00:00:00
     */
@@ -1757,13 +1774,33 @@
        return list;
    }
    /*public static void main(String[] args) throws ParseException {
        List<Date> allTheDateOftheMonth = getAllTheDateOftheMonth(new Date());
        Date date = allTheDateOftheMonth.get(11);
        System.out.println(date.toLocaleString());
        int i = daysBetween(new Date(), date);
        System.out.println(i);
    }*/
    public static Date getTodayTime(){
        // 获取当天的开始时间(0点)
        LocalDate today = LocalDate.now();
        LocalDateTime startOfDay = LocalDateTime.of(today, LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }
    public static Date getWeekOfLastDay(){
        // 获取本周的最后一天的时间(23点59分59秒)
        LocalDate today = LocalDate.now();
        LocalDate lastDayOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        LocalDateTime endOfDay = LocalDateTime.of(lastDayOfWeek, LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }
    /**
     * 获取有效期数的结束时间
     * @param startTime 开始时间
     * @param validPeriod 有效期数
     * @return 结束时间
     */
    public static Date getExpirationDate(Date startTime, int validPeriod){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.DAY_OF_MONTH, validPeriod);
        return calendar.getTime();
    }
}