Dateutils.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package com.fdkankan.manage.util;
  2. import org.apache.commons.lang3.StringUtils;
  3. import java.text.SimpleDateFormat;
  4. import java.time.LocalDate;
  5. import java.time.format.DateTimeFormatter;
  6. import java.time.temporal.TemporalAdjusters;
  7. import java.util.*;
  8. public class Dateutils {
  9. public static int DAY = Calendar.DAY_OF_MONTH;
  10. public static int WEEK = Calendar.WEEK_OF_MONTH;
  11. public static int MONTH = Calendar.MONTH;
  12. public static String orderSn = "yyyyMMddHHmmssSSS";
  13. /**
  14. * 获取指定时间区间的所有数据(包含日期和月份)
  15. * @param dBegin
  16. * @param dEnd
  17. * @param rule 日历规则 如:Calendar.DAY_OF_MONTH
  18. * @return
  19. */
  20. public static List<Date> findDates(Date dBegin, Date dEnd, int rule) {
  21. List<Date> lDate = new ArrayList<>();
  22. if (dEnd.before(dBegin)){
  23. return lDate;
  24. }
  25. Calendar calBegin = Calendar.getInstance();
  26. // 使用给定的 Date 设置此 Calendar 的时间
  27. calBegin.setTime(dBegin);
  28. Calendar calEnd = Calendar.getInstance();
  29. // 使用给定的 Date 设置此 Calendar 的时间
  30. calEnd.setTime(dEnd);
  31. // 测试此日期是否在指定日期之后
  32. while (dEnd.after(calBegin.getTime())) {
  33. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  34. Date time = calBegin.getTime();
  35. if(rule == Calendar.WEEK_OF_MONTH){
  36. time = getMonday(time);
  37. }
  38. lDate.add(time);
  39. calBegin.add(rule, 1);
  40. }
  41. return lDate;
  42. }
  43. public static List<String> findDatesStr(Date dBegin, Date dEnd, int rule) {
  44. List<String> datesStr = new ArrayList<>();
  45. List<Date> dates = findDates(dBegin, dEnd, rule);
  46. for (Date date : dates) {
  47. String day ;
  48. if(rule == MONTH){
  49. day =getMonthDate(date);
  50. }else {
  51. day = getDayDate(date);
  52. }
  53. datesStr.add(day);
  54. }
  55. return datesStr;
  56. }
  57. public static Date getDate(String date) {
  58. try {
  59. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
  60. }catch (Exception e){
  61. e.printStackTrace();
  62. }
  63. return new Date();
  64. }
  65. public static Date getMonday(Date date) {
  66. Calendar calBegin = Calendar.getInstance();
  67. calBegin.setTime(date);
  68. calBegin.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //周一
  69. return calBegin.getTime();
  70. }
  71. public static String getDate(Date date) {
  72. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
  73. }
  74. public static String getDateN(Date date) {
  75. return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
  76. }
  77. public static String getMonthDate(Date date) {
  78. return new SimpleDateFormat("yyyy-MM").format(date);
  79. }
  80. public static Date getMonthDate(String date) {
  81. try {
  82. return new SimpleDateFormat("yyyy-MM").parse(date);
  83. }catch (Exception e){
  84. e.printStackTrace();
  85. }
  86. return new Date();
  87. }
  88. public static String getDayDate(Date date) {
  89. return new SimpleDateFormat("yyyy-MM-dd").format(date);
  90. }
  91. public static String getDayZeroDate(Date date) {
  92. return new SimpleDateFormat("yyyy-MM-dd").format(date) +" 00:00:00";
  93. }
  94. public static String getHalfYearStr() {
  95. return new SimpleDateFormat("yyyy-MM-dd").format(getHalfYear())+" 00:00:00";
  96. }
  97. public static Date getHalfYear() {
  98. Calendar c = Calendar.getInstance();
  99. c.setTime(new Date());
  100. c.add(Calendar.MONTH, -6);
  101. return c.getTime();
  102. }
  103. public static String getLastMonTh(Date date){
  104. Calendar ca = Calendar.getInstance();
  105. ca.setTime(date);
  106. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  107. return new SimpleDateFormat("yyyy-MM-dd").format(ca.getTime()) +" 23:59:59";
  108. }
  109. public static String formatStartTime(String startTime) {
  110. if(StringUtils.isBlank(startTime)){
  111. startTime = Dateutils.getHalfYearStr();
  112. }
  113. if(startTime.length() <12){ //月,开始时间到1号 0点
  114. startTime += " 00:00:00";
  115. }
  116. return startTime;
  117. }
  118. public static String formatEndTime(String endTime) {
  119. if(StringUtils.isBlank(endTime)){
  120. endTime = Dateutils.getDate(new Date());
  121. }
  122. if(endTime.length() <12 ){ //月,开始时间到1号 0点
  123. endTime += " 23:59:59";
  124. }
  125. return endTime;
  126. }
  127. public static String getStartTime(String startTime) {
  128. if(StringUtils.isBlank(startTime)){
  129. return null;
  130. }
  131. if(startTime.length() > 12){
  132. return startTime;
  133. }
  134. return startTime +" 00:00:00";
  135. }
  136. public static String getEndTime(String endTime) {
  137. if(StringUtils.isBlank(endTime)){
  138. return null;
  139. }
  140. if(endTime.length() > 12){
  141. return endTime;
  142. }
  143. return endTime +" 23:59:59";
  144. }
  145. public static final String DEF_FMT = "yyyy-MM-dd HH:mm:ss";
  146. public static String AddMinute(String date, Integer minute){
  147. SimpleDateFormat sdf = new SimpleDateFormat(DEF_FMT);
  148. try {
  149. Date time = sdf.parse(date);
  150. return AddMinute(time,minute);
  151. }catch (Exception e){
  152. e.printStackTrace();
  153. }
  154. return date;
  155. }
  156. public static String AddMinute(Date date, Integer minute){
  157. SimpleDateFormat sdf = new SimpleDateFormat(DEF_FMT);
  158. Calendar nowTime = Calendar.getInstance();
  159. nowTime.setTime(date);
  160. nowTime.add(Calendar.MINUTE, ~minute);
  161. return sdf.format(nowTime.getTime());
  162. }
  163. public synchronized static String getOrderSn() {
  164. try {
  165. Thread.sleep(1L);
  166. }catch (Exception e){
  167. }
  168. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(orderSn);
  169. Date date = new Date();
  170. return simpleDateFormat.format(date);
  171. }
  172. /*日期加+1年*/
  173. public static Date dateAddOneYear(Date date,Integer i) {
  174. Calendar calendar = new GregorianCalendar();
  175. calendar.setTime(date);
  176. calendar.add(Calendar.YEAR,i); //把日期往后增加一天,整数 往后推,负数往前移动
  177. date=calendar.getTime(); //这个时间就是日期往后推一天的结果
  178. return date;
  179. }
  180. /*日期加+1月*/
  181. public static Date dateAddOneMonth(Date date,Integer i) {
  182. Calendar calendar = new GregorianCalendar();
  183. calendar.setTime(date);
  184. calendar.add(Calendar.MONTH,i); //把日期往后增加一天,整数 往后推,负数往前移动
  185. date=calendar.getTime(); //这个时间就是日期往后推一天的结果
  186. return date;
  187. }
  188. /*日期加+1天*/
  189. public static Date dateAddOne(Date date,Integer i) {
  190. Calendar calendar = new GregorianCalendar();
  191. calendar.setTime(date);
  192. calendar.add(Calendar.DATE,i); //把日期往后增加一天,整数 往后推,负数往前移动
  193. date=calendar.getTime(); //这个时间就是日期往后推一天的结果
  194. return date;
  195. }
  196. }