package cn.com.lzt.common.util; import org.apache.commons.lang3.StringUtils; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class DateTimeUtil { // 各种时间格式 public static final DateTimeFormatter dtf_yyyy_MM_dd = DateTimeFormatter.ofPattern( "yyyy-MM-dd"); // 各种时间格式 public static final DateTimeFormatter dtf_yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd"); // 各种时间格式 public static final DateTimeFormatter dtf_dateChinese = DateTimeFormatter.ofPattern( "yyyy年MM月dd日"); public static final DateTimeFormatter dtf_yyyy_MM_dd_HH_mm = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm"); public static final DateTimeFormatter dtf_yyyymmddhhmmss = DateTimeFormatter.ofPattern( "yyyyMMddHHmmss"); public static final DateTimeFormatter dtf_HH_mm = DateTimeFormatter.ofPattern( "HH:mm"); public static final DateTimeFormatter dtf_yyyy_MM_dd_HH_mm_SS = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss"); /** * 日期转换为时间戳秒 * @param localDateTime * @return */ public static long toSeconds(LocalDateTime localDateTime) { return localDateTime.toEpochSecond(ZoneOffset.of("+8")); } public static String formatDateTime(LocalDateTime lastDateTime) { return lastDateTime.format(dtf_yyyy_MM_dd_HH_mm_SS); } public static String formatDateTime(Date date) { if(date == null) return ""; SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyy-MM-dd HH:mm:ss ms"); return simpleDateFormat.format(date); } public static String formatDateTime(Object dateTime, String pattern) { if(StringUtils.isEmpty(pattern)) pattern = "yyy-MM-dd HH:mm:ss"; if(dateTime instanceof Date) return formatDateTime((Date)dateTime, pattern); else if(dateTime instanceof LocalDateTime) return formatDateTime((LocalDateTime)dateTime, pattern); return ""; } public static String formatDateTime(Date date, String pattern) { if(date == null) return ""; SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern); return simpleDateFormat.format(date); } public static String formatDateTime(LocalDateTime dateTime, String pattern) { if(dateTime == null) return ""; return dateTime.format(DateTimeFormatter.ofPattern(pattern)); } public static String formatDateTime(LocalDateTime dateTime, DateTimeFormatter dateTimeFormatter) { if(dateTime == null) return ""; return dateTime.format(dateTimeFormatter); } /** * 时间戳秒转换为日期 * @param seconds * @return */ public static LocalDateTime fromSeconds(long seconds) { return LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.ofHours(8)); } public static LocalDateTime fromMillis(long milliSeconds) { return fromSeconds(milliSeconds * 1000); } public static LocalDate parseLocalDate(String date) { return LocalDate.parse(date, dtf_yyyy_MM_dd); } public static String formatLocalDate(LocalDate localDate) { return DateTimeUtil.dtf_yyyy_MM_dd.format(localDate); } /** * 两日期之间的天数 * @param startDate * @param endDate * @return */ public static long getDiffDaysCount(LocalDate startDate, LocalDate endDate, boolean includeEndDate) { long num = ChronoUnit.DAYS.between(startDate, endDate); if(includeEndDate) return num + 1; else return num; } public static List getDateList(LocalDate startDate, LocalDate endDate, boolean includeEndDate) { long numOfDays = DateTimeUtil.getDiffDaysCount(startDate, endDate, includeEndDate); List listOfDates1 = Stream.iterate(startDate, date -> date.plusDays(1)) .limit(numOfDays) .collect(Collectors.toList()); return listOfDates1; } }