ly 2 лет назад
Родитель
Сommit
92a227a5df

+ 61 - 0
master/src/main/java/com/ruoyi/common/utils/DateUtils.java

@@ -8,6 +8,7 @@ import java.text.SimpleDateFormat;
 import java.time.*;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.regex.Pattern;
 
 /**
  * 时间工具类
@@ -188,4 +189,64 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
         now.add(Calendar.DATE,-1);
        return now.getTime();
     }
+
+    /**
+     * 常规自动日期格式识别
+     * @param str 时间字符串
+     * @return Date
+     * @author dc
+     */
+    public static String getDateFormat(String str) throws ParseException {
+        boolean year = false;
+        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
+        if (pattern.matcher(str.substring(0, 4)).matches()) {
+            year = true;
+        }
+        StringBuilder sb = new StringBuilder();
+        int index = 0;
+        if (!year) {
+            if (str.contains("月") || str.contains("-") || str.contains("/") || str.contains(".")) {
+                if (Character.isDigit(str.charAt(0))) {
+                    index = 1;
+                }
+            } else {
+                index = 3;
+            }
+        }
+        for (int i = 0; i < str.length(); i++) {
+            char chr = str.charAt(i);
+            if (Character.isDigit(chr)) {
+                if (index == 0) {
+                    sb.append("y");
+                }
+                if (index == 1) {
+                    sb.append("M");
+                }
+                if (index == 2) {
+                    sb.append("d");
+                }
+                if (index == 3) {
+                    sb.append("H");
+                }
+                if (index == 4) {
+                    sb.append("m");
+                }
+                if (index == 5) {
+                    sb.append("s");
+                }
+                if (index == 6) {
+                    sb.append("S");
+                }
+            } else {
+                if (i > 0) {
+                    char lastChar = str.charAt(i - 1);
+                    if (Character.isDigit(lastChar)) {
+                        index++;
+                    }
+                }
+                sb.append(chr);
+            }
+        }
+        return sb.toString();
+    }
 }

+ 2 - 1
master/src/main/java/com/ruoyi/project/base/controller/TBaseDeviceController.java

@@ -9,6 +9,7 @@ import javax.servlet.http.HttpServletResponse;
 
 import com.alibaba.fastjson2.JSON;
 import com.ruoyi.common.core.domain.entity.SysDictData;
+import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.file.ExcelUtils;
 import com.ruoyi.project.base.domain.TBasePlant;
 import com.ruoyi.project.base.domain.TBaseRegion;
@@ -203,7 +204,7 @@ public class TBaseDeviceController extends BaseController
                         entity.setResponseFactorFrom(cellValue);
                     } else if (j == 8) {
                         // 维护日期
-                        entity.setUpdatedate(sdf.parse(cellValue));
+                        entity.setUpdatedate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                     } else if (j == 9) {
                         // 备注
                         entity.setRemarks(cellValue);

+ 2 - 1
master/src/main/java/com/ruoyi/project/base/controller/TBasePointController.java

@@ -8,6 +8,7 @@ import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.domain.entity.SysDictData;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
+import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.ExcelUtils;
 import com.ruoyi.common.utils.file.FileUploadUtils;
@@ -312,7 +313,7 @@ public class TBasePointController extends BaseController {
                         } else if (j == 27) {
                             // 运行时间
                             if (cellValue.length() > 3) {//下次年度检验日期
-                                entity.setRunTime(new SimpleDateFormat("yyyy-MM-dd").parse(cellValue));
+                                entity.setRunTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                             }
                         } else if (j == 28) {
                             // 备注

+ 2 - 2
master/src/main/java/com/ruoyi/project/check/controller/TCheckCheckpointsController.java

@@ -6,6 +6,7 @@ import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
+import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.ExcelUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -210,8 +211,7 @@ public class TCheckCheckpointsController extends BaseController {
                             entity.setChecker(cellValue);
                         } else if (j == 7) {
                             // 检测日期
-                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
-                            entity.setCheckDate(sdf.parse(cellValue));
+                            entity.setCheckDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                         }
                     }
                     entity.setUpdatedate(new Date());

+ 2 - 2
master/src/main/java/com/ruoyi/project/check/controller/TCheckRepairpointsController.java

@@ -14,6 +14,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 import javax.servlet.http.HttpServletResponse;
 
 import com.alibaba.fastjson2.JSON;
+import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.ExcelUtils;
 import com.ruoyi.project.base.domain.TBasePoint;
@@ -222,8 +223,7 @@ public class TCheckRepairpointsController extends BaseController {
                             entity.setRepairer(cellValue);
                         } else if (j == 5) {
                             // 维修日期
-                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
-                            entity.setRepairDate(sdf.parse(cellValue));
+                            entity.setRepairDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
                         } else if (j == 6) {
                             // 描述
                             entity.setRemarks(cellValue);