DateTimeUtil.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package cn.com.lzt.common.util;
  2. import org.apache.commons.lang3.StringUtils;
  3. import java.text.SimpleDateFormat;
  4. import java.time.LocalDate;
  5. import java.time.LocalDateTime;
  6. import java.time.ZoneOffset;
  7. import java.time.format.DateTimeFormatter;
  8. import java.time.temporal.ChronoUnit;
  9. import java.util.Date;
  10. import java.util.List;
  11. import java.util.stream.Collectors;
  12. import java.util.stream.Stream;
  13. public class DateTimeUtil {
  14. // 各种时间格式
  15. public static final DateTimeFormatter dtf_yyyy_MM_dd = DateTimeFormatter.ofPattern(
  16. "yyyy-MM-dd");
  17. // 各种时间格式
  18. public static final DateTimeFormatter dtf_yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
  19. // 各种时间格式
  20. public static final DateTimeFormatter dtf_dateChinese = DateTimeFormatter.ofPattern(
  21. "yyyy年MM月dd日");
  22. public static final DateTimeFormatter dtf_yyyy_MM_dd_HH_mm = DateTimeFormatter.ofPattern(
  23. "yyyy-MM-dd HH:mm");
  24. public static final DateTimeFormatter dtf_yyyymmddhhmmss = DateTimeFormatter.ofPattern(
  25. "yyyyMMddHHmmss");
  26. public static final DateTimeFormatter dtf_HH_mm = DateTimeFormatter.ofPattern(
  27. "HH:mm");
  28. public static final DateTimeFormatter dtf_yyyy_MM_dd_HH_mm_SS = DateTimeFormatter.ofPattern(
  29. "yyyy-MM-dd HH:mm:ss");
  30. /**
  31. * 日期转换为时间戳秒
  32. * @param localDateTime
  33. * @return
  34. */
  35. public static long toSeconds(LocalDateTime localDateTime) {
  36. return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
  37. }
  38. public static String formatDateTime(LocalDateTime lastDateTime) {
  39. return lastDateTime.format(dtf_yyyy_MM_dd_HH_mm_SS);
  40. }
  41. public static String formatDateTime(Date date) {
  42. if(date == null) return "";
  43. SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyy-MM-dd HH:mm:ss ms");
  44. return simpleDateFormat.format(date);
  45. }
  46. public static String formatDateTime(Object dateTime, String pattern) {
  47. if(StringUtils.isEmpty(pattern)) pattern = "yyy-MM-dd HH:mm:ss";
  48. if(dateTime instanceof Date) return formatDateTime((Date)dateTime, pattern);
  49. else if(dateTime instanceof LocalDateTime) return formatDateTime((LocalDateTime)dateTime, pattern);
  50. return "";
  51. }
  52. public static String formatDateTime(Date date, String pattern) {
  53. if(date == null) return "";
  54. SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern);
  55. return simpleDateFormat.format(date);
  56. }
  57. public static String formatDateTime(LocalDateTime dateTime, String pattern) {
  58. if(dateTime == null) return "";
  59. return dateTime.format(DateTimeFormatter.ofPattern(pattern));
  60. }
  61. public static String formatDateTime(LocalDateTime dateTime, DateTimeFormatter dateTimeFormatter) {
  62. if(dateTime == null) return "";
  63. return dateTime.format(dateTimeFormatter);
  64. }
  65. /**
  66. * 时间戳秒转换为日期
  67. * @param seconds
  68. * @return
  69. */
  70. public static LocalDateTime fromSeconds(long seconds) {
  71. return LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.ofHours(8));
  72. }
  73. public static LocalDateTime fromMillis(long milliSeconds) {
  74. return fromSeconds(milliSeconds * 1000);
  75. }
  76. public static LocalDate parseLocalDate(String date) {
  77. return LocalDate.parse(date, dtf_yyyy_MM_dd);
  78. }
  79. public static String formatLocalDate(LocalDate localDate) {
  80. return DateTimeUtil.dtf_yyyy_MM_dd.format(localDate);
  81. }
  82. /**
  83. * 两日期之间的天数
  84. * @param startDate
  85. * @param endDate
  86. * @return
  87. */
  88. public static long getDiffDaysCount(LocalDate startDate, LocalDate endDate, boolean includeEndDate) {
  89. long num = ChronoUnit.DAYS.between(startDate, endDate);
  90. if(includeEndDate) return num + 1;
  91. else return num;
  92. }
  93. public static List<LocalDate> getDateList(LocalDate startDate, LocalDate endDate, boolean includeEndDate) {
  94. long numOfDays = DateTimeUtil.getDiffDaysCount(startDate, endDate, includeEndDate);
  95. List<LocalDate> listOfDates1 = Stream.iterate(startDate, date -> date.plusDays(1))
  96. .limit(numOfDays)
  97. .collect(Collectors.toList());
  98. return listOfDates1;
  99. }
  100. }