Просмотр исходного кода

更新日期大小比较方法

hejq 7 лет назад
Родитель
Сommit
1ddbb7cc93

+ 74 - 0
src/main/java/com/uas/platform/b2b/core/util/DateUtils.java

@@ -15,11 +15,40 @@ import java.util.GregorianCalendar;
  */
 public class DateUtils {
 
+    /**
+     * 时间比较,默认
+     */
 	public static final int COMPARE_NONE = 0;
+
+    /**
+     * 时间比较,精确到秒
+     */
 	public static final int COMPARE_SECOND = 1;
+
+    /**
+     * 时间比较,精确到分钟
+     */
 	public static final int COMPARE_MINUTE = 2;
+
+    /**
+     * 时间比较,精确到小时
+     */
 	public static final int COMPARE_HOUR = 3;
+
+    /**
+     * 时间比较,精确到天
+     */
 	public static final int COMPARE_DAY = 4;
+
+    /**
+     * 时间比较,精确到月份
+     */
+    public static final int COMPARE_MONTH = 5;
+
+    /**
+     * 时间比较,精确到年份
+     */
+    public static final int COMPARE_YEAR = 6;
 	static final SimpleDateFormat ym = new SimpleDateFormat("yyyyMM");
 	static final SimpleDateFormat YM = new SimpleDateFormat("yyyy-MM");
     public static final SimpleDateFormat YMD = new SimpleDateFormat("yyyy-MM-dd");
@@ -27,6 +56,9 @@ public class DateUtils {
     static final FastDateFormat MM = FastDateFormat.getInstance("MM");
     static final FastDateFormat YYYY = FastDateFormat.getInstance("yyyy");
     static final FastDateFormat DD = FastDateFormat.getInstance("dd");
+    static final SimpleDateFormat COMPARE_HOUR_PATTERN = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
+    static final SimpleDateFormat COMPARE_MINUTE_PATTERN = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
+    static final SimpleDateFormat COMPARE_SECOND_PATTERN = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
     /**
      * 获取月份
@@ -269,4 +301,46 @@ public class DateUtils {
         calendar.add(Calendar.MONTH, increase);
         return calendar.getTime();
     }
+
+    /**
+     * 比较日期大小
+     *
+     * @param firstDate 时间1
+     * @param secondDate 时间2
+     * @param compareType 比较类型
+     *            精确位(天、小时、分钟、秒)
+     * @return 1:firstDate > secondDate; 0: firstDate = secondDate; -1: firstDate < secondDate;
+     */
+    public static int dateCompare(Date firstDate, Date secondDate, int compareType) {
+        String firstDateParam;
+        String secondDateParam;
+        if (COMPARE_NONE == compareType) {
+            return firstDate.compareTo(secondDate);
+        } else if (COMPARE_SECOND == compareType) {
+            firstDateParam = COMPARE_SECOND_PATTERN.format(firstDate);
+            secondDateParam = COMPARE_SECOND_PATTERN.format(secondDate);
+            return firstDateParam.compareTo(secondDateParam);
+        } else if (COMPARE_MINUTE == compareType) {
+            firstDateParam = COMPARE_MINUTE_PATTERN.format(firstDate);
+            secondDateParam = COMPARE_MINUTE_PATTERN.format(secondDate);
+            return firstDateParam.compareTo(secondDateParam);
+        } else if (COMPARE_HOUR == compareType) {
+            firstDateParam = COMPARE_HOUR_PATTERN.format(firstDate);
+            secondDateParam = COMPARE_HOUR_PATTERN.format(secondDate);
+            return firstDateParam.compareTo(secondDateParam);
+        } else if (COMPARE_DAY == compareType) {
+            firstDateParam = YMD.format(firstDate);
+            secondDateParam = YMD.format(secondDate);
+            return firstDateParam.compareTo(secondDateParam);
+        } else if (COMPARE_MONTH == compareType) {
+            firstDateParam = YM.format(firstDate);
+            secondDateParam = YM.format(secondDate);
+            return firstDateParam.compareTo(secondDateParam);
+        } else if (COMPARE_YEAR == compareType) {
+            firstDateParam = YYYY.format(firstDate);
+            secondDateParam = YYYY.format(secondDate);
+            return firstDateParam.compareTo(secondDateParam);
+        }
+        return 0;
+    }
 }

+ 21 - 21
src/main/java/com/uas/platform/b2b/task/SendNoticeTask.java

@@ -7,6 +7,7 @@ import com.uas.platform.b2b.dao.PurchaseOrderDao;
 import com.uas.platform.b2b.model.CommunalLog;
 import com.uas.platform.core.model.Constant;
 import com.uas.platform.core.util.HttpUtil;
+import org.apache.commons.lang3.time.FastDateFormat;
 import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -137,25 +138,26 @@ public class SendNoticeTask {
      * <pre>
      *  1.判断当前是否属于同一个年份
      *      1.1.属于同一年
-     *          1.1.1.物料备料提前期为-1
-     *              1.1.1.1.如果交货期为当月或之前月份,可以发货
-     *              1.1.1.2.如果交货期为下月
-     *                  1.1.1.2.1.如果当前日期大于25,可以发货
-     *                  1.1.1.2.2.如果当前日期小于或等于25,不可以发货
-     *              1.1.1.3.如果交货日期大于下月,不可以发货
-     *          1.1.2.物料备料提前期为具体天数
-     *              1.1.2.1.交货日期小于备料提前期 + 当前日期,可以发货
-     *              1.1.2.2.其他状况不可以发货
+     *          物料备料提前期为-1
+     *             如果交货期为当月或之前月份,可以发货
+     *              如果交货期为下月
+     *                 如果当前日期大于25,可以发货
+     *                 如果当前日期小于或等于25,不可以发货
+     *              如果交货日期大于下月,不可以发货
+     *          物料备料提前期为具体天数
+     *              交货日期小于备料提前期 + 当前日期,可以发货
+     *              其他状况不可以发货
      *      1.2.不属于同一年
-     *          1.2.1.如果备料提前期大于或等于0
-     *              1.2.1.1.当前日期+备料提前期+1大于收货日期,可以发货
-     *              1.2.1.2.其他条件不能发货
+     *          如果备料提前期大于或等于0
+     *          当前日期+备料提前期大于收货日期,可以发货
+     *          其他条件不能发货
      *
      * </pre>
-     *
+     * <a herf="https://static.usoftchina.com//appb2c/5b20e18e-7bc3-4813-b479-98e7b747217a.pdf">逻辑附件</a>
      * @param delivery 发货日期
      * @param stock 备料提前期
      * @return 1: 等待发货 0: 可以发货
+     *
      */
     private static Short checkNoticeWaitStatus(Date delivery, Integer stock) {
         Integer nowYear = DateUtils.getYear(new Date());
@@ -180,7 +182,7 @@ public class SendNoticeTask {
                 }
             } else if (stock >= 0) {
                 Date resultDay = DateUtils.addDay(new Date(), stock);
-                if (DateUtils.compare(resultDay, delivery, DateUtils.COMPARE_DAY) == 1) {
+                if (DateUtils.dateCompare(resultDay, delivery, DateUtils.COMPARE_DAY) >= 0) {
                     return Constant.NO;
                 } else {
                     return Constant.YES;
@@ -188,8 +190,8 @@ public class SendNoticeTask {
             }
         } else {
             if (stock >= 0) {
-                Date resultDay = DateUtils.addDay(new Date(), stock + 1);
-                if (DateUtils.compare(resultDay, delivery, DateUtils.COMPARE_DAY) == 1) {
+                Date resultDay = DateUtils.addDay(new Date(), stock);
+                if (DateUtils.dateCompare(resultDay, delivery, DateUtils.COMPARE_DAY) >= 0) {
                     return Constant.NO;
                 } else {
                     return Constant.YES;
@@ -200,10 +202,8 @@ public class SendNoticeTask {
     }
 
     public static void main(String[] args) throws ParseException {
-        Long start = System.currentTimeMillis();
-        Date date = DateUtils.YMD.parse("2019-01-06");
-        System.out.println(checkNoticeWaitStatus(date, 60));
-        Long cost = System.currentTimeMillis() - start;
-        System.out.println("cost: " + cost);
+        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+        Short status = checkNoticeWaitStatus(dateFormat.parse("2019-01-11"), 32);
+        System.out.println(status);
     }
 }