add RegexListDateParser

This commit is contained in:
Looly 2024-06-11 19:00:21 +08:00
parent 21be35c9c3
commit c1df666c1a
2 changed files with 113 additions and 4 deletions

View File

@ -43,7 +43,17 @@ public class RegexDateParser implements PredicateDateParser {
* @return RegexDateParser
*/
public static RegexDateParser of(final String regex) {
return new RegexDateParser(PatternPool.get(regex));
return of(PatternPool.get(regex));
}
/**
* 根据给定带名称的分组正则创建RegexDateParser
*
* @param pattern 正则表达式
* @return RegexDateParser
*/
public static RegexDateParser of(final Pattern pattern) {
return new RegexDateParser(pattern);
}
private final Pattern pattern;
@ -65,10 +75,21 @@ public class RegexDateParser implements PredicateDateParser {
@Override
public Date parse(final CharSequence source) throws DateException {
final Matcher matcher = this.pattern.matcher(source);
if(!matcher.matches()){
if (!matcher.matches()) {
throw new DateException("Invalid date string: [{}], not match the date regex: [{}].", source, this.pattern.pattern());
}
return parse(matcher);
}
/**
* 解析日期
*
* @param matcher 正则匹配器
* @return 日期
* @throws DateException 日期解析异常
*/
public static Date parse(final Matcher matcher) throws DateException {
// 毫秒时间戳
final String millisecond = ReUtil.group(matcher, "millisecond");
if (StrUtil.isNotEmpty(millisecond)) {
@ -147,7 +168,7 @@ public class RegexDateParser implements PredicateDateParser {
throw new DateException("Invalid month: [{}]", month);
}
private static int parseWeek(final String week){
private static int parseWeek(final String week) {
return Week.of(week).getIso8601Value();
}
@ -184,10 +205,11 @@ public class RegexDateParser implements PredicateDateParser {
/**
* 解析时区偏移类似于'+0800', '+08', '+8:00', '+08:00'
*
* @param zoneOffset 时区偏移
* @return 偏移量
*/
private int parseZoneOffset(final String zoneOffset) {
private static int parseZoneOffset(final String zoneOffset) {
int from = 0;
final int to = zoneOffset.length();
final boolean neg = '-' == zoneOffset.charAt(from);

View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.date.format.parser;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.date.DateException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 使用正则列表方式的日期解析器<br>
* 通过定义若干的日期正则遍历匹配到给定正则后按照正则方式解析为日期
*
* @author Looly
*/
public class RegexListDateParser implements DateParser, Serializable {
private static final long serialVersionUID = 1L;
/**
* 根据给定的正则列表创建RegexListDateParser
*
* @param patterns 正则列表
* @return RegexListDateParser
*/
public static RegexListDateParser of(final Pattern... patterns) {
return new RegexListDateParser(ListUtil.of(patterns));
}
private final List<Pattern> patterns;
/**
* 构造
*
* @param patterns 正则列表
*/
public RegexListDateParser(final List<Pattern> patterns) {
this.patterns = patterns;
}
/**
* 新增自定义日期正则
*
* @param regex 日期正则
* @return this
*/
public RegexListDateParser addRegex(final String regex) {
return addPattern(Pattern.compile(regex));
}
/**
* 新增自定义日期正则
*
* @param pattern 日期正则
* @return this
*/
public RegexListDateParser addPattern(final Pattern pattern) {
this.patterns.add(pattern);
return this;
}
@Override
public Date parse(final CharSequence source) throws DateException {
Matcher matcher;
for (final Pattern pattern : this.patterns) {
matcher = pattern.matcher(source);
if (matcher.matches()) {
return RegexDateParser.parse(matcher);
}
}
throw new DateException("No valid pattern for date string: [{}]", source);
}
}