Sfoglia il codice sorgente

excel导入修复,LocalDateTime无法读取纯日期的情况

欧阳劲驰 3 settimane fa
parent
commit
e9975af3f4

+ 20 - 11
bespoke-gateway-core/src/main/java/com/shkpr/service/bespokegateway/core/utils/ExcelUtil.java

@@ -23,7 +23,10 @@ import java.lang.reflect.InvocationTargetException;
 import java.math.BigDecimal;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.text.SimpleDateFormat;
+import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -586,7 +589,7 @@ public class ExcelUtil {
             }
 
             //字段类型为时间
-            else if (field.getType() == LocalDateTime.class || field.getType() == java.time.LocalDate.class || field.getType() == java.time.LocalTime.class ||
+            else if (field.getType() == LocalDateTime.class || field.getType() == LocalDate.class || field.getType() == java.time.LocalTime.class ||
                     field.getType() == java.util.Date.class || field.getType() == java.sql.Date.class || field.getType() == java.sql.Timestamp.class) {
                 //常见的日期时间格式
                 List<String> patterns = Arrays.asList(
@@ -599,32 +602,38 @@ public class ExcelUtil {
                 for (String pattern : patterns) {
                     try {
                         if (field.getType() == LocalDateTime.class) {
-                            java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
-                            LocalDateTime dateTime = LocalDateTime.parse(value, formatter);
-                            field.set(data, dateTime);
+                            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
+                            // 根据pattern判断是日期时间还是纯日期
+                            if (pattern.contains("HH") || pattern.contains("HH:mm") || pattern.contains("HH:mm:ss") || pattern.contains("'T'")) {
+                                LocalDateTime dateTime = LocalDateTime.parse(value, formatter);
+                                field.set(data, dateTime);
+                            } else {
+                                LocalDate localDate = LocalDate.parse(value, formatter);
+                                field.set(data, localDate.atStartOfDay());
+                            }
                             return;
-                        } else if (field.getType() == java.time.LocalDate.class) {
-                            java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
-                            java.time.LocalDate localDate = java.time.LocalDate.parse(value, formatter);
+                        } else if (field.getType() == LocalDate.class) {
+                            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
+                            LocalDate localDate = LocalDate.parse(value, formatter);
                             field.set(data, localDate);
                             return;
                         } else if (field.getType() == java.time.LocalTime.class) {
-                            java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
+                            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
                             java.time.LocalTime localTime = java.time.LocalTime.parse(value, formatter);
                             field.set(data, localTime);
                             return;
                         } else if (field.getType() == java.util.Date.class) {
-                            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
+                            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                             java.util.Date date = sdf.parse(value);
                             field.set(data, date);
                             return;
                         } else if (field.getType() == java.sql.Date.class) {
-                            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
+                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                             java.util.Date utilDate = sdf.parse(value);
                             field.set(data, new java.sql.Date(utilDate.getTime()));
                             return;
                         } else if (field.getType() == java.sql.Timestamp.class) {
-                            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                             java.util.Date utilDate = sdf.parse(value);
                             field.set(data, new java.sql.Timestamp(utilDate.getTime()));
                             return;