package com.packet.utils; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateUtils; import java.text.ParseException; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; import java.util.Objects; /** * 日期时间工具类 */ @Slf4j public class DateUtil { private static final String patternDateTime = "yyyy-MM-dd HH:mm:ss"; public static final String patternDateTimeMillisecond = "yyyy-MM-dd HH:mm:ss.SSS"; public static final String patternDateTimeOfMonth = "yyyy-MM-dd"; private static final String EXPORT_FORMAT_DATE_TIME = "yyyyMMdd-hhmmss"; public static List<Integer> holidays = Lists.newArrayList(6, 7); public static List<Integer> workdays = Lists.newArrayList(1, 2, 3, 4, 5); /** * java8 LocalDateTime转String * * @param dateTime * @return */ public static String formatDateTime(LocalDateTime dateTime) { if (Objects.isNull(dateTime)) { return ""; } DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patternDateTime); String dateTimeStr = dateTime.format(formatter); return dateTimeStr; } public static String formatDateTimeOfMonth(LocalDateTime dateTime) { if (Objects.isNull(dateTime)) { return ""; } DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patternDateTimeOfMonth); String dateTimeStr = dateTime.format(formatter); return dateTimeStr; } public static String dateTimestamp(LocalDateTime date) { return DateTimeFormatter.ofPattern("yyMMddHHmmssSSS").format(date); } public static String formatDateTime(LocalDateTime dateTime, String pattern) { if (Objects.isNull(dateTime)) { return ""; } DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); String dateTimeStr = dateTime.format(formatter); return dateTimeStr; } public static String formatDateTime(Date dateTime) { if (Objects.nonNull(dateTime)) { return DateFormatUtils.format(dateTime, patternDateTimeOfMonth); } return ""; } public static String formatDateOfDate(Date dateTime) { if (Objects.nonNull(dateTime)) { return DateFormatUtils.format(dateTime, patternDateTime); } return ""; } /** * java8 LocalDateTime转String * * @param dateTime * @return */ public static String exportFormatDateTime(Date dateTime) { if (Objects.nonNull(dateTime)) { return DateFormatUtils.format(dateTime, EXPORT_FORMAT_DATE_TIME); } return ""; } public static String formatDateTimeSimple(Date dateTime) { if (Objects.nonNull(dateTime)) { return DateFormatUtils.format(dateTime, "yyyy-MM-dd"); } return ""; } public static String formatDate(Date dateTime) { if (Objects.nonNull(dateTime)) { return DateFormatUtils.format(dateTime, "yyyy.MM.dd"); } return ""; } public static String formatDateTime(Date dateTime, String pattern) { if (Objects.nonNull(dateTime)) { return DateFormatUtils.format(dateTime, pattern); } return ""; } public static Date strToDate(String string) throws ParseException { Date date = null; date = DateUtils.parseDate(string, "yyyy-MM-dd"); return date; } public static LocalDateTime strToLocalDateTime(String string) throws ParseException { Date date = null; date = DateUtils.parseDate(string, "yyyy-MM-dd HH:mm:ss"); return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } public static LocalDateTime strToLocalDateTimeMillisecond(String string) throws ParseException { Date date = null; date = DateUtils.parseDate(string, patternDateTimeMillisecond); return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } public static Date strToDate3(String string) throws ParseException { Date date = null; date = DateUtils.parseDate(string, patternDateTime); return date; } /** * 传入的开始时间和现在的时间差Duration * * @param beginDate * @return */ public static Duration timeDifferenceToDuration(Date beginDate) { return Duration .between(beginDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(), LocalDateTime.now()); } /** * 传入的开始时间和结束时间时间差Duration * * @param beginDate * @param endDate * @return */ public static Duration timeDifferenceToDuration(Date beginDate, Date endDate) { return Duration.between(beginDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(), endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()); } /** * 获得某天最大时间 2019-10-15 23:59:59 */ public static Date getEndOfDay(Date date) { LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); ; LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX); return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant()); } /** * 获得某天最小时间 2019-10-15 00:00:00 */ public static Date getStartOfDay(Date date) { LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN); return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()); } public static Date localDateTimeToDate(LocalDateTime localDateTime) { return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } /** * 通过时间秒毫秒数判断两个时间的间隔(天) * * @param date1 开始时间 * @param date2 截止时间 * @return 返回天数 */ public static int differenceBetween(Date date1, Date date2) { int days = (int)(((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24))); return days; } public static void main(String[] args) { // String begin = "2020-09-11 11:00:00"; // String end = "2020-09-19 17:57:00"; // Duration duration = timeDifferenceToDuration(DateUtil.strToDate3(begin), DateUtil.strToDate3(end)); // System.out.println(duration.toDays()); System.out.println(exportFormatDateTime(new Date())); LocalDateTime removeLogTime = LocalDateTime.now().plusDays(-100); System.out.println(removeLogTime); } }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118754.html