Browse Source

toDate、toLocalDateTime 支持月、日、小时、分、秒都允许是一位数,例如:2022-1-2 3:4:5

James 3 years ago
parent
commit
6cc8da0f00
1 changed files with 22 additions and 6 deletions
  1. 22 6
      src/main/java/com/jfinal/kit/TypeKit.java

+ 22 - 6
src/main/java/com/jfinal/kit/TypeKit.java

@@ -145,10 +145,18 @@ public class TypeKit {
 			String s = (String)d;
 			if (s.length() <= dateLen) {
 				return TimeKit.parse(s, datePattern);
-			} else if (s.length() <= dateTimeWithoutSecondLen) {
-				return TimeKit.parse(s, dateTimeWithoutSecondPattern);
-			} else {
+			} else if (s.length() > dateTimeWithoutSecondLen) {
 				return TimeKit.parse(s, dateTimePattern);
+			} else {
+				// 判断冒号字符是否出现两次,月、日、小时、分、秒都允许是一位数,例如:2022-1-2 3:4:5
+				int index = s.indexOf(':');
+				if (index != -1) {
+					if (index != s.lastIndexOf(':')) {
+						return TimeKit.parse(s, dateTimePattern);	
+					} else {
+						return TimeKit.parse(s, dateTimeWithoutSecondPattern);
+					}
+				}
 			}
 		}
 		
@@ -174,10 +182,18 @@ public class TypeKit {
 			String s = (String)ldt;
 			if (s.length() <= dateLen) {
 				return TimeKit.parseLocalDateTime(s, datePattern);
-			} else if (s.length() <= dateTimeWithoutSecondLen) {
-				return TimeKit.parseLocalDateTime(s, dateTimeWithoutSecondPattern);
-			} else {
+			} else if (s.length() > dateTimeWithoutSecondLen) {
 				return TimeKit.parseLocalDateTime(s, dateTimePattern);
+			} else {
+				// 判断冒号字符是否出现两次,月、日、小时、分、秒都允许是一位数,例如:2022-1-2 3:4:5
+				int index = s.indexOf(':');
+				if (index != -1) {
+					if (index != s.lastIndexOf(':')) {
+						return TimeKit.parseLocalDateTime(s, dateTimePattern);
+					} else {
+						return TimeKit.parseLocalDateTime(s, dateTimeWithoutSecondPattern);
+					}
+				}
 			}
 		}