Dateutils.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.fdkankan.tk.util;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUnit;
  4. import cn.hutool.core.date.DateUtil;
  5. import org.apache.commons.lang3.StringUtils;
  6. import java.text.SimpleDateFormat;
  7. import java.time.Duration;
  8. import java.util.ArrayList;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.List;
  12. public class Dateutils {
  13. public static int DAY = Calendar.DAY_OF_MONTH;
  14. public static int WEEK = Calendar.WEEK_OF_MONTH;
  15. public static int MONTH = Calendar.MONTH;
  16. /**
  17. * 获取指定时间区间的所有数据(包含日期和月份)
  18. * @param dBegin
  19. * @param dEnd
  20. * @param rule 日历规则 如:Calendar.DAY_OF_MONTH
  21. * @return
  22. */
  23. public static List<Date> findDates(Date dBegin, Date dEnd, int rule) {
  24. List<Date> lDate = new ArrayList<>();
  25. if (dEnd.before(dBegin)){
  26. return lDate;
  27. }
  28. Calendar calBegin = Calendar.getInstance();
  29. // 使用给定的 Date 设置此 Calendar 的时间
  30. calBegin.setTime(dBegin);
  31. Calendar calEnd = Calendar.getInstance();
  32. // 使用给定的 Date 设置此 Calendar 的时间
  33. calEnd.setTime(dEnd);
  34. // 测试此日期是否在指定日期之后
  35. while (dEnd.after(calBegin.getTime())) {
  36. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  37. Date time = calBegin.getTime();
  38. if(rule == Calendar.WEEK_OF_MONTH){
  39. time = getMonday(time);
  40. }
  41. lDate.add(time);
  42. calBegin.add(rule, 1);
  43. }
  44. return lDate;
  45. }
  46. public static List<String> findDatesStr(Date dBegin, Date dEnd, int rule) {
  47. List<String> datesStr = new ArrayList<>();
  48. List<Date> dates = findDates(dBegin, dEnd, rule);
  49. for (Date date : dates) {
  50. String day ;
  51. if(rule == MONTH){
  52. day =getMonthDate(date);
  53. }else {
  54. day = getDayDate(date);
  55. }
  56. datesStr.add(day);
  57. }
  58. return datesStr;
  59. }
  60. public static Date getDate(String date) {
  61. try {
  62. return new SimpleDateFormat("yyyy-MM-dd").parse(date);
  63. }catch (Exception e){
  64. e.printStackTrace();
  65. }
  66. return new Date();
  67. }
  68. public static Date getDateTime(String date) {
  69. try {
  70. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
  71. }catch (Exception e){
  72. e.printStackTrace();
  73. }
  74. return new Date();
  75. }
  76. public static Date getMonday(Date date) {
  77. Calendar calBegin = Calendar.getInstance();
  78. calBegin.setTime(date);
  79. calBegin.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //周一
  80. return calBegin.getTime();
  81. }
  82. public static String getDate(Date date) {
  83. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
  84. }
  85. public static String getDateDay(Date date) {
  86. return new SimpleDateFormat("yyyy-MM-dd").format(date);
  87. }
  88. public static String getDateN(Date date) {
  89. return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
  90. }
  91. public static String getMonthDate(Date date) {
  92. return new SimpleDateFormat("yyyy-MM").format(date);
  93. }
  94. public static Date getMonthDate(String date) {
  95. try {
  96. return new SimpleDateFormat("yyyy-MM").parse(date);
  97. }catch (Exception e){
  98. e.printStackTrace();
  99. }
  100. return new Date();
  101. }
  102. public static String getDayDate(Date date) {
  103. return new SimpleDateFormat("yyyy-MM-dd").format(date);
  104. }
  105. public static String getDayZeroDate(Date date) {
  106. return new SimpleDateFormat("yyyy-MM-dd").format(date) +" 00:00:00";
  107. }
  108. public static String getHalfYearStr() {
  109. return new SimpleDateFormat("yyyy-MM-dd").format(getHalfYear())+" 00:00:00";
  110. }
  111. public static String getHalfYearStrDay() {
  112. return new SimpleDateFormat("yyyy-MM-dd").format(getHalfYear());
  113. }
  114. public static Date getHalfYear() {
  115. Calendar c = Calendar.getInstance();
  116. c.setTime(new Date());
  117. c.add(Calendar.MONTH, -6);
  118. return c.getTime();
  119. }
  120. public static String getLastMonTh(Date date){
  121. Calendar ca = Calendar.getInstance();
  122. ca.setTime(date);
  123. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  124. return new SimpleDateFormat("yyyy-MM-dd").format(ca.getTime()) +" 23:59:59";
  125. }
  126. public static String formatStartTime(String startTime) {
  127. if(StringUtils.isBlank(startTime)){
  128. startTime = Dateutils.getHalfYearStr();
  129. }
  130. return startTime;
  131. }
  132. public static String formatEndTime(String endTime) {
  133. if(StringUtils.isBlank(endTime)){
  134. endTime = Dateutils.getDate(new Date());
  135. }
  136. return endTime;
  137. }
  138. public static String formatStartTimeDay(String startTime) {
  139. if(StringUtils.isBlank(startTime)){
  140. return Dateutils.getHalfYearStrDay();
  141. }
  142. Date date = getDateTime(startTime);
  143. return Dateutils.getDateDay(date);
  144. }
  145. public static String formatEndTimeDay(String endTime) {
  146. if(StringUtils.isBlank(endTime)){
  147. return Dateutils.getDateDay(new Date());
  148. }
  149. Date date = getDateTime(endTime);
  150. return Dateutils.getDateDay(date);
  151. }
  152. public static Long getLongTime(Date useStartTime, Date useEndTime,DateUnit dateUnit) {
  153. if(useStartTime == null || useEndTime == null){
  154. return 0L;
  155. }
  156. return DateUtil.between(useStartTime, useEndTime, dateUnit);
  157. }
  158. }