mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
660c41b972
commit
62c87b28cd
@ -17,9 +17,8 @@ import org.dromara.hutool.core.comparator.CompareUtil;
|
||||
import org.dromara.hutool.core.date.format.DatePrinter;
|
||||
import org.dromara.hutool.core.date.format.FastDateFormat;
|
||||
import org.dromara.hutool.core.date.format.GlobalCustomFormat;
|
||||
import org.dromara.hutool.core.date.format.parser.GlobalRegexDateParser;
|
||||
import org.dromara.hutool.core.date.format.parser.PositionDateParser;
|
||||
import org.dromara.hutool.core.date.format.parser.TimeParser;
|
||||
import org.dromara.hutool.core.date.format.parser.RegisterDateParser;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.text.split.SplitUtil;
|
||||
@ -802,11 +801,7 @@ public class DateUtil {
|
||||
* @return 日期
|
||||
*/
|
||||
public static DateTime parse(final CharSequence dateCharSequence) {
|
||||
if (TimeParser.INSTANCE.test(dateCharSequence)) {
|
||||
// 独立解析时间,则默认使用今天的日期
|
||||
return TimeParser.INSTANCE.parse(dateCharSequence);
|
||||
}
|
||||
return GlobalRegexDateParser.parse(dateCharSequence);
|
||||
return (DateTime) RegisterDateParser.INSTANCE.parse(dateCharSequence);
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@ -23,11 +23,45 @@ import java.util.regex.Pattern;
|
||||
* @author Looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class GlobalRegexDateParser {
|
||||
public class DefaultRegexDateParser implements PredicateDateParser {
|
||||
|
||||
private static final RegexDateParser PARSER;
|
||||
/**
|
||||
* 默认实例
|
||||
*/
|
||||
public static DefaultRegexDateParser INSTANCE = new DefaultRegexDateParser();
|
||||
|
||||
static {
|
||||
private final RegexDateParser parser;
|
||||
|
||||
/**
|
||||
* 构造,初始化默认的解析规则
|
||||
*/
|
||||
public DefaultRegexDateParser() {
|
||||
parser = createDefault();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(final CharSequence charSequence) {
|
||||
// 此类用于托底,当自定义规则无法匹配时,一律使用此规则
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析日期,此方法线程安全
|
||||
*
|
||||
* @param source 日期字符串
|
||||
* @return 日期
|
||||
*/
|
||||
@Override
|
||||
public DateTime parse(final CharSequence source) {
|
||||
return (DateTime) parser.parse(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建默认的 正则日期解析器
|
||||
*
|
||||
* @return {@link RegexDateParser}
|
||||
*/
|
||||
private RegexDateParser createDefault() {
|
||||
final String yearRegex = "(?<year>\\d{2,4})";
|
||||
// 月的正则,匹配:Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec,
|
||||
// 或 January, February, March, April, May, June, July, August, September, October, November, December
|
||||
@ -64,7 +98,7 @@ public class GlobalRegexDateParser {
|
||||
+ ")";
|
||||
final String maskRegex = "(\\smsk m=[+-]\\d[.]\\d+)?";
|
||||
|
||||
PARSER = RegexDateParser.of(
|
||||
return RegexDateParser.of(
|
||||
//【年月日时】类似:2009-Feb-08,时间部分可选,类似:5:57:51,05:57:51 +08:00
|
||||
yearRegex + "\\W" + dateRegexMonthFirst + timeRegexWithPre + zoneRegex + maskRegex,
|
||||
//【年月日时】类似:2009-02-08或2014年04月08日,时间部分可选,类似:5:57:51,05:57:51 +08:00
|
||||
@ -92,23 +126,13 @@ public class GlobalRegexDateParser {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析日期,此方法线程安全
|
||||
*
|
||||
* @param source 日期字符串
|
||||
* @return 日期
|
||||
*/
|
||||
public static DateTime parse(final CharSequence source) {
|
||||
return (DateTime) PARSER.parse(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当用户传入的月和日无法判定默认位置时,设置默认的日期格式为dd/mm还是mm/dd
|
||||
*
|
||||
* @param preferMonthFirst {@code true}默认为mm/dd,否则dd/mm
|
||||
*/
|
||||
synchronized public static void setPreferMonthFirst(final boolean preferMonthFirst) {
|
||||
PARSER.setPreferMonthFirst(preferMonthFirst);
|
||||
synchronized public void setPreferMonthFirst(final boolean preferMonthFirst) {
|
||||
parser.setPreferMonthFirst(preferMonthFirst);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,8 +140,8 @@ public class GlobalRegexDateParser {
|
||||
*
|
||||
* @param regex 日期正则
|
||||
*/
|
||||
synchronized public static void registerRegex(final String regex) {
|
||||
PARSER.addRegex(regex);
|
||||
synchronized public void registerRegex(final String regex) {
|
||||
parser.addRegex(regex);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,7 +149,7 @@ public class GlobalRegexDateParser {
|
||||
*
|
||||
* @param pattern 日期正则
|
||||
*/
|
||||
synchronized public static void registerPattern(final Pattern pattern) {
|
||||
PARSER.addPattern(pattern);
|
||||
synchronized public void registerPattern(final Pattern pattern) {
|
||||
parser.addPattern(pattern);
|
||||
}
|
||||
}
|
@ -29,19 +29,10 @@ public class RegisterDateParser implements DateParser, Serializable {
|
||||
*/
|
||||
public RegisterDateParser() {
|
||||
parserList = ListUtil.of(
|
||||
// 纯数字形式
|
||||
PureDateParser.INSTANCE,
|
||||
// HH:mm:ss 或者 HH:mm 时间格式匹配单独解析
|
||||
TimeParser.INSTANCE,
|
||||
// JDK的Date对象toString默认格式,类似于:
|
||||
// Tue Jun 4 16:25:15 +0800 2019
|
||||
// Thu May 16 17:57:18 GMT+08:00 2019
|
||||
// Wed Aug 01 00:00:00 CST 2012
|
||||
RFC2822DateParser.INSTANCE,
|
||||
// ISO8601标准时间
|
||||
// yyyy-MM-dd'T'HH:mm:ss'Z'
|
||||
// yyyy-MM-dd'T'HH:mm:ss+0800
|
||||
ISO8601DateParser.INSTANCE
|
||||
// 默认的正则解析器
|
||||
DefaultRegexDateParser.INSTANCE
|
||||
);
|
||||
}
|
||||
|
||||
@ -62,7 +53,8 @@ public class RegisterDateParser implements DateParser, Serializable {
|
||||
* @return this
|
||||
*/
|
||||
public RegisterDateParser register(final PredicateDateParser dateParser) {
|
||||
this.parserList.add(dateParser);
|
||||
// 用户定义的规则优先
|
||||
this.parserList.add(0, dateParser);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
/**
|
||||
* https://gitee.com/dromara/hutool/issues/I8IUTB
|
||||
*/
|
||||
public class GlobalRegexDateParserTest {
|
||||
public class DefaultRegexDateParserTest {
|
||||
|
||||
@Test
|
||||
void parseYearMonthDaySplitByDashedTest() {
|
||||
@ -182,7 +182,7 @@ public class GlobalRegexDateParserTest {
|
||||
}
|
||||
|
||||
private static void assertParse(final String dateStr, final String dateStrToParse) {
|
||||
final Date date = GlobalRegexDateParser.parse(dateStrToParse);
|
||||
final Date date = DefaultRegexDateParser.INSTANCE.parse(dateStrToParse);
|
||||
assertEquals(dateStr, DateUtil.date(date).toString());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user