import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter;import java.time.temporal.TemporalAdjusters; import java.util.Date; /** * @program: pandahero-shop * @description: 时间处理工具类 * @author: Yaoson * @create: 2021-09-08 12:52 **/ public class DateUtils { public static final String PATTERN = "yyyy-MM-dd"; public static final String PATTERN2 = "yyyy-MM-dd HH:mm:ss"; /** * Date 转 String * * @param date * @param pattern * @return */ public static String dateToString(Date date, String pattern) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); return simpleDateFormat.format(date); } /** * String 转 Date * * @param date * @param pattern * @return */ public static Date stringToDate(String date, String pattern) { try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); return simpleDateFormat.parse(date); } catch (Exception e) { e.printStackTrace(); return null; } } /** * LocalDate 转 String * * @param date * @param pattern * @return */ public static String localDateToString(LocalDate date, String pattern) { DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern); return df.format(date); } /** * String 转 LocalDate * * @param date * @return */ public static LocalDate stringToLocalDate(String date) { try { DateTimeFormatter df = DateTimeFormatter.ofPattern(PATTERN); return LocalDate.parse(date, df); } catch (Exception e) { e.printStackTrace(); return null; } } /** * LocalDateTime 转 String * * @param date * @param pattern * @return */ public static String localDateTimeToString(LocalDateTime date, String pattern) { DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern); return df.format(date); } /** * String 转 LocalDateTime * * @param date * @return */ public static LocalDateTime stringToLocalDateTime(String date) { try { DateTimeFormatter df = DateTimeFormatter.ofPattern(PATTERN2); return LocalDateTime.parse(date, df); } catch (Exception e) { e.printStackTrace(); return null; } } /** * Date 转 LocalDate * * @param date * @return */ public static LocalDate dateToLocalDate(Date date) { ZoneId zoneId = ZoneId.systemDefault(); return date.toInstant().atZone(zoneId).toLocalDate(); } /** * LocalDate 转 Date * * @param date * @return */ public static Date localDateToDate(LocalDate date) { ZoneId zoneId = ZoneId.systemDefault(); return Date.from(date.atStartOfDay().atZone(zoneId).toInstant()); } /** * Date 转 LocalDateTime * * @param date * @return */ public static LocalDateTime dateToLocalDateTime(Date date) { ZoneId zoneId = ZoneId.systemDefault(); return date.toInstant().atZone(zoneId).toLocalDateTime(); } /** * LocalDateTime 转 Date * * @param date * @return */ public static Date localDateTimeToDate(LocalDateTime date) { //获取系统默认时区 ZoneId zoneId = ZoneId.systemDefault(); //时区的日期和时间 ZonedDateTime zonedDateTime = date.atZone(zoneId); //获取时刻 return Date.from(zonedDateTime.toInstant()); } /** * LocalDate 转 LocalDateTime * * @param date * @return */ public static LocalDateTime localDateToLocalDateTime(LocalDate date) { return date.atStartOfDay(); } /** * LocalDateTime 转 LocalDate * * @param date * @return */ public static LocalDate localDateTimeToLocalDate(LocalDateTime date) { return date.toLocalDate(); } /** * 当前时间 * * @param pattern * @return */ public static String getNow(String pattern) { return localDateTimeToString(LocalDateTime.now(), pattern); } /** * 时间格式转换 * * @param date * @param pattern * @param pattern2 * @return */ public static String toPattern(String date, String pattern, String pattern2) { Date flagDate = stringToDate(date, pattern); return dateToString(flagDate, pattern2); } /** * 获取指定时间的开始时间,结束时间 * * @param date * @param type 类型:0-天,1-年,2-月,3-周 * @param pattern * @param isStartDate 是否开始时间 * @return */ public static String getStartDateOrEndDate(String date, Integer type, String pattern, Boolean isStartDate) { switch (type) { case 1: return getYearStartDateOrEndDate(date, pattern, isStartDate); case 2: return getMonthStartDateOrEndDate(date, pattern, isStartDate); case 3: return getWeekStartDateOrEndDate(date, pattern, isStartDate); default: return getDayStartDateOrEndDate(date, pattern, isStartDate); } } /** * 获取某天的开始时间,结束时间 * @param date * @param pattern * @param isStartDate * @return */ private static String getDayStartDateOrEndDate(String date, String pattern, Boolean isStartDate) { LocalDateTime flagDate = stringToLocalDateTime(toPattern(date, pattern, PATTERN2)); //某天的开始 if (isStartDate) { LocalDateTime firstDay = flagDate.withHour(0).withMinute(0).withSecond(0); return toPattern(localDateTimeToString(firstDay, PATTERN2), PATTERN2, pattern); } else { //某天结束 LocalDateTime lastDay = flagDate.withHour(23).withMinute(59).withSecond(59); return toPattern(localDateTimeToString(lastDay, PATTERN2), PATTERN2, pattern); } } /** * 获取某星期的第一天,最后一天 * * @param date * @param pattern * @param isStartDate * @return */ private static String getWeekStartDateOrEndDate(String date, String pattern, Boolean isStartDate) { LocalDate flagDate = stringToLocalDate(toPattern(date, pattern, PATTERN)); //某星期的第一天 if (isStartDate) { LocalDate firstDate = flagDate.with(DayOfWeek.MONDAY); return toPattern(localDateToString(firstDate, PATTERN), PATTERN, pattern) + " 00:00:00"; } else { //某星期的最后一天 LocalDate lastDay = flagDate.with(DayOfWeek.MONDAY); return toPattern(localDateToString(lastDay, PATTERN), PATTERN, pattern) + " 23:59:59"; } } /** * 获取某月的第一天,最后一天 * * @param date * @param pattern * @param isStartDate * @return */ private static String getMonthStartDateOrEndDate(String date, String pattern, Boolean isStartDate) { LocalDateTime flagDate = stringToLocalDateTime(toPattern(date, pattern, PATTERN2)); //某年的第一天 if (isStartDate) { LocalDateTime firstDay = flagDate.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0); return toPattern(localDateTimeToString(firstDay, PATTERN2), PATTERN2, pattern); } else { //某年的最后一天 LocalDateTime lastDay = flagDate.with(TemporalAdjusters.firstDayOfMonth()).withHour(23).withMinute(59).withSecond(59); return toPattern(localDateTimeToString(lastDay, PATTERN2), PATTERN2, pattern); } } /** * 获取某年的第一天,最后一天 * * @param date * @param pattern * @param isStartDate * @return */ private static String getYearStartDateOrEndDate(String date, String pattern, Boolean isStartDate) { LocalDateTime flagDate = stringToLocalDateTime(toPattern(date, pattern, PATTERN2)); //某年的第一天 if (isStartDate) { LocalDateTime firstDay = flagDate.with(TemporalAdjusters.firstDayOfYear()).withHour(0).withMinute(0).withSecond(0); return toPattern(localDateTimeToString(firstDay, PATTERN2), PATTERN2, pattern); } else { //某年的最后一天 LocalDateTime lastDay = flagDate.with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59); return toPattern(localDateTimeToString(lastDay, PATTERN2), PATTERN2, pattern); } } /** * 时间计算 * @param date * @param pattern * @param type 类型:0-分钟,1-小时,2-天,3-星期,4-月,5-年 * @return */ public static String calculateDate(String date, String pattern, Integer type, Long number, Boolean isPlus){ switch (type){ case 1: return calculateHourDate(date, pattern, number, isPlus); case 2: return calculateDayDate(date, pattern, number, isPlus); case 3: return calculateWeekDate(date, pattern, number, isPlus); case 4: return calculateMonthDate(date, pattern, number, isPlus); case 5: return calculateYearDate(date, pattern, number, isPlus); default: return calculateMinuteDate(date, pattern, number, isPlus); } } /** * 计算分钟 * @param date * @param pattern * @param number * @param isPlus * @return */ private static String calculateMinuteDate(String date, String pattern, Long number, Boolean isPlus) { LocalDateTime localDateTime = stringToLocalDateTime(toPattern(date, pattern, PATTERN2)); if (isPlus) { localDateTime = localDateTime.plusMinutes(number); }else { localDateTime = localDateTime.minusMinutes(number); } return localDateTimeToString(localDateTime, pattern); } /** * 计算年 * @param date * @param pattern * @param number * @param isPlus * @return */ private static String calculateYearDate(String date, String pattern, Long number, Boolean isPlus) { LocalDateTime localDateTime = stringToLocalDateTime(toPattern(date, pattern, PATTERN2)); if (isPlus) { localDateTime = localDateTime.plusYears(number); }else { localDateTime = localDateTime.minusYears(number); } return localDateTimeToString(localDateTime, pattern); } /** * 计算星期 * @param date * @param pattern * @param number * @param isPlus * @return */ private static String calculateWeekDate(String date, String pattern, Long number, Boolean isPlus) { LocalDateTime localDateTime = stringToLocalDateTime(toPattern(date, pattern, PATTERN2)); if (isPlus) { LocalDateTime newDate = localDateTime.plusWeeks(number); return localDateTimeToString(newDate, pattern); }else { LocalDateTime newDate = localDateTime.minusWeeks(number); return localDateTimeToString(newDate, pattern); } } /** * 计算月 * @param date * @param pattern * @return */ private static String calculateMonthDate(String date, String pattern, Long number, Boolean isPlus) { LocalDateTime localDateTime = stringToLocalDateTime(toPattern(date, pattern, PATTERN2)); if (isPlus) { LocalDateTime newDate = localDateTime.plusMonths(number); return localDateTimeToString(newDate, pattern); }else { LocalDateTime newDate = localDateTime.minusMonths(number); return localDateTimeToString(newDate, pattern); } } /** * 计算天 * @param date * @param pattern * @param number * @param isPlus * @return */ private static String calculateDayDate(String date, String pattern, Long number, Boolean isPlus) { LocalDateTime localDateTime = stringToLocalDateTime(toPattern(date, pattern, PATTERN2)); if (isPlus) { LocalDateTime newDate = localDateTime.plusDays(number); return localDateTimeToString(newDate, pattern); }else { LocalDateTime newDate = localDateTime.minusDays(number); return localDateTimeToString(newDate, pattern); } } /** * 计算小时 * @param date * @param pattern * @param number * @param isPlus * @return */ private static String calculateHourDate(String date, String pattern, Long number, Boolean isPlus) { LocalDateTime localDateTime = stringToLocalDateTime(toPattern(date, pattern, PATTERN2)); if (isPlus) { LocalDateTime newDate = localDateTime.plusHours(number); return localDateTimeToString(newDate, pattern); }else { LocalDateTime newDate = localDateTime.minusHours(number); return localDateTimeToString(newDate, pattern); } } }