DateUtil.java 6.5 KB

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