| | |
| | | package com.zy.crm.manager.utils; |
| | | |
| | | import javax.xml.crypto.Data; |
| | | import java.time.DayOfWeek; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.ZoneId; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.TimeZone; |
| | | import java.time.temporal.TemporalAdjusters; |
| | | import java.time.temporal.WeekFields; |
| | | import java.util.*; |
| | | |
| | | public class TimeCalculatorUtils { |
| | | |
| | | public static int getTimeHour(){ |
| | | //如何获取当前时间的小时数(24小时制): |
| | | //获取当前时间的小时数(24小时制): |
| | | public static int nowTimeHour(){ |
| | | // 获取当前时间戳 |
| | | long timestamp = System.currentTimeMillis(); |
| | | |
| | |
| | | return calendar.get(java.util.Calendar.HOUR_OF_DAY); |
| | | } |
| | | |
| | | public static int getTimeHour(Date date){ |
| | | //获取当天小时数(24小时制)中国北京时间: |
| | | public static int timeTimeHour(Date date){ |
| | | // 创建TimeZone对象,设置为中国北京时间 |
| | | TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai"); |
| | | |
| | |
| | | return calendar.get(java.util.Calendar.HOUR_OF_DAY); |
| | | } |
| | | |
| | | public static Date getYesterday(Date date){ |
| | | //获取前一天的日期(日期天数减一) |
| | | public static Date timeYesterday(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.add(Calendar.DAY_OF_MONTH, -1); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | public static String getYestMonthDay(Date date){ |
| | | //获取七天前的日期(日期天数减七) |
| | | public static Date timeFrontSevenYesterday(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.add(Calendar.DAY_OF_MONTH, -7); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | //获取七天后的日期(日期天数加七) |
| | | public static Date timeAfterSevenYesterday(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.add(Calendar.DAY_OF_MONTH, 7); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | //获取相差n天的日期(日期天数加N) |
| | | public static Date timeYesterdayN(Date date,int day){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.add(Calendar.DAY_OF_MONTH, day); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | //获取年月日 |
| | | public static String timeYestMonthDay(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | |
| | |
| | | return year+"年"+month+"月"+day+"日"; |
| | | } |
| | | |
| | | public static int getDifferenceDayInt(Date startDay,Date endDay){ |
| | | return getDifferenceDayLong(startDay,endDay).intValue(); |
| | | //获取年 |
| | | public static int timeYest(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | |
| | | return calendar.get(Calendar.YEAR); |
| | | } |
| | | |
| | | public static Long getDifferenceDayLong(Date startDay,Date endDay){ |
| | | LocalDate localDateA = dateToLocalDate(startDay); |
| | | LocalDate localDateB = dateToLocalDate(endDay); |
| | | //获取月 |
| | | public static int timeMonth(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | |
| | | return getDifferenceDays(localDateA, localDateB); |
| | | return calendar.get(Calendar.MONTH) + 1; |
| | | } |
| | | |
| | | public static Long getDifferenceDayDouble(String startDay,String endDay){ |
| | | // 定义日期字符串的格式 |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日ahh:mm"); |
| | | //获取日 |
| | | public static int timeDay(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | |
| | | return calendar.get(Calendar.DAY_OF_MONTH); |
| | | } |
| | | //获取年差异 |
| | | public static int DifferenceYest(Date startDay,Date endDay){ |
| | | int startYest = timeYest(startDay); |
| | | int endYest = timeYest(endDay); |
| | | return endYest-startYest; |
| | | } |
| | | //获取月差异 |
| | | public static int DifferenceMonth(Date startDay,Date endDay){ |
| | | int startMonth = timeMonth(startDay); |
| | | int endMonth = timeMonth(endDay); |
| | | return endMonth-startMonth; |
| | | } |
| | | //获取天差异 |
| | | public static int DifferenceDayInt(Date startDay,Date endDay){ |
| | | return DifferenceDayLong(startDay,endDay).intValue(); |
| | | } |
| | | //获取天差异 |
| | | public static Long DifferenceDayLong(Date startDay,Date endDay){ |
| | | |
| | | LocalDate a = LocalDate.of(timeYest(startDay), timeMonth(startDay), timeDay(startDay)); |
| | | LocalDate b = LocalDate.of(timeYest(endDay), timeMonth(endDay), timeDay(endDay)); |
| | | |
| | | return ChronoUnit.DAYS.between(a, b)+1; |
| | | } |
| | | //获取天差异 |
| | | public static Double DifferenceDayMorningAfternoon(Date startDay,Date endDay,int startTime,int endTime){ |
| | | double between = DifferenceDayLong(startDay, endDay).doubleValue(); |
| | | if (startTime==2){ |
| | | between = between-0.5; |
| | | } |
| | | if (endTime == 1){ |
| | | between = between-0.5; |
| | | } |
| | | return between; |
| | | } |
| | | |
| | | public static boolean CompareData(Date startDay,Date endDay){ |
| | | |
| | | // 将日期字符串解析为LocalDate对象 |
| | | LocalDate dateA = LocalDate.parse(startDay, formatter); |
| | | LocalDate dateB = LocalDate.parse(endDay, formatter); |
| | | LocalDate a = LocalDate.of(timeYest(startDay), timeMonth(startDay), timeDay(startDay)); |
| | | LocalDate b = LocalDate.of(timeYest(endDay), timeMonth(endDay), timeDay(endDay)); |
| | | |
| | | // 计算日期对象之间的相隔天数 |
| | | return ChronoUnit.DAYS.between(dateA, dateB); |
| | | |
| | | } |
| | | |
| | | private static String getDatePart(String dateTime) { |
| | | int index = dateTime.indexOf(" "); |
| | | if (index != -1) { |
| | | return dateTime.substring(0, index); |
| | | // 比较日期 |
| | | if (a.isAfter(b)) { |
| | | return false; |
| | | } else if (a.isBefore(b)) { |
| | | return true; |
| | | } else { |
| | | return true; |
| | | } |
| | | return dateTime; |
| | | } |
| | | |
| | | private static String getTimePart(String dateTime) { |
| | | int index = dateTime.indexOf(" "); |
| | | if (index != -1) { |
| | | return dateTime.substring(index + 1); |
| | | //java.time.LocalDate 转换为 java.util.Date; |
| | | public static Date DateLocalDate(LocalDate localDate) { |
| | | return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); |
| | | } |
| | | |
| | | //获取现在日期是当月第几周 |
| | | public static int WeeklyNowMonth(Date date) { |
| | | // LocalDate currentDate = LocalDate.now(); |
| | | LocalDate currentDate = LocalDate.of(timeYest(date), timeMonth(date), timeDay(date)); |
| | | |
| | | WeekFields weekFields = WeekFields.of(Locale.getDefault()); |
| | | |
| | | int weekOfMonth = currentDate.get(weekFields.weekOfMonth()); |
| | | |
| | | |
| | | return weekOfMonth; |
| | | } |
| | | |
| | | //限制两个日期是周一和周末 工作日 周一到周六 |
| | | public static void WeeklySix(Date date) { |
| | | // LocalDate startDate = LocalDate.of(2022, 9, 5); // 假设开始日期是2022年9月5日,周一 |
| | | LocalDate startDate = LocalDate.of(timeYest(date), timeMonth(date), timeDay(date)); |
| | | System.out.println("当前日期 " + startDate + " 周"); |
| | | |
| | | LocalDate endDate = startDate.plus(6, ChronoUnit.DAYS); // 结束日期是开始日期加上6天 |
| | | System.out.println("当前日期加上6天是 " + endDate + " 周"); |
| | | |
| | | |
| | | // 如果开始日期不是周一,则将它调整到下一个周一 |
| | | if (startDate.getDayOfWeek() != DayOfWeek.MONDAY) { |
| | | startDate = startDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY)); |
| | | } |
| | | return ""; |
| | | |
| | | // 如果结束日期不是周末(即周六或周日),则将它调整到下一个周末 |
| | | if (endDate.getDayOfWeek() != DayOfWeek.SATURDAY && endDate.getDayOfWeek() != DayOfWeek.SUNDAY) { |
| | | endDate = endDate.with(TemporalAdjusters.next(DayOfWeek.SATURDAY)); |
| | | } |
| | | |
| | | // 确保两个日期之间相隔七天 |
| | | while (startDate.plus(7, ChronoUnit.DAYS).isBefore(endDate)) { |
| | | startDate = startDate.plus(7, ChronoUnit.DAYS); |
| | | } |
| | | |
| | | System.out.println("开始日期:" + startDate); |
| | | System.out.println("结束日期:" + endDate); |
| | | } |
| | | |
| | | private static LocalDate dateToLocalDate(Date date) { |
| | | return date.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate(); |
| | | //限制两个日期是周一和周末 周一到周日 |
| | | public static List<Date> WeeklySeven(Date date) { |
| | | // LocalDate startDate = LocalDate.of(2022, 9, 5); // 假设开始日期是2022年9月5日,周一 |
| | | LocalDate startDate = LocalDate.of(timeYest(date), timeMonth(date), timeDay(date)); |
| | | // System.out.println("当前日期 " + startDate + " 周"); |
| | | |
| | | LocalDate endDate = startDate.plus(6, ChronoUnit.DAYS); // 结束日期是开始日期加上6天 |
| | | // System.out.println("当前日期加上6天是 " + endDate + " 周"); |
| | | |
| | | // 如果开始日期不是周一,则将它调整到下一个周一 |
| | | if (startDate.getDayOfWeek() != DayOfWeek.MONDAY) { |
| | | startDate = startDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY)); |
| | | } |
| | | |
| | | // 如果结束日期不是周日,则将它调整到下一个周日 |
| | | if (endDate.getDayOfWeek() != DayOfWeek.SUNDAY) { |
| | | endDate = endDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)); |
| | | } |
| | | |
| | | // 确保两个日期之间相隔七天 |
| | | while (startDate.plus(7, ChronoUnit.DAYS).isBefore(endDate)) { |
| | | startDate = startDate.plus(7, ChronoUnit.DAYS); |
| | | } |
| | | |
| | | List<Date> dateList = new ArrayList<>(); |
| | | dateList.add(DateLocalDate(startDate)); |
| | | dateList.add(DateLocalDate(endDate)); |
| | | return dateList; |
| | | // System.out.println("开始日期:" + startDate); |
| | | // System.out.println("结束日期:" + endDate); |
| | | } |
| | | |
| | | private static long getDifferenceDays(LocalDate dateA, LocalDate dateB) { |
| | | return ChronoUnit.DAYS.between(dateA, dateB); |
| | | //判断是不是周一 |
| | | public static boolean WeeklyOneSign(Date date) { |
| | | LocalDate localDate = LocalDate.of(timeYest(date), timeMonth(date), timeDay(date)); |
| | | // 判断是不是周一 |
| | | if (localDate.getDayOfWeek() != DayOfWeek.MONDAY) { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | //判断是不是周一 如果日期不是周一,则将它调整到本周周一 |
| | | public static Date WeeklyMonday (Date date) { |
| | | LocalDate localDate = LocalDate.of(timeYest(date), timeMonth(date), timeDay(date)); |
| | | // 判断是不是周一 则将它调整到下周周一 |
| | | if (WeeklyOneSign(date)) { |
| | | localDate = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY)); |
| | | } |
| | | // 将LocalDate转换为Date |
| | | Date monday = DateLocalDate(localDate); |
| | | |
| | | // 则将它调整到本周周一 |
| | | Date nowMonday = timeFrontSevenYesterday(monday); |
| | | |
| | | |
| | | return nowMonday; |
| | | } |
| | | |
| | | //获取当前日期所在的周一及周日时间 |
| | | public static List<Date> WeeklyMondayAndSundayNow(Date date) { |
| | | Date nowMonday = WeeklyMonday(date); |
| | | return WeeklySeven(nowMonday); |
| | | } |
| | | |
| | | //获取当前日期上一周周一及周日时间 |
| | | public static List<Date> WeeklyMondayAndSundayFront(Date date) { |
| | | Date nowMonday = WeeklyMonday(date); |
| | | Date frontSevenYesterday = timeFrontSevenYesterday(nowMonday); |
| | | return WeeklySeven(frontSevenYesterday); |
| | | } |
| | | |
| | | //获取当前日期下一周周一及周日时间 |
| | | public static List<Date> WeeklyMondayAndSundayAfter(Date date) { |
| | | Date nowMonday = WeeklyMonday(date); |
| | | Date afterSevenYesterday = timeAfterSevenYesterday(nowMonday); |
| | | return WeeklySeven(afterSevenYesterday); |
| | | } |
| | | |
| | | //获取周一到周末 |
| | | public static List<Date> WeeklyDays(Date date){ |
| | | Date nowMonday = WeeklyMonday(date); |
| | | List<Date> dateList = new ArrayList<>(); |
| | | dateList.add(nowMonday); |
| | | for (int i = 1; i<7 ; i++){ |
| | | dateList.add(timeYesterdayN(nowMonday,i)); |
| | | } |
| | | return dateList; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | Date now = new Date(); |
| | | //本周 |
| | | List<Date> dateList = WeeklyMondayAndSundayNow(now); |
| | | for (Date date : dateList){ |
| | | System.out.println("------------------------------"); |
| | | System.out.println(date); |
| | | } |
| | | //上周 |
| | | List<Date> dateList1 = WeeklyMondayAndSundayFront(now); |
| | | for (Date date : dateList1){ |
| | | System.out.println("------------------------------"); |
| | | System.out.println(date); |
| | | } |
| | | //下周 |
| | | List<Date> dateList2 = WeeklyMondayAndSundayAfter(now); |
| | | for (Date date : dateList2){ |
| | | System.out.println("------------------------------"); |
| | | System.out.println(date); |
| | | } |
| | | //周 |
| | | List<Date> dateList3 = WeeklyDays(now); |
| | | for (Date date : dateList3){ |
| | | System.out.println("------------------------------"); |
| | | System.out.println(date); |
| | | } |
| | | } |
| | | |
| | | } |