|
@@ -0,0 +1,160 @@
|
|
|
+package com.fdkankan.sale.util;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class Dateutils {
|
|
|
+
|
|
|
+ public static int DAY = Calendar.DAY_OF_MONTH;
|
|
|
+ public static int WEEK = Calendar.WEEK_OF_MONTH;
|
|
|
+ public static int MONTH = Calendar.MONTH;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定时间区间的所有数据(包含日期和月份)
|
|
|
+ * @param dBegin
|
|
|
+ * @param dEnd
|
|
|
+ * @param rule 日历规则 如:Calendar.DAY_OF_MONTH
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Date> findDates(Date dBegin, Date dEnd, int rule) {
|
|
|
+ List<Date> lDate = new ArrayList<>();
|
|
|
+ if (dEnd.before(dBegin)){
|
|
|
+ return lDate;
|
|
|
+ }
|
|
|
+ Calendar calBegin = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calBegin.setTime(dBegin);
|
|
|
+ Calendar calEnd = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calEnd.setTime(dEnd);
|
|
|
+ // 测试此日期是否在指定日期之后
|
|
|
+ while (dEnd.after(calBegin.getTime())) {
|
|
|
+ // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
|
|
|
+ Date time = calBegin.getTime();
|
|
|
+ if(rule == Calendar.WEEK_OF_MONTH){
|
|
|
+ time = getMonday(time);
|
|
|
+ }
|
|
|
+ lDate.add(time);
|
|
|
+ calBegin.add(rule, 1);
|
|
|
+ }
|
|
|
+ return lDate;
|
|
|
+ }
|
|
|
+ public static List<String> findDatesStr(Date dBegin, Date dEnd, int rule) {
|
|
|
+ List<String> datesStr = new ArrayList<>();
|
|
|
+ List<Date> dates = findDates(dBegin, dEnd, rule);
|
|
|
+ for (Date date : dates) {
|
|
|
+ String day ;
|
|
|
+ if(rule == MONTH){
|
|
|
+ day =getMonthDate(date);
|
|
|
+ }else {
|
|
|
+ day = getDayDate(date);
|
|
|
+ }
|
|
|
+ datesStr.add(day);
|
|
|
+ }
|
|
|
+ return datesStr;
|
|
|
+ }
|
|
|
+ public static Date getDate(String date) {
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+ public static Date getMonday(Date date) {
|
|
|
+ Calendar calBegin = Calendar.getInstance();
|
|
|
+ calBegin.setTime(date);
|
|
|
+ calBegin.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //周一
|
|
|
+ return calBegin.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDate(Date date) {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
|
|
|
+ }
|
|
|
+ public static String getDateN(Date date) {
|
|
|
+ return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getMonthDate(Date date) {
|
|
|
+ return new SimpleDateFormat("yyyy-MM").format(date);
|
|
|
+ }
|
|
|
+ public static Date getMonthDate(String date) {
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat("yyyy-MM").parse(date);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDayDate(Date date) {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(date);
|
|
|
+ }
|
|
|
+ public static String getDayZeroDate(Date date) {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(date) +" 00:00:00";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getHalfYearStr() {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(getHalfYear())+" 00:00:00";
|
|
|
+ }
|
|
|
+ public static Date getHalfYear() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(new Date());
|
|
|
+ c.add(Calendar.MONTH, -6);
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getLastMonTh(Date date){
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
+ ca.setTime(date);
|
|
|
+ ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(ca.getTime()) +" 23:59:59";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatStartTime(String startTime) {
|
|
|
+ if(StringUtils.isBlank(startTime)){
|
|
|
+ startTime = Dateutils.getHalfYearStr();
|
|
|
+ }
|
|
|
+ if(startTime.length() <12){ //月,开始时间到1号 0点
|
|
|
+ startTime += " 00:00:00";
|
|
|
+ }
|
|
|
+ return startTime;
|
|
|
+ }
|
|
|
+ public static String formatEndTime(String endTime) {
|
|
|
+ if(StringUtils.isBlank(endTime)){
|
|
|
+ endTime = Dateutils.getDate(new Date());
|
|
|
+ }
|
|
|
+ if(endTime.length() <12 ){ //月,开始时间到1号 0点
|
|
|
+ endTime += " 23:59:59";
|
|
|
+ }
|
|
|
+ return endTime;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getStartTime(String startTime) {
|
|
|
+ if(StringUtils.isBlank(startTime)){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if(startTime.length() > 12){
|
|
|
+ return startTime;
|
|
|
+ }
|
|
|
+ return startTime +" 00:00:00";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getEndTime(String endTime) {
|
|
|
+ if(StringUtils.isBlank(endTime)){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if(endTime.length() > 12){
|
|
|
+ return endTime;
|
|
|
+ }
|
|
|
+ return endTime +" 23:59:59";
|
|
|
+ }
|
|
|
+
|
|
|
+}
|