package com.repair.common.utils; import java.text.SimpleDateFormat; import java.time.Duration; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; public class DateUtils { // 计算时差 public static String difference(Date start, Date end) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time1 = sdf.format(start); String time2 = sdf.format(end); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dateTime1 = LocalDateTime.parse(time1, formatter); LocalDateTime dateTime2 = LocalDateTime.parse(time2, formatter); Duration duration = Duration.between(dateTime1, dateTime2); long days = duration.toDays(); long hours = duration.toHours() % 24; long minutes = duration.toMinutes() % 60; long seconds = duration.getSeconds() % 60; // System.out.println("时间差为:" + days + "天 " + hours + "小时 " + minutes + "分钟 " + seconds + "秒"); return days + "天 " + hours + "小时 " + minutes + "分钟 " + seconds + "秒"; } public static String startTime(Integer state) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime now = LocalDateTime.now(); switch (state) { // 本年 case 1: LocalDateTime localDateTime = now.minusYears(1); return localDateTime.format(dateTimeFormatter); // 本月 case 2: LocalDateTime localDateTime1 = now.minusMonths(1); return localDateTime1.format(dateTimeFormatter); // 本周 case 3: LocalDateTime localDateTime2 = now.minusWeeks(1); return localDateTime2.format(dateTimeFormatter); // 今天 case 4: LocalDateTime localDateTime3 = now.minusDays(1); return localDateTime3.format(dateTimeFormatter); } return null; } public static String endTime(){ DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); return LocalDateTime.now().format(dateTimeFormatter); } public static String trendStartTime(Integer state){ DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime now = LocalDateTime.now(); now=now.withHour(0).withMinute(0).withSecond(0); switch (state) { // 本年 case 1: LocalDateTime localDateTime = now.withDayOfYear(1); return localDateTime.format(dateTimeFormatter); // 本月 case 2: LocalDateTime localDateTime1 = now.withDayOfMonth(1); return localDateTime1.format(dateTimeFormatter); // 本周 case 3: int value = now.getDayOfWeek().getValue(); LocalDateTime localDateTime2 = now.minusDays(value-1); return localDateTime2.format(dateTimeFormatter); // 今天 case 4: ; return now.format(dateTimeFormatter); } return null; } /** * * @param state * @return */ public static String trendComparisonStartTime(Integer state) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime now = LocalDateTime.now(); now=now.withHour(0).withMinute(0).withSecond(0); switch (state) { // 去年 case 1: LocalDateTime localDateTime = now.minusYears(1).withDayOfYear(1); return localDateTime.format(dateTimeFormatter); // 上月 case 2: LocalDateTime localDateTime1 = now.minusMonths(1).withDayOfMonth(1); return localDateTime1.format(dateTimeFormatter); // 上周 case 3: int value = now.getDayOfWeek().getValue(); LocalDateTime localDateTime2 = now.minusWeeks(1).minusDays(value-1); return localDateTime2.format(dateTimeFormatter); // 昨天 case 4: LocalDateTime localDateTime3 = now.minusDays(1); return localDateTime3.format(dateTimeFormatter); } return null; } public static String weekName(int weekState){ switch (weekState) { case 1: return "星期一"; case 2: return "星期二"; case 3: return "星期三"; case 4: return "星期四"; case 5: return "星期五"; case 6: return "星期六"; case 7: return "星期日"; } return null; } public static void main(String[] args) { String s = trendStartTime(4); System.out.println("s = " + s); String s1 = trendComparisonStartTime(4); System.out.println("s1 = " + s1); } }