mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix DatePattern
This commit is contained in:
parent
327502a846
commit
411fea9c9f
@ -10,6 +10,7 @@
|
||||
* 【setting】 Props异常规则修改(issue#907@Github)
|
||||
* 【setting】 增加GIF支持
|
||||
* 【core 】 复制创建一个Bean对象, 并忽略某些属性(pr#130@Gitee)
|
||||
* 【core 】 DateUtil.parse支持更多日期格式(issue#I1KHTB@Gitee)
|
||||
|
||||
### Bug修复
|
||||
* 【json 】 修复append方法导致的JSONConfig传递失效问题(issue#906@Github)
|
||||
|
@ -14,6 +14,6 @@
|
||||
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<description>Hutool核心</description>
|
||||
<description>Hutool核心,包括集合、字符串、Bean等工具</description>
|
||||
|
||||
</project>
|
||||
|
@ -17,13 +17,14 @@ public class DatePattern {
|
||||
/**
|
||||
* 标准日期时间正则,每个字段支持单个数字或2个数字,包括:
|
||||
* <pre>
|
||||
* yyyy-MM-dd HH:mm:ss.SSS
|
||||
* yyyy-MM-dd HH:mm:ss
|
||||
* yyyy-MM-dd HH:mm
|
||||
* yyyy-MM-dd
|
||||
* </pre>
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static final Pattern REGEX_NORM = Pattern.compile("\\d{4}-\\d{1,2}-\\d{1,2}( \\d{1,2}:\\d{1,2}(:\\d{1,2})?)?");
|
||||
public static final Pattern REGEX_NORM = Pattern.compile("\\d{4}-\\d{1,2}-\\d{1,2}(\\s\\d{1,2}:\\d{1,2}(:\\d{1,2})?)?(.\\d{1,3})?");
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------- Normal
|
||||
/** 标准日期格式:yyyy-MM-dd */
|
||||
|
@ -35,6 +35,7 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
/**
|
||||
* 时间工具类
|
||||
@ -911,6 +912,7 @@ public class DateUtil extends CalendarUtil {
|
||||
|
||||
//标准日期格式(包括单个数字的日期时间)
|
||||
dateStr = normalize(dateStr);
|
||||
final Matcher matcher = DatePattern.REGEX_NORM.matcher(dateStr);
|
||||
if (ReUtil.isMatch(DatePattern.REGEX_NORM, dateStr)) {
|
||||
final int colonCount = StrUtil.count(dateStr, CharUtil.COLON);
|
||||
switch (colonCount) {
|
||||
@ -921,17 +923,15 @@ public class DateUtil extends CalendarUtil {
|
||||
// yyyy-MM-dd HH:mm
|
||||
return parse(dateStr, DatePattern.NORM_DATETIME_MINUTE_FORMAT);
|
||||
case 2:
|
||||
if(StrUtil.contains(dateStr, CharUtil.DOT)){
|
||||
// yyyy-MM-dd HH:mm:ss.SSS
|
||||
return parse(dateStr, DatePattern.NORM_DATETIME_MS_FORMAT);
|
||||
}
|
||||
// yyyy-MM-dd HH:mm:ss
|
||||
return parse(dateStr, DatePattern.NORM_DATETIME_FORMAT);
|
||||
}
|
||||
}
|
||||
|
||||
// 长度判断
|
||||
if (length >= DatePattern.NORM_DATETIME_MS_PATTERN.length() - 2) {
|
||||
//yyyy-MM-dd HH:mm:ss.SSS
|
||||
return parse(dateStr, DatePattern.NORM_DATETIME_MS_FORMAT);
|
||||
}
|
||||
|
||||
// 没有更多匹配的时间格式
|
||||
throw new DateException("No format fit for date String [{}] !", dateStr);
|
||||
}
|
||||
|
@ -779,6 +779,9 @@ public class DateUtilTest {
|
||||
Assert.assertEquals("2020-05-08 03:02:03", dateTime.toString());
|
||||
dateTime = DateUtil.parse("2020-5-8 3:12:13");
|
||||
Assert.assertEquals("2020-05-08 03:12:13", dateTime.toString());
|
||||
|
||||
dateTime = DateUtil.parse("2020-5-8 4:12:26.223");
|
||||
Assert.assertEquals("2020-05-08 04:12:26", dateTime.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
|
Loading…
x
Reference in New Issue
Block a user