From 2b140bd180f078bc90560c8c8eac7ea9445144f3 Mon Sep 17 00:00:00 2001
From: lld <15027638633@163.com>
Date: Fri, 6 Mar 2026 03:17:31 +0800
Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8C=96=E4=BB=BB=E5=8A=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../agri/common/enums/TempCommandStatus.java | 32 +
.../com/agri/common/utils/DateTimeUtils.java | 2181 +++++++++++++++++
.../common/utils/RollerTimeCalculator.java | 55 +
.../com/agri/common/utils/TempJudgeUtil.java | 42 +
.../agri/common/utils/TimeConvertUtil.java | 41 +
.../com/agri/common/utils/TimeRangeUtil.java | 46 +
.../interceptor/DeviceStatusHandler.java | 12 +-
agri-quartz/pom.xml | 6 +-
.../com/agri/quartz/task/RollerAutoTask.java | 112 +
.../com/agri/system/domain/SysDtuData.java | 61 +-
.../com/agri/system/domain/SysRollerAir.java | 19 +-
.../agri/system/domain/SysRollerParam.java | 104 +-
.../agri/system/domain/vo/AgriAutoInfoVo.java | 49 +
.../agri/system/domain/vo/RollerTermVO.java | 32 +
.../agri/system/mapper/SysAgriInfoMapper.java | 16 +-
.../system/mapper/SysRollerParamMapper.java | 24 +-
.../system/service/ISysAgriInfoService.java | 17 +-
.../system/service/ISysDtuDataService.java | 23 +-
.../service/ISysRollerParamService.java | 23 +-
.../service/impl/SysAgriInfoServiceImpl.java | 22 +-
.../service/impl/SysDtuDataServiceImpl.java | 61 +-
.../impl/SysRollerParamServiceImpl.java | 33 +-
.../mapper/system/SysAgriInfoMapper.xml | 22 +
.../{control => system}/SysAutoTermMapper.xml | 0
.../{assets => system}/SysRollerAirMapper.xml | 0
.../SysRollerParamMapper.xml | 34 +-
26 files changed, 2842 insertions(+), 225 deletions(-)
create mode 100644 agri-common/src/main/java/com/agri/common/enums/TempCommandStatus.java
create mode 100644 agri-common/src/main/java/com/agri/common/utils/DateTimeUtils.java
create mode 100644 agri-common/src/main/java/com/agri/common/utils/RollerTimeCalculator.java
create mode 100644 agri-common/src/main/java/com/agri/common/utils/TempJudgeUtil.java
create mode 100644 agri-common/src/main/java/com/agri/common/utils/TimeConvertUtil.java
create mode 100644 agri-common/src/main/java/com/agri/common/utils/TimeRangeUtil.java
create mode 100644 agri-system/src/main/java/com/agri/system/domain/vo/AgriAutoInfoVo.java
create mode 100644 agri-system/src/main/java/com/agri/system/domain/vo/RollerTermVO.java
rename agri-system/src/main/resources/mapper/{control => system}/SysAutoTermMapper.xml (100%)
rename agri-system/src/main/resources/mapper/{assets => system}/SysRollerAirMapper.xml (100%)
rename agri-system/src/main/resources/mapper/{control => system}/SysRollerParamMapper.xml (85%)
diff --git a/agri-common/src/main/java/com/agri/common/enums/TempCommandStatus.java b/agri-common/src/main/java/com/agri/common/enums/TempCommandStatus.java
new file mode 100644
index 0000000..f70d5c0
--- /dev/null
+++ b/agri-common/src/main/java/com/agri/common/enums/TempCommandStatus.java
@@ -0,0 +1,32 @@
+package com.agri.common.enums;
+
+/**
+ * @ClassName TempCommandStatus
+ * @Description TODO
+ * @Author lld
+ * @Date 2026/3/6 2:46
+ * @Version 1.0
+ */
+public enum TempCommandStatus {
+ OPEN(1, "下发开指令"),
+ CLOSE(2, "下发关指令"),
+ NO_OPERATE(0, "不操作");
+
+ private final int code;
+ private final String desc;
+
+ TempCommandStatus(int code, String desc) {
+ this.code = code;
+ this.desc = desc;
+ }
+
+
+ public int getCode() {
+ return code;
+ }
+
+ public String getDesc() {
+ return desc;
+ }
+}
+// 使用:status.getCode() → 1/2/0
diff --git a/agri-common/src/main/java/com/agri/common/utils/DateTimeUtils.java b/agri-common/src/main/java/com/agri/common/utils/DateTimeUtils.java
new file mode 100644
index 0000000..84317aa
--- /dev/null
+++ b/agri-common/src/main/java/com/agri/common/utils/DateTimeUtils.java
@@ -0,0 +1,2181 @@
+package com.agri.common.utils;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.math.RoundingMode;
+import java.sql.Timestamp;
+import java.text.DateFormat;
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+
+/**
+ *
+ * @Title: DateTimeUtils.java
+ * _日期工具类
+ * @author hanwei
+ * @date 2019年3月25日 下午2:42:38
+ * @version V1.0
+ * @Copyright (c) 2022 SGai
+ */
+public class DateTimeUtils {
+
+ // 定义时间日期显示格式
+ private final static String DATE_FORMAT = "yyyy-MM-dd";
+
+ private final static String DATE_FORMAT_CN = "yyyy年MM月dd日";
+
+ private final static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
+
+ private final static String TIME_FORMAT_CN = "yyyy年MM月dd日 HH:mm:ss";
+
+ private final static String MONTH_FORMAT = "yyyy-MM";
+
+ private final static String DATE_FORMAT_HOUR = "HH";
+
+ private final static String YEAR_FORMAT = "yyyy-MM";
+
+ private final static String DAY_FORMAT = "yyyyMMdd";
+
+ public final static int ONE_DAY_SECOND = 86400;
+
+ // private final static String TIME_FORMAT_MILLI = "yyyy-MM-dd HH:mm:ss.SSS";
+
+
+ /************ hanwei begin **************/
+ /**
+ * 取得当前系统时间,返回java.util.Date类型
+ *
+ * @see java.util.Date
+ * @return java.util.Date 返回服务器当前系统时间
+ */
+ public static java.util.Date getCurrDate() {
+ return new java.util.Date();
+ }
+
+ /**
+ * 取得当前系统时间戳
+ *
+ * @see java.sql.Timestamp
+ * @return java.sql.Timestamp 系统时间戳
+ */
+ public static java.sql.Timestamp getCurrTimestamp() {
+ return new java.sql.Timestamp(System.currentTimeMillis());
+ }
+
+ /**
+ *
+ * @Description: 获取时间戳long
+ * @return
+ * @throws
+ * @Author:hanwei
+ * @CreateDtae :2017年2月9日 下午2:57:40
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Long getCurrentTimeMillis(){
+ return System.currentTimeMillis();
+ }
+
+
+ /**
+ * 得到格式化后的日期,格式为yyyy-MM-dd,如2006-02-15
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see #getFormatDate(java.util.Date, String)
+ * @return String 返回格式化后的日期,默认格式为为yyyy-MM-dd,如2006-02-15
+ */
+ public static String getFormatDate(java.util.Date currDate) {
+ return getFormatDate(currDate, DATE_FORMAT);
+ }
+ /**
+ * 得到格式化后的日期,格式为yyyy-MM-dd,如2006-02-15
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see #getFormatDate(String, String)
+ * @return Date 返回格式化后的日期,默认格式为yyyy-MM-dd,如2006-02-15d
+ */
+ public static Date getFormatAllDate(String currDate) {
+
+ Date date = getFormatDate(currDate, DATE_FORMAT);
+ if(null != date) {
+ return date;
+ }
+ date = getFormatDate(currDate, MONTH_FORMAT);
+ if(null != date) {
+ return date;
+ }
+ date = getFormatDate(currDate, YEAR_FORMAT);
+ return date;
+ }
+
+ /**
+ * 得到格式化后的日期,格式为yyyy-MM-dd,如2006-02-15
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see #getFormatDate(java.util.Date)
+ * @return Date 返回格式化后的日期,默认格式为为yyyy-MM-dd,如2006-02-15
+ */
+ public static Date getFormatDateToDate(java.util.Date currDate) {
+ return getFormatDate(getFormatDate(currDate));
+ }
+
+ /**
+ * 得到格式化后的日期,格式为yyyy年MM月dd日,如2006年02月15日
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see #getFormatDate(java.util.Date, String)
+ * @return String 返回格式化后的日期,默认格式为yyyy年MM月dd日,如2006年02月15日
+ */
+ public static String getFormatDate_CN(java.util.Date currDate) {
+ return getFormatDate(currDate, DATE_FORMAT_CN);
+ }
+
+ /**
+ * 得到格式化后的日期,格式为yyyy年MM月dd日,如2006年02月15日
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see #getFormatDate_CN(String)
+ * @return Date 返回格式化后的日期,默认格式为yyyy年MM月dd日,如2006年02月15日
+ */
+ public static Date getFormatDateToDate_CN(java.util.Date currDate) {
+ return getFormatDate_CN(getFormatDate_CN(currDate));
+ }
+
+ /**
+ * 得到格式化后的日期,格式为yyyy-MM-dd,如2006-02-15
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see #getFormatDate(String, String)
+ * @return Date 返回格式化后的日期,默认格式为yyyy-MM-dd,如2006-02-15d
+ */
+ public static Date getFormatDate(String currDate) {
+ return getFormatDate(currDate, DATE_FORMAT);
+ }
+
+ public static String getFormatDateHour(Date currDate) {
+ return getFormatDate(currDate, DATE_FORMAT_HOUR);
+ }
+ /**
+ * 得到格式化后的日期,格式为yyyy年MM月dd日,如2006年02月15日
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see #getFormatDate(String, String)
+ * @return 返回格式化后的日期,默认格式为yyyy年MM月dd日,如2006年02月15日
+ */
+ public static Date getFormatDate_CN(String currDate) {
+ return getFormatDate(currDate, DATE_FORMAT_CN);
+ }
+
+ /**
+ * 根据格式得到格式化后的日期
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @param format
+ * 日期格式,如yyyy-MM-dd
+ * @see java.text.SimpleDateFormat#format(java.util.Date)
+ * @return String 返回格式化后的日期,格式由参数format定义,如yyyy-MM-dd,如2006-02-15
+ */
+ public static String getFormatDate(java.util.Date currDate, String format) {
+ SimpleDateFormat dtFormatdB = null;
+ try {
+ dtFormatdB = new SimpleDateFormat(format);
+ return dtFormatdB.format(currDate);
+ } catch (Exception e) {
+ dtFormatdB = new SimpleDateFormat(DATE_FORMAT);
+ return dtFormatdB.format(currDate);
+ }
+ }
+
+ /**
+ * 得到格式化后的时间,格式为yyyy-MM-dd HH:mm:ss,如2006-02-15 15:23:45
+ *
+ * @param currDate
+ * 要格式化的时间
+ * @see #getFormatDateTime(java.util.Date, String)
+ * @return String 返回格式化后的时间,默认格式为yyyy-MM-dd HH:mm:ss,如2006-02-15 15:23:45
+ */
+ public static String getFormatDateTime(java.util.Date currDate) {
+ return getFormatDateTime(currDate, TIME_FORMAT);
+ }
+
+ /**
+ * 得到格式化后的时间,格式为yyyy-MM-dd HH:mm:ss,如2006-02-15 15:23:45
+ *
+ * @param currDate
+ * 要格式环的时间
+ * @see #getFormatDateTime(String)
+ * @return Date 返回格式化后的时间,默认格式为yyyy-MM-dd HH:mm:ss,如2006-02-15 15:23:45
+ */
+ public static Date getFormatDateTimeToTime(java.util.Date currDate) {
+ return getFormatDateTime(getFormatDateTime(currDate));
+ }
+
+ /**
+ * 得到格式化后的时间,格式为yyyy-MM-dd HH:mm:ss,如2006-02-15 15:23:45
+ *
+ * @param currDate
+ * 要格式化的时间
+ * @see #getFormatDateTime(String, String)
+ * @return Date 返回格式化后的时间,默认格式为yyyy-MM-dd HH:mm:ss,如2006-02-15 15:23:45
+ */
+ public static Date getFormatDateTime(String currDate) {
+ return getFormatDateTime(currDate, TIME_FORMAT);
+ }
+
+ /**
+ * 得到格式化后的时间,格式为yyyy年MM月dd日 HH:mm:ss,如2006年02月15日 15:23:45
+ *
+ * @param currDate
+ * 要格式化的时间
+ * @see #getFormatDateTime(java.util.Date, String)
+ * @return String 返回格式化后的时间,默认格式为yyyy年MM月dd日 HH:mm:ss,如2006年02月15日 15:23:45
+ */
+ public static String getFormatDateTime_CN(java.util.Date currDate) {
+ return getFormatDateTime(currDate, TIME_FORMAT_CN);
+ }
+
+ /**
+ * 得到格式化后的时间,格式为yyyy年MM月dd日 HH:mm:ss,如2006年02月15日 15:23:45
+ *
+ * @param currDate
+ * 要格式化的时间
+ * @see #getFormatDateTime_CN(String)
+ * @return Date 返回格式化后的时间,默认格式为yyyy年MM月dd日 HH:mm:ss,如2006年02月15日 15:23:45
+ */
+ public static Date getFormatDateTimeToTime_CN(java.util.Date currDate) {
+ return getFormatDateTime_CN(getFormatDateTime_CN(currDate));
+ }
+
+ /**
+ * 得到格式化后的时间,格式为yyyy年MM月dd日 HH:mm:ss,如2006年02月15日 15:23:45
+ *
+ * @param currDate
+ * 要格式化的时间
+ * @see #getFormatDateTime(String, String)
+ * @return Date 返回格式化后的时间,默认格式为yyyy年MM月dd日 HH:mm:ss,如2006年02月15日 15:23:45
+ */
+ public static Date getFormatDateTime_CN(String currDate) {
+ return getFormatDateTime(currDate, TIME_FORMAT_CN);
+ }
+
+ /**
+ * 根据格式得到格式化后的时间
+ *
+ * @param currDate
+ * 要格式化的时间
+ * @param format
+ * 时间格式,如yyyy-MM-dd HH:mm:ss
+ * @see java.text.SimpleDateFormat#format(java.util.Date)
+ * @return String 返回格式化后的时间,格式由参数format定义,如yyyy-MM-dd HH:mm:ss
+ */
+ public static String getFormatDateTime(java.util.Date currDate, String format) {
+ SimpleDateFormat dtFormatdB = null;
+ try {
+ dtFormatdB = new SimpleDateFormat(format);
+ return dtFormatdB.format(currDate);
+ } catch (Exception e) {
+ dtFormatdB = new SimpleDateFormat(TIME_FORMAT);
+ return dtFormatdB.format(currDate);
+ }
+ }
+
+ /**
+ * 根据格式得到格式化后的日期
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @param format
+ * 日期格式,如yyyy-MM-dd
+ * @see java.text.SimpleDateFormat#parse(java.lang.String)
+ * @return Date 返回格式化后的日期,格式由参数format定义,如yyyy-MM-dd,如2006-02-15
+ */
+ public static Date getFormatDate(String currDate, String format) {
+ SimpleDateFormat dtFormatdB = null;
+ try {
+ dtFormatdB = new SimpleDateFormat(format);
+ return dtFormatdB.parse(currDate);
+ } catch (Exception e) {
+ dtFormatdB = new SimpleDateFormat(DATE_FORMAT);
+ try {
+ return dtFormatdB.parse(currDate);
+ } catch (Exception ex) {
+ }
+ }
+ return null;
+ }
+
+ /**
+ * 根据格式得到格式化后的时间
+ *
+ * @param currDate
+ * 要格式化的时间
+ * @param format
+ * 时间格式,如yyyy-MM-dd HH:mm:ss
+ * @see java.text.SimpleDateFormat#parse(java.lang.String)
+ * @return Date 返回格式化后的时间,格式由参数format定义,如yyyy-MM-dd HH:mm:ss
+ */
+ public static Date getFormatDateTime(String currDate, String format) {
+ SimpleDateFormat dtFormatdB = null;
+ try {
+ dtFormatdB = new SimpleDateFormat(format);
+ return dtFormatdB.parse(currDate);
+ } catch (Exception e) {
+ dtFormatdB = new SimpleDateFormat(TIME_FORMAT);
+ try {
+ return dtFormatdB.parse(currDate);
+ } catch (Exception ex) {
+ }
+ }
+ return null;
+ }
+
+ /**
+ * 得到格式化后的当前系统日期,格式为yyyy-MM-dd,如2006-02-15
+ *
+ * @see #getFormatDate(java.util.Date)
+ * @return String 返回格式化后的当前服务器系统日期,格式为yyyy-MM-dd,如2006-02-15
+ */
+ public static String getCurrDateStr() {
+ return getFormatDate(getCurrDate());
+ }
+
+ /**
+ * 得到格式化后的当前系统时间,格式为yyyy-MM-dd HH:mm:ss,如2006-02-15 15:23:45
+ *
+ * @see #getFormatDateTime(java.util.Date)
+ * @return String 返回格式化后的当前服务器系统时间,格式为yyyy-MM-dd HH:mm:ss,如2006-02-15 15:23:45
+ */
+ public static String getCurrDateTimeStr() {
+ return getFormatDateTime(getCurrDate());
+ }
+
+ /**
+ * 得到格式化后的当前系统日期,格式为yyyy年MM月dd日,如2006年02月15日
+ *
+ * @see #getFormatDate(java.util.Date, String)
+ * @return String 返回当前服务器系统日期,格式为yyyy年MM月dd日,如2006年02月15日
+ */
+ public static String getCurrDateStr_CN() {
+ return getFormatDate(getCurrDate(), DATE_FORMAT_CN);
+ }
+
+ /**
+ * 得到格式化后的当前系统时间,格式为yyyy年MM月dd日 HH:mm:ss,如2006年02月15日 15:23:45
+ *
+ * @see #getFormatDateTime(java.util.Date, String)
+ * @return String 返回格式化后的当前服务器系统时间,格式为yyyy年MM月dd日 HH:mm:ss,如2006年02月15日 15:23:45
+ */
+ public static String getCurrDateTimeStr_CN() {
+ return getFormatDateTime(getCurrDate(), TIME_FORMAT_CN);
+ }
+
+ /**
+ * 得到系统当前日期的前或者后几天
+ *
+ * @param iDate
+ * 如果要获得前几天日期,该参数为负数; 如果要获得后几天日期,该参数为正数
+ * @see java.util.Calendar#add(int, int)
+ * @return Date 返回系统当前日期的前或者后几天
+ */
+ public static Date getDateBeforeOrAfter(int iDate) {
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.DAY_OF_MONTH, iDate);
+ return cal.getTime();
+ }
+
+ /**
+ * 得到日期的前或者后几天
+ *
+ * @param iDate
+ * 如果要获得前几天日期,该参数为负数; 如果要获得后几天日期,该参数为正数
+ * @see java.util.Calendar#add(int, int)
+ * @return Date 返回参数curDate定义日期的前或者后几天
+ */
+ public static Date getDateBeforeOrAfter(Date curDate, int iDate) {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(curDate);
+ cal.add(Calendar.DAY_OF_MONTH, iDate);
+ return cal.getTime();
+ }
+
+ /**
+ * 得到格式化后的月份,格式为yyyy-MM,如2006-02
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see #getFormatDate(java.util.Date, String)
+ * @return String 返回格式化后的月份,格式为yyyy-MM,如2006-02
+ */
+ public static String getFormatMonth(java.util.Date currDate) {
+ return getFormatDate(currDate, MONTH_FORMAT);
+ }
+
+ /**
+ * 得到格式化后的日,格式为yyyyMMdd,如20060210
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see #getFormatDate(java.util.Date, String)
+ * @return String 返回格式化后的日,格式为yyyyMMdd,如20060210
+ */
+ public static String getFormatDay(java.util.Date currDate) {
+ return getFormatDate(currDate, DAY_FORMAT);
+ }
+
+
+ /**
+ * 得到格式化后的当月第一天,格式为yyyy-MM-dd,如2006-02-01
+ *
+ * @see java.util.Calendar#getMinimum(int)
+ * @see #getFormatDate(java.util.Date, String)
+ * @return String 返回格式化后的当月第一天,格式为yyyy-MM-dd,如2006-02-01
+ */
+ public static String getFirstDayOfMonth() {
+ Calendar cal = Calendar.getInstance();
+ int firstDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
+ cal.set(Calendar.DAY_OF_MONTH, firstDay);
+ return getFormatDate(cal.getTime(), DATE_FORMAT);
+ }
+
+ /**
+ * 得到格式化后的当月第一天,格式为yyyy-MM-dd,如2006-02-01
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see java.util.Calendar#getMinimum(int)
+ * @see #getFormatDate(java.util.Date, String)
+ * @return String 返回格式化后的当月第一天,格式为yyyy-MM-dd,如2006-02-01
+ */
+ public static String getFirstDayOfMonth(Date currDate) {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(currDate);
+ int firstDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
+ cal.set(Calendar.DAY_OF_MONTH, firstDay);
+ return getFormatDate(cal.getTime(), DATE_FORMAT);
+ }
+
+ /**
+ * 得到日期的前或者后几分钟
+ *
+ * 如果要获得前几分钟日期,该参数为负数; 如果要获得后几分钟日期,该参数为正数
+ * @see java.util.Calendar#add(int, int)
+ * @return Date 返回参数curDate定义日期的前或者后几分钟
+ */
+ public static Date getDateBeforeOrAfterMinutes(Date curDate, int iMinute) {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(curDate);
+ cal.add(Calendar.MINUTE, iMinute);
+ return cal.getTime();
+ }
+
+ /**
+ * 得到日期的前或者后几小时
+ *
+ * @param iHour
+ * 如果要获得前几小时日期,该参数为负数; 如果要获得后几小时日期,该参数为正数
+ * @see java.util.Calendar#add(int, int)
+ * @return Date 返回参数curDate定义日期的前或者后几小时
+ */
+ public static Date getDateBeforeOrAfterHours(Date curDate, int iHour) {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(curDate);
+ cal.add(Calendar.HOUR_OF_DAY, iHour);
+ return cal.getTime();
+ }
+
+ /**
+ * 得到日期的前或者后几个月
+ *
+ * @param iMonth
+ * 如果要获得前几个月日期,该参数为负数; 如果要获得后几个月日期,该参数为正数
+ * @see java.util.Calendar#add(int, int)
+ * @return Date 返回参数curDate定义日期的前或者后几个月
+ */
+ public static Date getDateBeforeOrAfterMonths(Date curDate, int iMonth) {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(curDate);
+ cal.add(Calendar.MONTH, iMonth);
+ return cal.getTime();
+ }
+
+ /**
+ * 获取日期的凌晨Date对象
+ *
+ * @Desc:
+ * @author: hanwei
+ * @param currDate
+ * @return Date
+ */
+ public static Date getWeeHourDateTime(java.util.Date currDate) {
+
+ Calendar c = Calendar.getInstance();
+ c.setTime(currDate);
+ c.set(Calendar.HOUR_OF_DAY, 0);
+ c.set(Calendar.MINUTE, 0);
+ c.set(Calendar.SECOND, 0);
+ return c.getTime();
+ }
+
+ /**
+ * 获取明天的凌晨的时间
+ *
+ * @Desc:
+ * @author: hanwei
+ * @param currDate
+ * @return Date
+ */
+ public static Date getDateOfNextDay(java.util.Date currDate) {
+
+ return getWeeHourDateTime(getDateBeforeOrAfter(currDate, 1));
+ }
+
+ /**
+ * 得到传入日期与当前系统日期相差几天 (纯日期比较)
+ *
+ * @Desc: 比如 2月5日 和2月6日比较 不关心几点 就是返回相差1天
+ *
+ * @author: hanwei
+ * @param currDate
+ * 需要比较的日期时间
+ * @return long 返回参数 如果比较日期比当前日期小,则返回负数 如果比较日期比当前日期大,则返回正数
+ */
+ public static int getIntCompareToCurrDateDay(java.util.Date currDate) {
+
+ currDate = getWeeHourDateTime(currDate);
+ Date nowDate = getWeeHourDateTime(getCurrDate());
+
+ long currDayCount = currDate.getTime() / (1000 * 60 * 60 * 24);
+ long nowDateCount = nowDate.getTime() / (1000 * 60 * 60 * 24);
+
+ return Math.abs((int) (currDayCount - nowDateCount));
+ }
+
+ /**
+ * 得到传入日期与当前日期相差几天 (24小时为一天,不足24小时为0天)
+ *
+ * @Desc:
+ * @author: hanwei
+ * @param currDate
+ * 需要比较的日期时间
+ * @return long 返回参数 如果比较日期比当前日期小,则返回负数 如果比较日期比当前日期大,则返回正数
+ */
+ public static int getIntCompareToCurrDateHour(java.util.Date currDate) {
+
+ return (int) ((currDate.getTime() - getCurrDate().getTime()) / (1000 * 60 * 60 * 24));
+ }
+
+ /**
+ * 根据时间戳返回日期对象
+ *
+ * @Desc:
+ * @author: hanwei
+ * @return Date
+ */
+ public static Date getDateFromTimeMillis(java.lang.Long currentTimeMillis) {
+
+ Calendar cal = Calendar.getInstance();
+ cal.setTimeInMillis(currentTimeMillis);
+
+ return cal.getTime();
+ }
+
+ /**
+ * 根据时间戳(秒)返回日期对象
+ *
+ * @Desc:
+ * @author: hanwei
+ * @return Date
+ */
+ public static Date getDateFromTimeSeconds(int second) {
+
+ String mills = second + "000";
+ Calendar cal = Calendar.getInstance();
+ cal.setTimeInMillis(Long.parseLong(mills));
+
+ return cal.getTime();
+ }
+
+
+ /************ hanwei end **************/
+
+ /**
+ * Get the previous time, from how many days to now.
+ *
+ * @param days
+ * How many days.
+ * @return The new previous time.
+ */
+ public static Date previous(int days) {
+ return new Date(System.currentTimeMillis() - days * 3600000L * 24L);
+ }
+
+ public static String formatDateTime(Date d) {
+ return new SimpleDateFormat(TIME_FORMAT).format(d);
+ }
+
+ public static String formatDateTime(long d) {
+ return new SimpleDateFormat(TIME_FORMAT).format(d);
+ }
+
+ public static Date parseDate(String d) {
+ try {
+ return new SimpleDateFormat(DATE_FORMAT).parse(d);
+ } catch (Exception e) {
+ }
+ return null;
+ }
+
+ public static Date parseDateTime(String dt) {
+ try {
+ return new SimpleDateFormat(TIME_FORMAT).parse(dt);
+ } catch (Exception e) {
+ }
+ return null;
+ }
+
+ /** 日期 */
+ public final static String DEFAILT_DATE_PATTERN = "yyyy-MM-dd";
+ /** 日期时间 */
+ public final static String DEFAILT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
+ /** 时间 */
+ public final static String DEFAULT_TIME_PATTERN = "HH:mm:ss";
+ /**
+ * 每天的毫秒数
+ */
+ public final static long MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
+
+ /**
+ * 转换日期字符串得到指定格式的日期类型
+ *
+ * @param formatString
+ * 需要转换的格式字符串
+ * @param targetDate
+ * 需要转换的时间
+ * @return
+ * @throws ParseException
+ */
+ public static final Date convertString2Date(String formatString, String targetDate) throws ParseException {
+ if (StringUtils.isBlank(targetDate))
+ return null;
+ SimpleDateFormat format = null;
+ Date result = null;
+ format = new SimpleDateFormat(formatString);
+ try {
+ result = format.parse(targetDate);
+ } catch (ParseException pe) {
+ throw new ParseException(pe.getMessage(), pe.getErrorOffset());
+ }
+ return result;
+ }
+
+ public static final Date convertString2Date(String[] formatString, String targetDate) throws ParseException {
+ if (StringUtils.isBlank(targetDate)) {
+ return null;
+ }
+ SimpleDateFormat format = null;
+ Date result = null;
+ String errorMessage = null;
+ Integer errorOffset = null;
+ for (String dateFormat : formatString) {
+ try {
+ format = new SimpleDateFormat(dateFormat);
+ result = format.parse(targetDate);
+ } catch (ParseException pe) {
+ result = null;
+ errorMessage = pe.getMessage();
+ errorOffset = pe.getErrorOffset();
+ } finally {
+ if (result != null && result.getTime() > 1) {
+ break;
+ }
+ }
+ }
+ if (result == null) {
+ throw new ParseException(errorMessage, errorOffset);
+ }
+ return result;
+ }
+
+ /**
+ * 转换字符串得到默认格式的日期类型
+ *
+ * @param strDate
+ * @return
+ * @throws ParseException
+ */
+ public static Date convertString2Date(String strDate) throws ParseException {
+ Date result = null;
+ try {
+ result = convertString2Date(DEFAILT_DATE_PATTERN, strDate);
+ } catch (ParseException pe) {
+ throw new ParseException(pe.getMessage(), pe.getErrorOffset());
+ }
+ return result;
+ }
+
+ /**
+ * 转换日期得到指定格式的日期字符串
+ *
+ * @param formatString
+ * 需要把目标日期格式化什么样子的格式。例如,yyyy-MM-dd HH:mm:ss
+ * @param targetDate
+ * 目标日期
+ * @return
+ */
+ public static String convertDate2String(String formatString, Date targetDate) {
+ SimpleDateFormat format = null;
+ String result = null;
+ if (targetDate != null) {
+ format = new SimpleDateFormat(formatString);
+ result = format.format(targetDate);
+ } else {
+ return null;
+ }
+ return result;
+ }
+
+ /**
+ * 转换日期,得到默认日期格式字符串
+ *
+ * @param targetDate
+ * @return
+ */
+ public static String convertDate2String(Date targetDate) {
+ return convertDate2String(DEFAILT_DATE_PATTERN, targetDate);
+ }
+
+ /**
+ * 比较日期大小
+ *
+ * @param src
+ * @param src
+ * @return int; 1:DATE1>DATE2;
+ */
+ public static int compare_date(Date src, Date src1) {
+
+ String date1 = convertDate2String(DEFAILT_DATE_TIME_PATTERN, src);
+ String date2 = convertDate2String(DEFAILT_DATE_TIME_PATTERN, src1);
+ DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ try {
+ Date dt1 = df.parse(date1);
+ Date dt2 = df.parse(date2);
+ if (dt1.getTime() > dt2.getTime()) {
+ return 1;
+ } else if (dt1.getTime() < dt2.getTime()) {
+ return -1;
+ } else {
+ return 0;
+ }
+ } catch (Exception exception) {
+ exception.printStackTrace();
+ }
+ return 0;
+ }
+
+ /**
+ * 日期比较
+ *
+ * 判断时间date1是否在时间date2之前
+ * 时间格式 2005-4-21 16:16:34
+ * 添加人:胡建国
+ *
+ * @return
+ */
+ public static boolean isDateBefore(String date1, String date2) {
+ try {
+ DateFormat df = DateFormat.getDateTimeInstance();
+ return df.parse(date1).before(df.parse(date2));
+ } catch (ParseException e) {
+ return false;
+ }
+ }
+
+ /**
+ * 日期比较
+ *
+ * 判断当前时间是否在时间date2之前
+ * 时间格式 2005-4-21 16:16:34
+ * 添加人:胡建国
+ *
+ * @return
+ */
+ public static boolean isDateBefore(String date2) {
+ if (date2 == null) {
+ return false;
+ }
+ try {
+ Date date1 = new Date();
+ DateFormat df = DateFormat.getDateTimeInstance();
+ return date1.before(df.parse(date2));
+ } catch (ParseException e) {
+ return false;
+ }
+ }
+
+ /**
+ * 比较当前时间与时间date2的天相等 时间格式 2008-11-25 16:30:10 如:当前时间是2008-11-25 16:30:10与传入时间2008-11-25 15:31:20 相比较,返回true即相等
+ *
+
+ * @param date2
+ * @return boolean; true:相等
+ * @author zhangjl
+ */
+ public static boolean equalDate(String date2) {
+ try {
+ String date1 = convertDate2String(DEFAILT_DATE_TIME_PATTERN, new Date());
+ date1.equals(date2);
+ Date d1 = convertString2Date(DEFAILT_DATE_PATTERN, date1);
+ Date d2 = convertString2Date(DEFAILT_DATE_PATTERN, date2);
+ return d1.equals(d2);
+ } catch (ParseException e) {
+ System.out.println(e.getMessage());
+ return false;
+ }
+ }
+
+ /**
+ * 比较时间date1与时间date2的天相等 时间格式 2008-11-25 16:30:10
+ *
+ * @param date1
+ * @param date2
+ * @return boolean; true:相等
+ * @author zhangjl
+ */
+ public static boolean equalDate(String date1, String date2) {
+ try {
+
+ Date d1 = convertString2Date(DEFAILT_DATE_PATTERN, date1);
+ Date d2 = convertString2Date(DEFAILT_DATE_PATTERN, date2);
+
+ return d1.equals(d2);
+ } catch (ParseException e) {
+ return false;
+ }
+ }
+
+ /**
+ * 比较时间date1是否在时间date2之前 时间格式 2008-11-25 16:30:10
+ *
+ * @param date1
+ * @param date2
+ * @return boolean; true:在date2之前
+ * @author 胡建国
+ */
+ public static boolean beforeDate(String date1, String date2) {
+ try {
+ Date d1 = convertString2Date(DEFAILT_DATE_PATTERN, date1);
+ Date d2 = convertString2Date(DEFAILT_DATE_PATTERN, date2);
+ return d1.before(d2);
+ } catch (ParseException e) {
+ return false;
+ }
+ }
+
+ /**
+ * 获取上个月开始时间
+ *
+ * @param currentDate
+ * 当前时间
+ * @return 上个月的第一天
+ */
+ public static Date getBoferBeginDate(Calendar currentDate) {
+ Calendar result = Calendar.getInstance();
+ result.set(currentDate.get(Calendar.YEAR), (currentDate.get(Calendar.MONTH)) - 1,
+ result.getActualMinimum(Calendar.DATE), 0, 0, 0);
+ return result.getTime();
+ }
+
+ /**
+ * 获取指定月份的第一天
+ *
+ * @param currentDate
+ * @return
+ */
+ public static Date getBeginDate(Calendar currentDate) {
+ Calendar result = Calendar.getInstance();
+ result.set(currentDate.get(Calendar.YEAR), (currentDate.get(Calendar.MONTH)),
+ result.getActualMinimum(Calendar.DATE));
+ return result.getTime();
+ }
+
+ /**
+ * 获取上个月结束时间
+ *
+ * @param currentDate
+ * 当前时间
+ * @return 上个月最后一天
+ */
+ public static Date getBoferEndDate(Calendar currentDate) {
+ Calendar result = currentDate;
+ // result.set(currentDate.get(Calendar.YEAR), currentDate
+ // .get(Calendar.MONTH) - 1);
+ result.set(Calendar.DATE, 1);
+ result.add(Calendar.DATE, -1);
+ return result.getTime();
+ }
+
+ /**
+ * 获取两个时间的时间间隔
+ *
+ * @param beginDate
+ * 开始时间
+ * @param endDate
+ * 结束时间
+ * @return
+ */
+ public static int getDaysBetween(Calendar beginDate, Calendar endDate) {
+ if (beginDate.after(endDate)) {
+ Calendar swap = beginDate;
+ beginDate = endDate;
+ endDate = swap;
+ }
+ int days = endDate.get(Calendar.DAY_OF_YEAR) - beginDate.get(Calendar.DAY_OF_YEAR) + 1;
+ int year = endDate.get(Calendar.YEAR);
+ if (beginDate.get(Calendar.YEAR) != year) {
+ beginDate = (Calendar) beginDate.clone();
+ do {
+ days += beginDate.getActualMaximum(Calendar.DAY_OF_YEAR);
+ beginDate.add(Calendar.YEAR, 1);
+ } while (beginDate.get(Calendar.YEAR) != year);
+ }
+ return days;
+ }
+
+ /**
+ * 获取两个时间的时间间隔(月份)
+ *
+ * @param beginDate
+ * 开始时间
+ * @param endDate
+ * 结束时间
+ * @return
+ */
+ public static int getMonthsBetween(Date beginDate, Date endDate) {
+ if (beginDate.after(endDate)) {
+ Date swap = beginDate;
+ beginDate = endDate;
+ endDate = swap;
+ }
+ int months = endDate.getMonth() - beginDate.getMonth();
+ int years = endDate.getYear() - beginDate.getYear();
+
+ months += years * 12;
+
+ return months;
+ }
+
+ /**
+ * 获取两个时间内的工作日
+ *
+ * @param beginDate
+ * 开始时间
+ * @param endDate
+ * 结束时间
+ * @return
+ */
+ public static int getWorkingDay(Calendar beginDate, Calendar endDate) {
+ int result = -1;
+ if (beginDate.after(endDate)) {
+ Calendar swap = beginDate;
+ beginDate = endDate;
+ endDate = swap;
+ }
+ int charge_start_date = 0;
+ int charge_end_date = 0;
+ int stmp;
+ int etmp;
+ stmp = 7 - beginDate.get(Calendar.DAY_OF_WEEK);
+ etmp = 7 - endDate.get(Calendar.DAY_OF_WEEK);
+ if (stmp != 0 && stmp != 6) {
+ charge_start_date = stmp - 1;
+ }
+ if (etmp != 0 && etmp != 6) {
+ charge_end_date = etmp - 1;
+ }
+ result = (getDaysBetween(getNextMonday(beginDate), getNextMonday(endDate)) / 7) * 5 + charge_start_date
+ - charge_end_date;
+ return result;
+ }
+
+ /**
+ * 根据当前给定的日期获取当前天是星期几(中国版的)
+ *
+ * @param date
+ * 任意时间
+ * @return
+ */
+ public static String getChineseWeek(Calendar date) {
+ final String[] dayNames = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
+ int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
+ return dayNames[dayOfWeek - 1];
+
+ }
+
+ /**
+ * 获得日期的下一个星期一的日期
+ *
+ * @param date
+ * 任意时间
+ * @return
+ */
+ public static Calendar getNextMonday(Calendar date) {
+ Calendar result = null;
+ result = date;
+ do {
+ result = (Calendar) result.clone();
+ result.add(Calendar.DATE, 1);
+ } while (result.get(Calendar.DAY_OF_WEEK) != 2);
+ return result;
+ }
+
+ /**
+ * 获取两个日期之间的休息时间
+ *
+ * @param beginDate
+ * 开始时间
+ * @param endDate
+ * 结束时间
+ * @return
+ */
+ public static int getHolidays(Calendar beginDate, Calendar endDate) {
+ return getDaysBetween(beginDate, endDate) - getWorkingDay(beginDate, endDate);
+
+ }
+ /**
+ *
+ * 判断日期是否在当前区间内
+ * @param beginDate
+ * @param endDate
+ * @param currentDate
+ * @return
+ * @throws
+ * @Author:hanwei
+ * @CreateDate :2022年8月27日 上午11:17:30
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static boolean isDateBetween(Date beginDate, Date endDate, Date currentDate) {
+ // 开始日期
+ long beginDateLong = beginDate.getTime();
+ // 结束日期
+ long endDateLong = endDate.getTime();
+ // 当前日期
+ long currentDateLong = currentDate.getTime();
+ if (currentDateLong > beginDateLong && currentDateLong < endDateLong) {
+ return Boolean.TRUE;
+ }
+ return Boolean.FALSE;
+ }
+
+ /**
+ * 获取当前月份的第一天
+ *
+ * 当前时间
+ * @return
+ */
+ public static Date getMinDate(Calendar currentDate) {
+ Calendar result = Calendar.getInstance();
+ result.set(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH),
+ currentDate.getActualMinimum(Calendar.DATE));
+ return result.getTime();
+ }
+
+ public static Calendar getDate(int year, int month, int date) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(year, month, date);
+ return calendar;
+ }
+
+ public static Calendar getDate(int year, int month) {
+ return getDate(year, month, 0);
+ }
+
+ public static Date getCountMinDate(int year, int month) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(year, month - 1, calendar.getActualMinimum(Calendar.DATE));
+ return calendar.getTime();
+ }
+
+ public static Date getCountMaxDate(int year, int month) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(Calendar.YEAR, year);
+ calendar.set(Calendar.MONTH, month);
+ Calendar calendar2 = Calendar.getInstance();
+ calendar2.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 0);
+ return calendar2.getTime();
+ }
+
+ /**
+ * 获取当前月份的第一天
+ *
+ * 当前时间
+ * @return
+ */
+ public static Date getMinDate() {
+ Calendar currentDate = Calendar.getInstance();
+ return getMinDate(currentDate);
+ }
+
+ /**
+ * 获取当前月分的最大天数
+ *
+ * @param currentDate
+ * 当前时间
+ * @return
+ */
+ public static Date getMaxDate(Calendar currentDate) {
+ Calendar result = Calendar.getInstance();
+ result.set(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH),
+ currentDate.getActualMaximum(Calendar.DATE));
+ return result.getTime();
+ }
+
+ /**
+ * 获取当前月分的最大天数
+ *
+ * 当前时间
+ * @return
+ */
+ public static Date getMaxDate() {
+ Calendar currentDate = Calendar.getInstance();
+ return getMaxDate(currentDate);
+ }
+
+ /**
+ * 获取今天最大的时间
+ *
+ * @return
+ */
+ public static String getMaxDateTimeForToDay() {
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(Calendar.HOUR_OF_DAY, calendar.getMaximum(Calendar.HOUR_OF_DAY));
+ calendar.set(Calendar.MINUTE, calendar.getMaximum(Calendar.MINUTE));
+ calendar.set(Calendar.SECOND, calendar.getMaximum(Calendar.SECOND));
+ return convertDate2String(DEFAILT_DATE_TIME_PATTERN, calendar.getTime());
+ }
+
+ /**
+ * 获取日期最大的时间
+ *
+ * @return
+ */
+ public static Date getMaxDateTimeForToDay(Date date) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(date);
+ calendar.set(Calendar.HOUR_OF_DAY, calendar.getMaximum(Calendar.HOUR_OF_DAY));
+ calendar.set(Calendar.MINUTE, calendar.getMaximum(Calendar.MINUTE));
+ calendar.set(Calendar.SECOND, calendar.getMaximum(Calendar.SECOND));
+ return calendar.getTime();
+ }
+
+ /**
+ * 获取今天最小时间
+ *
+ * @return
+ */
+ public static String getMinDateTimeForToDay() {
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(Calendar.HOUR_OF_DAY, calendar.getMinimum(Calendar.HOUR_OF_DAY));
+ calendar.set(Calendar.MINUTE, calendar.getMinimum(Calendar.MINUTE));
+ calendar.set(Calendar.SECOND, calendar.getMinimum(Calendar.SECOND));
+ return convertDate2String(DEFAILT_DATE_TIME_PATTERN, calendar.getTime());
+ }
+
+ /**
+ * 获取 date 最小时间
+ *
+ * @return
+ */
+ public static Date getMinDateTimeForToDay(Date date) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(date);
+ calendar.set(Calendar.HOUR_OF_DAY, calendar.getMinimum(Calendar.HOUR_OF_DAY));
+ calendar.set(Calendar.MINUTE, calendar.getMinimum(Calendar.MINUTE));
+ calendar.set(Calendar.SECOND, calendar.getMinimum(Calendar.SECOND));
+ return calendar.getTime();
+ }
+
+ /**
+ * 获取发生日期的结束时间 根据用户设置的日期天数来判定这这个日期是什么(例如 (getHappenMinDate = 2008-10-1) 的话 那么 (getHappenMaxDate = 2008-11-1) 号)
+ *
+ * @return
+ */
+ public static Date getHappenMaxDate() {
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
+ return calendar.getTime();
+ }
+
+ /**
+ * 加减天数
+ *
+ * @param num
+ * @param Date
+ * @return
+ */
+ public static Date addDay(int num, Date Date) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(Date);
+ calendar.add(Calendar.DATE, num);// 把日期往后增加 num 天.整数往后推,负数往前移动
+ return calendar.getTime(); // 这个时间就是日期往后推一天的结果
+ }
+ /**
+ * 加减年份
+ *
+ * @param num
+ * @param Date
+ * @return
+ */
+ public static Date addYear(int num, Date Date) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(Date);
+ calendar.add(Calendar.YEAR, num);// 把日期往后增加 num 天.整数往后推,负数往前移动
+ return calendar.getTime(); // 这个时间就是日期往后推一天的结果
+ }
+
+ /**
+ * 加减分钟
+ *
+ * @param num
+ * @param Date
+ * @return
+ */
+ public static Date addMin(int num, Date Date) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(Date);
+ calendar.add(Calendar.MINUTE, num);
+ return calendar.getTime();
+ }
+
+ /**
+ * 加减分钟
+ *
+ * @param num
+ * @param Date
+ * @return
+ */
+ public static Date addHour(int num, Date Date) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(Date);
+ calendar.add(Calendar.HOUR, num);
+ return calendar.getTime();
+ }
+ /**
+ * 加减秒数
+ *
+ * @param num
+ * @param Date
+ * @return
+ */
+ public static Date addSecond(int num, Date Date) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(Date);
+ calendar.add(Calendar.SECOND, num);
+ return calendar.getTime();
+ }
+
+
+ /*
+ * public static void main(String[] args) { System.out.println(getMaxDateTimeForToDay());
+ * System.out.println(getMinDateTimeForToDay()); }
+ */
+
+ /**
+ * 计算两端时间的小时差
+ *
+ * @param begin
+ * @param end
+ * @return
+ */
+ public static int getHour(Date begin, Date end) {
+ Calendar c1 = Calendar.getInstance();
+ c1.setTime(begin);
+ Calendar c2 = Calendar.getInstance();
+ c2.setTime(end);
+ Long millisecond = c2.getTimeInMillis() - c1.getTimeInMillis();
+ Long hour = millisecond / 1000 / 60 / 60;
+ Long minute = (millisecond / 1000 / 60) % 60;
+ if (minute >= 30) {
+ hour++;
+ }
+
+ return hour.intValue();
+ }
+
+ /**
+ * 计算两端时间的小时差 取整数
+ *
+ * @param begin
+ * @param end
+ * @return
+ */
+ public static int getTruncHour(Date begin, Date end) {
+ Calendar c1 = Calendar.getInstance();
+ c1.setTime(begin);
+ Calendar c2 = Calendar.getInstance();
+ c2.setTime(end);
+ Long millisecond = c2.getTimeInMillis() - c1.getTimeInMillis();
+ Long hour = millisecond / 1000 / 60 / 60;
+
+ return hour.intValue();
+ }
+
+
+ /**
+ *
+ * 计算两端时间的小时差
+ * @param begin
+ * @param end
+ * @return
+ * @throws
+ * @Author:hanwei
+ * @CreateDate :2022年9月8日 下午5:55:55
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static String getHourBuf(Date begin, Date end) {
+ Calendar c1 = Calendar.getInstance();
+ c1.setTime(begin);
+ Calendar c2 = Calendar.getInstance();
+ c2.setTime(end);
+ Long millisecond = c2.getTimeInMillis() - c1.getTimeInMillis();
+ Long hour = millisecond / 1000 / 60 / 60;
+ Long minute = (millisecond / 1000 / 60) % 60;
+ return StringUtils.join(hour,"小时",minute,"分钟");
+ }
+
+ /**
+ * 格式化日期
+ */
+ public static String dateFormat(Date date) {
+ if (date == null) {
+ return null;
+ }
+ return DateFormat.getDateInstance().format(date);
+ }
+
+ /**
+ * @return String
+ * @throws ParseException
+ */
+ public static String setDateFormat(Date myDate, String strFormat) throws ParseException {
+ if (myDate == null) {
+ return null;
+ }
+ SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
+ String sDate = sdf.format(myDate);
+ return sDate;
+ }
+
+ public static String setDateFormat(String myDate, String strFormat) throws ParseException {
+ SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
+ String sDate = sdf.format(myDate);
+
+ return sDate;
+ }
+
+ /*****************************************
+ * @功能 计算某年某月的结束日期
+ * @return interger
+ * @throws ParseException
+ ****************************************/
+ public static String getYearMonthEndDay(int yearNum, int monthNum) throws ParseException {
+
+ // 分别取得当前日期的年、月、日
+ String tempYear = Integer.toString(yearNum);
+ String tempMonth = Integer.toString(monthNum);
+ String tempDay = "31";
+ if (tempMonth.equals("1") || tempMonth.equals("3") || tempMonth.equals("5") || tempMonth.equals("7")
+ || tempMonth.equals("8") || tempMonth.equals("10") || tempMonth.equals("12")) {
+ tempDay = "31";
+ }
+ if (tempMonth.equals("4") || tempMonth.equals("6") || tempMonth.equals("9") || tempMonth.equals("11")) {
+ tempDay = "30";
+ }
+ if (tempMonth.equals("2")) {
+ if (isLeapYear(yearNum)) {
+ tempDay = "29";
+ } else {
+ tempDay = "28";
+ }
+ }
+ String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
+ return tempDate;// setDateFormat(tempDate,"yyyy-MM-dd");
+ }
+
+ /*****************************************
+ * @功能 判断某年是否为闰年
+ * @return boolean
+ * @throws ParseException
+ ****************************************/
+ public static boolean isLeapYear(int yearNum) {
+ boolean isLeep = false;
+ /** 判断是否为闰年,赋值给一标识符flag */
+ if ((yearNum % 4 == 0) && (yearNum % 100 != 0)) {
+ isLeep = true;
+ } else isLeep = yearNum % 400 == 0;
+ return isLeep;
+ }
+
+ /**
+ * 格式化日期
+ *
+ * @throws ParseException
+ *
+ * 例: DateUtils.formatDate("yyyy-MM-dd HH",new Date()) "yyyy-MM-dd HH:00:00"
+ */
+ public static Date formatDate(String formatString, Date date) throws ParseException {
+ if (date == null) {
+ date = new Date();
+ }
+ if (StringUtils.isBlank(formatString))
+ formatString = DateTimeUtils.DEFAILT_DATE_PATTERN;
+
+ date = DateTimeUtils.convertString2Date(formatString, DateTimeUtils.convertDate2String(formatString, date));
+
+ return date;
+ }
+
+ /**
+ * 格式化日期 yyyy-MM-dd
+ *
+ * @throws ParseException
+ * 例: DateUtils.formatDate(new Date()) "yyyy-MM-dd 00:00:00"
+ */
+ public static Date formatDate(Date date) throws ParseException{
+ try {
+ date = formatDate(DateTimeUtils.DEFAILT_DATE_PATTERN, date);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ return date;
+ }
+
+ /**
+ * @throws ParseException
+ * 根据日期获得 星期一的日期
+ *
+ */
+ public static Date getMonDay(Date date) throws ParseException {
+
+ Calendar cal = Calendar.getInstance();
+ if (date == null)
+ date = new Date();
+ cal.setTime(date);
+ if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
+ cal.add(Calendar.WEEK_OF_YEAR, -1);
+
+ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
+
+ date = formatDate(cal.getTime());
+
+ return date;
+ }
+
+ /**
+ * @throws ParseException
+ * 根据日期获得 星期日 的日期
+ *
+ */
+ public static Date getSunDay(Date date) throws ParseException {
+
+ Calendar cal = Calendar.getInstance();
+ if (date == null)
+ date = new Date();
+ cal.setTime(date);
+ if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)
+ cal.add(Calendar.WEEK_OF_YEAR, 1);
+
+ cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
+
+ date = formatDate(cal.getTime());
+ return date;
+ }
+
+ /**
+ * 获得 下个月的第一天
+ *
+ * @param date
+ * @return
+ * @throws ParseException
+ */
+ public static Date getNextDay(Date date) throws ParseException {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(date);
+ cal.add(Calendar.MONTH, 1);
+ cal.set(Calendar.DATE, 1);
+ return formatDate(cal.getTime());
+ }
+
+ /**
+ * 获取二个日期相差几天
+ * @param startTime
+ * @param endTime
+ * @return
+ */
+ public static int getOverdueDays(Date startTime ,Date endTime){
+
+ Calendar startCal = Calendar.getInstance();
+ Calendar endCal = Calendar.getInstance();
+ startCal.setTime(startTime);
+ endCal.setTime(endTime);
+ int overDays = DateTimeUtils.getDaysBetween(startCal,
+ endCal);
+ overDays = Math.abs(overDays);
+ return overDays;
+ }
+
+ /**
+ *
+ * @Description: 日期格式化
+ * @param date
+ * @param flag 0 返回yyyy-MM-dd 00:00:00日期
+ * 1 返回yyyy-MM-dd 23:59:59日期
+ * @return
+ * @throws
+ * @Author:hanwei
+ * @CreateDtae :2017年4月1日 上午10:49:47
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Date boundaryHours(Date date, int flag) {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(date);
+ int hour = cal.get(Calendar.HOUR_OF_DAY);
+ int minute = cal.get(Calendar.MINUTE);
+ int second = cal.get(Calendar.SECOND);
+ //时分秒(毫秒数)
+ long millisecond = hour*60*60*1000 + minute*60*1000 + second*1000;
+ //凌晨00:00:00
+ cal.setTimeInMillis(cal.getTimeInMillis()-millisecond);
+ if (flag == 0) {
+ return cal.getTime();
+ } else if (flag == 1) {
+ //凌晨23:59:59
+ cal.setTimeInMillis(cal.getTimeInMillis()+23*60*60*1000 + 59*60*1000 + 59*1000);
+ }
+ return cal.getTime();
+ }
+ /**
+ *
+ * @Description: 当前时间距离整点的毫秒数
+ * @param date
+ * @return
+ * @throws
+ * @Author:hanwei
+ * @CreateDtae :2017年8月26日 下午5:43:52
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static long distanceWholeTimes(Date date) {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(date);
+ int hour = cal.get(Calendar.HOUR_OF_DAY);
+ int minute = cal.get(Calendar.MINUTE);
+ int second = cal.get(Calendar.SECOND);
+ long millNow = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second
+ * 1000;
+ long millWhole = (hour + 1) * 60 * 60 * 1000;
+ return millWhole - millNow;
+ }
+
+ /**
+ *
+ * @Description 获取当前月指定天数的日期
+ * @param day
+ * 指定天数,如果为0则为最后一天
+ * @param pattern
+ * 输出日期格式化,可为null,默认为yyyyMMdd
+ * @return String
+ * @create 2017年11月22日 上午9:50:28
+ * @author LiGuoqing
+ */
+ public static String dayOfMonth(int day,String pattern){
+ SimpleDateFormat format = null;
+ if(StringUtils.isNotEmpty(pattern)){
+ format = new SimpleDateFormat(pattern);
+ }else{
+ format = new SimpleDateFormat("yyyyMMdd");
+ }
+ Calendar cal=Calendar.getInstance();//获取当前日期
+ cal.set(Calendar.DAY_OF_MONTH,day==0?cal.getActualMaximum(Calendar.DAY_OF_MONTH):day);
+ return format.format(cal.getTime());
+ }
+
+ /**
+ *
+ * @Description 获取当前时间差,取一位小数以年为单位
+ * @author LMX
+ * @throws ParseException
+ */
+ public static String betweenYears(String beforeTime) throws ParseException{
+ DateFormat df = new SimpleDateFormat("yyyy-MM");
+ Date d1 = new Date();
+ Date d2 = df.parse(beforeTime);
+ double diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
+ double years = diff / (1000 * 60 * 60 * 24)/365;
+ NumberFormat nf = NumberFormat.getNumberInstance();
+ nf.setMaximumFractionDigits(1);
+ nf.setRoundingMode(RoundingMode.UP);
+ return nf.format(years);
+ }
+
+ /**
+ *
+ * @Description: 获取当前年份
+ * @return
+ * @throws
+ * @Author:hanwei
+ * @CreateDtae :2018年3月8日 下午5:53:23
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Integer getCurrentYear() {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
+ Date date = new Date();
+ return Integer.parseInt(sdf.format(date));
+ }
+ /**
+ *
+ * _当前月份
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2020年5月26日 下午3:59:11
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Integer getCurrentMonth() {
+ SimpleDateFormat sdf = new SimpleDateFormat("MM");
+ Date date = new Date();
+ return Integer.parseInt(sdf.format(date));
+ }
+
+ /**
+ *
+ * _当前月份
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2020年5月26日 下午3:59:11
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static String getCurrentMonthStr() {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
+ Date date = new Date();
+ return sdf.format(date);
+ }
+ /**
+ *
+ * _当前日期
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2020年5月26日 下午3:59:19
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Integer getCurrentDay() {
+ SimpleDateFormat sdf = new SimpleDateFormat("dd");
+ Date date = new Date();
+ return Integer.parseInt(sdf.format(date));
+ }
+ /**
+ *
+ * _当前unix_time
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2020年5月26日 下午3:59:19
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Integer getCurrentUnixTime() {
+ return (int)(System.currentTimeMillis()/1000);
+ }
+ /**
+ * 获取今天最小秒数
+ * @return
+ */
+ public static int getTodayMinSecond(){
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(new Date());
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
+ calendar.set(Calendar.MINUTE, 0);
+ calendar.set(Calendar.SECOND, 0);
+ Date zero = calendar.getTime();
+ return (int)(zero.getTime()/1000);
+ }
+
+ /**
+ * 获取某个日期最小(0时0分0秒)秒数
+ * @param date
+ * @return
+ */
+ public static int getDateMinSecond(Date date){
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(date);
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
+ calendar.set(Calendar.MINUTE, 0);
+ calendar.set(Calendar.SECOND, 0);
+ Date zero = calendar.getTime();
+ return (int)(zero.getTime()/1000);
+ }
+ /**
+ * 获取某个日期最大(0时0分0秒)秒数
+ * @param date
+ * @return
+ */
+ public static int getDateMaxSecond(Date date){
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(date);
+ calendar.set(Calendar.HOUR_OF_DAY, 23);
+ calendar.set(Calendar.MINUTE, 59);
+ calendar.set(Calendar.SECOND, 59);
+ Date zero = calendar.getTime();
+ return (int)(zero.getTime()/1000);
+ }
+ /**
+ *
+ * _昨天(yyyy-mm-dd)
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2019年5月30日 下午2:24:09
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static String getFormatYesterday(){
+ return DateTimeUtils.getFormatDate(DateTimeUtils.getDateBeforeOrAfter(-1));
+ }
+ /**
+ *
+ * _月份加减
+ * @param month
+ * @param date
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2019年8月9日 下午1:54:24
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Date addMonth(int month,Date date) {
+ Calendar cl = Calendar.getInstance();
+ cl.setTime(date);
+ cl.add(Calendar.MONTH, month);
+ return cl.getTime();
+ }
+ /**
+ *
+ * _判断是否为日期
+ * @param date 日期
+ * @param dayFormat 日期格式
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2019年8月9日 下午2:13:46
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static boolean isValidDate(String date,String dayFormat) {
+
+ boolean isDate = Boolean.TRUE;
+ SimpleDateFormat format = new SimpleDateFormat(dayFormat);
+ try {
+ format.setLenient(Boolean.FALSE);
+ format.parse(date);
+ } catch (ParseException e) {
+ isDate = Boolean.FALSE;
+ }
+ return isDate;
+ }
+ /**
+ *
+ * _获取月份(mm) +-
+ * @param add 负数向前 正数向后
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2018年8月17日 下午3:54:36
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Integer addMonthMM (int add) {
+ SimpleDateFormat format = new SimpleDateFormat("MM");
+ Calendar c = Calendar.getInstance();
+ c.setTime(new Date());
+ c.add(Calendar.MONTH, add);
+ Date m = c.getTime();
+ String lastMonth = format.format(m);
+ return Integer.valueOf(lastMonth);
+ }
+
+ /**
+ *
+ * _获取月份(yyyy-MM) +-
+ * @param add 负数向前 正数向后
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2018年8月17日 下午3:54:36
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static String addMonth(int add) {
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
+ Calendar c = Calendar.getInstance();
+ c.setTime(new Date());
+ c.add(Calendar.MONTH, add);
+ Date m = c.getTime();
+ return format.format(m);
+ }
+
+ /**
+ *
+ * _获取月份 +-
+ * @param add 负数向前 正数向后
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2018年8月17日 下午3:54:36
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static String addMonth(int add, String dateStr) {
+ Date date = DateTimeUtils.getFormatDate(dateStr, DATE_FORMAT);
+ Calendar c = Calendar.getInstance();
+ c.setTime(date);
+ c.add(Calendar.MONTH, add);
+ Date m = c.getTime();
+ return DateTimeUtils.getFormatDate(m, MONTH_FORMAT);
+ }
+
+ /**
+ *
+ * _获取全月份(yyyymm) +-
+ * @param add 负数向前 正数向后
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2018年8月17日 下午3:54:36
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static String addMonthFormat(int add,String dateFormat) {
+ SimpleDateFormat format = new SimpleDateFormat(dateFormat);
+ Calendar c = Calendar.getInstance();
+ c.setTime(new Date());
+ c.add(Calendar.MONTH, add);
+ Date m = c.getTime();
+ return format.format(m);
+ }
+
+ public static String addWeekFormat(int add,String dateFormat) {
+ SimpleDateFormat format = new SimpleDateFormat(dateFormat);
+ Calendar c = Calendar.getInstance();
+ c.setTime(new Date());
+ c.add(Calendar.WEDNESDAY, add);
+ Date m = c.getTime();
+ return format.format(m);
+ }
+
+ /**
+ *
+ * _Timestamp 转 date
+ * @param timestamp
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2019年11月19日 下午3:51:52
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Date timeToDate(Timestamp timestamp) {
+
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ String dateString = formatter.format(timestamp);
+ // String -> Date
+ Date date = new Date();
+ // 注意format的格式要与日期String的格式相匹配
+ DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ try {
+ date = sdf.parse(dateString);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ return date;
+ }
+ /**
+ *
+ * _日期加减+格式化
+ * @param day
+ * @param format
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2020年1月7日 下午5:13:26
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static String addDayFormat(int day,String format) {
+
+ if(StringUtils.isNotBlank(format)) {
+ return getFormatDate(addDay(day, getCurrDate()), format);
+ }
+ return getFormatDate(addDay(day, getCurrDate()), DATE_FORMAT);
+ }
+ /**
+ *
+ * _当前秒转时间格式
+ * @param seconds
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2020年3月2日 下午12:45:33
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static String timeStamp2Date(int seconds) {
+ SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
+ return sdf.format(new Date(Long.valueOf(seconds+"000")));
+ }
+ /**
+ *
+ * _判断时间季度返回1234
+ * @param date
+ * @return
+ * @throws
+ * @Author:hanwei1@Ifeng.com
+ * @CreateDtae :2020年8月12日 上午11:25:15
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static int getQuarterOfYear(Date date) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(date);
+ return calendar.get(Calendar.MONTH) / 3 + 1;
+ }
+ /**
+ * 得到格式化后的当周第一天,格式为yyyy-MM-dd,如2006-02-01
+ *
+ * @see java.util.Calendar#getMinimum(int)
+ * @see #getFormatDate(java.util.Date, String)
+ * @return String 返回格式化后的当月第一天,格式为yyyy-MM-dd,如2006-02-01
+ */
+ public static String getFirstDayOfWeek() {
+ Calendar cal = Calendar.getInstance();
+// int firstDay = cal.getMinimum(Calendar.DAY_OF_WEEK);
+ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
+ return getFormatDate(cal.getTime(), DATE_FORMAT);
+ }
+
+ /**
+ * 得到格式化后的当周第一天,格式为yyyy-MM-dd,如2006-02-01
+ *
+ * @param currDate
+ * 要格式化的日期
+ * @see java.util.Calendar#getMinimum(int)
+ * @see #getFormatDate(java.util.Date, String)
+ * @return String 返回格式化后的当月第一天,格式为yyyy-MM-dd,如2006-02-01
+ */
+ public static String getFirstDayOfWeek(Date currDate) {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(currDate);
+// int firstDay = cal.getMinimum(Calendar.DAY_OF_WEEK);
+ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
+ return getFormatDate(cal.getTime(), DATE_FORMAT);
+ }
+ /**
+ *
+ * 两个区间内的日期列表以及每天的小时数
+ * 精确到天
+ * @param startDate
+ * @param endDate
+ * @return
+ * @throws
+ * @Author:hanwei
+ * @CreateDate :2022年5月30日 下午3:35:44
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Map getBetweenDate_Day(Date startDate, Date endDate){
+
+ Map dateMap = new HashMap<>();
+ SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
+ try{
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(startDate);
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
+ calendar.set(Calendar.MINUTE, 0);
+ calendar.set(Calendar.SECOND, 0);
+ calendar.set(Calendar.MILLISECOND, 0);
+ while(calendar.getTime().before(endDate) || calendar.getTime().equals(endDate)){
+ dateMap.put(dateFormat.format(calendar.getTime()), 24);
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
+ }
+ }
+ catch(Exception e){
+ e.printStackTrace();
+ }
+ return dateMap;
+ }
+ /**
+ *
+ * 两个区间内的日期列表以及每天的小时数
+ * 精确到天
+ * @param startDate
+ * @param endDate
+ * @return
+ * @throws
+ * @Author:zhuxiangguo
+ * @CreateDate :2022年9月07日
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Map getBetweenDate_Day_Sort(Date startDate,Date endDate){
+
+ Map dateMap = new LinkedHashMap<>();
+ SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
+ try{
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(startDate);
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
+ calendar.set(Calendar.MINUTE, 0);
+ calendar.set(Calendar.SECOND, 0);
+ calendar.set(Calendar.MILLISECOND, 0);
+ while(calendar.getTime().before(endDate) || calendar.getTime().equals(endDate)){
+ dateMap.put(dateFormat.format(calendar.getTime()), 24);
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
+ }
+ }
+ catch(Exception e){
+ e.printStackTrace();
+ }
+ return dateMap;
+ }
+
+ /**
+ *
+ * 两个区间内的日期列表以及每天的小时数
+ * 精确到小时
+ * @param startDate
+ * @param endDate
+ * @return
+ * @throws
+ * @Author:hanwei
+ * @CreateDate :2022年5月30日 下午3:35:44
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static Map getBetweenDate_Hour(Date startDate,Date endDate){
+
+ Map dateMap = new HashMap<>();
+ SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
+ try{
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(startDate);
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
+ calendar.set(Calendar.MINUTE, 0);
+ calendar.set(Calendar.SECOND, 0);
+ calendar.set(Calendar.MILLISECOND, 0);
+ if(dateFormat.format(startDate).equals(dateFormat.format(endDate))){
+ int hour = DateTimeUtils.getHour(startDate, endDate);
+ dateMap.put(dateFormat.format(startDate), hour);
+ return dateMap;
+ }
+ while(calendar.getTime().before(endDate)){
+ // 第一天
+ if(dateFormat.format(calendar.getTime()).equals(dateFormat.format(startDate))){
+ int hour = DateTimeUtils.getHour(calendar.getTime(), startDate);
+ dateMap.put(dateFormat.format(startDate), (24-hour));
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
+ continue;
+ }
+ // 最后一天
+ if(dateFormat.format(calendar.getTime()).equals(dateFormat.format(endDate))){
+ int hour = DateTimeUtils.getHour(calendar.getTime(), endDate);
+ dateMap.put(dateFormat.format(endDate), hour);
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
+ continue;
+ }
+ dateMap.put(dateFormat.format(calendar.getTime()), 24);
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
+ }
+ }
+ catch(Exception e){
+ e.printStackTrace();
+ }
+ return dateMap;
+ }
+ /**
+ *
+ * 双区间段重叠小时数
+ * @param range1Start
+ * @param range1End
+ * @param range2Start
+ * @param range2End
+ * @return
+ * @throws
+ * @Author:hanwei
+ * @CreateDate :2022年5月31日 上午9:30:55
+ * @Update:
+ * @UpdateDate:
+ * @UpdateDescription:
+ */
+ public static int calcOverlapHour(Date range1Start, Date range1End, Date range2Start, Date range2End) {
+
+ long range1StartTime = range1Start.getTime();
+
+ long range1EndTime = range1End.getTime();
+
+ long range2StartTime = range2Start.getTime();
+
+ long range2EndTime = range2End.getTime();
+
+ assert range1StartTime > range1EndTime;
+
+ assert range2StartTime > range2EndTime;
+
+ long overlapTime = Math.min(range1EndTime, range2EndTime)
+ - Math.max(range1StartTime, range2StartTime);
+
+ return (overlapTime < 0) ? 0 : (int) (overlapTime / 60 / 60 / 1000);
+ }
+
+}
diff --git a/agri-common/src/main/java/com/agri/common/utils/RollerTimeCalculator.java b/agri-common/src/main/java/com/agri/common/utils/RollerTimeCalculator.java
new file mode 100644
index 0000000..202ed72
--- /dev/null
+++ b/agri-common/src/main/java/com/agri/common/utils/RollerTimeCalculator.java
@@ -0,0 +1,55 @@
+package com.agri.common.utils;
+
+/**
+ * @ClassName dd
+ * @Description TODO
+ * @Author lld
+ * @Date 2026/3/6 1:44
+ * @Version 1.0
+ */
+
+import org.springframework.beans.factory.annotation.Value;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
+/**
+ * 卷膜时间计算工具类(企业标准写法:单例+常量固化)
+ */
+public class RollerTimeCalculator {
+ // 基础常量:固化到工具类,统一维护
+ @Value("${agri.per-lap.len}")
+ private static BigDecimal perLapLen;
+
+ @Value("${agri.per-lap.sec}")
+ private static BigDecimal perLapSec;
+
+ // 私有化构造器,禁止实例化
+ private RollerTimeCalculator() {}
+
+ /**
+ * 计算卷膜运行时间(秒)
+ * @param targetLen 目标长度(cm,前端统一传入)
+ * @return 运行时间(秒)
+ */
+ public static int calculateRunTime(BigDecimal targetLen) {
+ // 边界处理:长度≤0时返回0
+ if (targetLen == null || targetLen.compareTo(BigDecimal.ZERO)<=0) {
+ return 0;
+ }
+ // 核心公式:时间 = (长度 / 每圈长度) × 每圈时间
+ BigDecimal cycleCount = targetLen.divide(perLapLen,2, RoundingMode.HALF_UP);
+ return cycleCount.multiply(perLapSec).setScale(2, RoundingMode.HALF_UP).intValue(); // 四舍五入取整
+ }
+
+ /**
+ * 进阶:基础参数可配置化(企业进阶方案)
+ * 从配置中心/参数表读取perLapLen、perLapSec,避免硬编码
+ */
+ /* public static long calculateRunTimeWithConfig(double targetLen, String imei) {
+ // 从配置表/缓存读取该设备的单圈长度、单圈时间(适配不同设备参数差异)
+ Double deviceCmPerCycle = ConfigCache.getVal(imei + "_perLapLen", perLapLen);
+ Long deviceSecPerCycle = ConfigCache.getVal(imei + "_sec_per_cycle", perLapSec);
+ return Math.round(targetLen / deviceCmPerCycle * deviceSecPerCycle);
+ }*/
+}
diff --git a/agri-common/src/main/java/com/agri/common/utils/TempJudgeUtil.java b/agri-common/src/main/java/com/agri/common/utils/TempJudgeUtil.java
new file mode 100644
index 0000000..ec0bc00
--- /dev/null
+++ b/agri-common/src/main/java/com/agri/common/utils/TempJudgeUtil.java
@@ -0,0 +1,42 @@
+package com.agri.common.utils;
+
+import com.agri.common.enums.TempCommandStatus;
+
+import java.math.BigDecimal;
+
+/**
+ * 温度判断工具类
+ */
+public class TempJudgeUtil {
+ // 条件参考温度允许的上下误差(抽成常量,后续可配置化)
+ public static final BigDecimal TEMP_ERROR_RANGE = java.math.BigDecimal.ONE;
+
+ /**
+ * 温度判断核心方法:根据当前温度与参考温度的差值,返回指令状态
+ *
+ * @param currentTemp 当前温度(DTU上报的温度)
+ * @param refTemp 条件参考温度(sys_auto_term的temp字段)
+ * @return 指令状态:OPEN(开)/CLOSE(关)/NO_OPERATE(不操作)
+ */
+ public static TempCommandStatus judgeTempCommand(BigDecimal currentTemp, BigDecimal refTemp) {
+ // 1. 空值校验(企业级必备,避免NPE)
+ if (currentTemp == null || refTemp == null) {
+ return TempCommandStatus.NO_OPERATE;
+ }
+
+ // 2. 计算当前温度与参考温度的差值(当前温度 - 参考温度)
+ BigDecimal tempDiff = currentTemp.subtract(refTemp);
+
+ // 3. 按规则判断状态
+ if (tempDiff.compareTo(TEMP_ERROR_RANGE) > 0) {
+ // 当前温度 > 参考温度 + 误差 → 下发开指令
+ return TempCommandStatus.OPEN;
+ } else if (tempDiff.compareTo(TEMP_ERROR_RANGE.negate()) < 0) {
+ // 当前温度 < 参考温度 - 误差 → 下发关指令
+ return TempCommandStatus.CLOSE;
+ } else {
+ // 差值在[-误差, +误差]范围内 → 不操作
+ return TempCommandStatus.NO_OPERATE;
+ }
+ }
+}
diff --git a/agri-common/src/main/java/com/agri/common/utils/TimeConvertUtil.java b/agri-common/src/main/java/com/agri/common/utils/TimeConvertUtil.java
new file mode 100644
index 0000000..5822cbc
--- /dev/null
+++ b/agri-common/src/main/java/com/agri/common/utils/TimeConvertUtil.java
@@ -0,0 +1,41 @@
+package com.agri.common.utils;
+
+/**
+ * @ClassName TimeConvertUtil
+ * @Description TODO
+ * @Author lld
+ * @Date 2026/3/6 2:16
+ * @Version 1.0
+ */
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+public class TimeConvertUtil {
+ // 定义常用时间格式(企业级:固化为常量,避免重复创建)
+ public static final DateTimeFormatter DEFAULT_DATETIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+
+ /**
+ * 字符串转LocalDateTime(适配yyyy-MM-dd HH:mm:ss格式)
+ */
+ public static LocalDateTime strToLocalDateTime(String timeStr) {
+ // 空值/空白校验(企业级必备)
+ if (timeStr == null || timeStr.trim().isEmpty()) {
+ return null;
+ }
+ // 标准格式直接解析
+ return LocalDateTime.parse(timeStr.trim(), DEFAULT_DATETIME_FORMAT);
+ }
+
+ /**
+ * 带异常处理的转换(推荐:避免程序崩溃)
+ */
+ public static LocalDateTime strToLocalDateTimeSafe(String timeStr) {
+ try {
+ return strToLocalDateTime(timeStr);
+ } catch (Exception e) {
+ // 日志记录转换失败原因(企业级:必加)
+ System.err.println("时间字符串转换失败,str=" + timeStr + ",错误:" + e.getMessage());
+ return null;
+ }
+ }
+}
diff --git a/agri-common/src/main/java/com/agri/common/utils/TimeRangeUtil.java b/agri-common/src/main/java/com/agri/common/utils/TimeRangeUtil.java
new file mode 100644
index 0000000..c8566d5
--- /dev/null
+++ b/agri-common/src/main/java/com/agri/common/utils/TimeRangeUtil.java
@@ -0,0 +1,46 @@
+package com.agri.common.utils;
+
+/**
+ * @ClassName TimeRangeUtil
+ * @Description TODO
+ * @Author lld
+ * @Date 2026/3/6 2:09
+ * @Version 1.0
+ */
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+
+public class TimeRangeUtil {
+
+ /**
+ * 判断dtu上报时间的时分秒是否在auto_term的时间区间内(支持跨天)
+ */
+ public static boolean isTimeInRange(LocalDateTime dtuTime, LocalDateTime startTime, LocalDateTime endTime) {
+ if (dtuTime == null || startTime == null || endTime == null) {
+ return false;
+ }
+ LocalTime currentTime = dtuTime.toLocalTime();
+ LocalTime beforeTime = startTime.toLocalTime();
+ LocalTime afterTime = endTime.toLocalTime();
+
+ if (startTime.isBefore(endTime)) {
+ // 正常时段:08:00:00 - 18:00:00
+ return !currentTime.isBefore(beforeTime) && !currentTime.isAfter(afterTime);
+ } else {
+ // 跨天时段:22:00:00 - 06:00:00
+ return currentTime.isAfter(beforeTime) || currentTime.isBefore(afterTime);
+ }
+ }
+
+ /**
+ * 从ts(毫秒时间戳)转换并判断
+ */
+ public static boolean isTsInRange(long ts, LocalDateTime startTime, LocalDateTime endTime) {
+ LocalDateTime dtuTime = LocalDateTime.ofInstant(
+ java.time.Instant.ofEpochMilli(ts),
+ java.time.ZoneId.systemDefault()
+ );
+ return isTimeInRange(dtuTime, startTime, endTime);
+ }
+
+}
diff --git a/agri-framework/src/main/java/com/agri/framework/interceptor/DeviceStatusHandler.java b/agri-framework/src/main/java/com/agri/framework/interceptor/DeviceStatusHandler.java
index 895853d..07fd71e 100644
--- a/agri-framework/src/main/java/com/agri/framework/interceptor/DeviceStatusHandler.java
+++ b/agri-framework/src/main/java/com/agri/framework/interceptor/DeviceStatusHandler.java
@@ -4,14 +4,8 @@ import com.agri.common.utils.wechat.WxUtil;
import com.agri.framework.config.MqttConfig;
import com.agri.framework.manager.MqttAutoOffManager;
import com.agri.framework.manager.MqttSubscriptionManager;
-import com.agri.system.domain.SysAgriLimit;
-import com.agri.system.domain.SysDevOperLog;
-import com.agri.system.service.ISysAgriLimitService;
-import com.agri.system.service.ISysDevOperLogService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
-import org.apache.commons.lang3.ObjectUtils;
-import org.checkerframework.checker.units.qual.A;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -19,17 +13,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
-import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
-import java.util.function.Function;
/**
* 设备状态消息处理器
@@ -237,4 +227,4 @@ public class DeviceStatusHandler {
return false;
}
-}
\ No newline at end of file
+}
diff --git a/agri-quartz/pom.xml b/agri-quartz/pom.xml
index 3433fda..0e9753b 100644
--- a/agri-quartz/pom.xml
+++ b/agri-quartz/pom.xml
@@ -38,7 +38,11 @@
com.agri
agri-system
+
+ com.agri
+ agri-framework
+
-
\ No newline at end of file
+
diff --git a/agri-quartz/src/main/java/com/agri/quartz/task/RollerAutoTask.java b/agri-quartz/src/main/java/com/agri/quartz/task/RollerAutoTask.java
index cc71ff4..d3a8a37 100644
--- a/agri-quartz/src/main/java/com/agri/quartz/task/RollerAutoTask.java
+++ b/agri-quartz/src/main/java/com/agri/quartz/task/RollerAutoTask.java
@@ -1,7 +1,25 @@
package com.agri.quartz.task;
+import com.agri.common.enums.TempCommandStatus;
+import com.agri.common.utils.TempJudgeUtil;
+import com.agri.common.utils.TimeConvertUtil;
+import com.agri.common.utils.TimeRangeUtil;
+import com.agri.system.domain.SysAgriInfo;
+import com.agri.system.domain.vo.RollerTermVO;
+import com.agri.system.service.ISysAgriInfoService;
+import com.agri.system.service.ISysDtuDataService;
+import com.agri.system.service.ISysRollerParamService;
+import org.apache.commons.collections4.CollectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.time.LocalDateTime;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
/**
* 卷膜自动模式定时检查
* 每五分钟执行一次
@@ -14,4 +32,98 @@ import org.springframework.stereotype.Component;
@Component("rollerAutoTask")
public class RollerAutoTask {
+ @Autowired
+ private ISysAgriInfoService agriInfoService;
+
+ @Autowired
+ private ISysDtuDataService dtuDataService;
+
+ @Autowired
+ private ISysRollerParamService rollerParamService;
+
+ @Resource
+ private MqttConfig.MqttMessageSender mqttMessageSender;
+
+
+ public void checkAutoTerm() {
+
+ // 查询自动模式的大棚
+ List agriInfos = agriInfoService.lambdaQuery()
+ .select(SysAgriInfo::getImei)
+ .eq(SysAgriInfo::getWorkMode, 1)
+ .eq(SysAgriInfo::getIsDeleted, 0)
+ .list();
+ if (CollectionUtils.isEmpty(agriInfos)) return;
+ // 取imei集合
+ List imeiList = agriInfos.stream().map(SysAgriInfo::getImei).collect(Collectors.toList());
+ if (CollectionUtils.isEmpty(imeiList)) return;
+
+ // 根据imei 查询dtu_data最后一条温度数据
+ List