mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-30 04:45:55 +08:00
fix code
This commit is contained in:
parent
c1df666c1a
commit
59dc7dec39
@ -14,7 +14,6 @@ package org.dromara.hutool.core.date.format.parser;
|
||||
|
||||
import org.dromara.hutool.core.date.*;
|
||||
import org.dromara.hutool.core.lang.Opt;
|
||||
import org.dromara.hutool.core.regex.PatternPool;
|
||||
import org.dromara.hutool.core.regex.ReUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
|
||||
@ -43,7 +42,8 @@ public class RegexDateParser implements PredicateDateParser {
|
||||
* @return RegexDateParser
|
||||
*/
|
||||
public static RegexDateParser of(final String regex) {
|
||||
return of(PatternPool.get(regex));
|
||||
// 日期正则忽略大小写
|
||||
return of(Pattern.compile(regex, Pattern.CASE_INSENSITIVE));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,7 +58,8 @@ public class RegexListDateParser implements DateParser, Serializable {
|
||||
* @return this
|
||||
*/
|
||||
public RegexListDateParser addRegex(final String regex) {
|
||||
return addPattern(Pattern.compile(regex));
|
||||
// 日期正则忽略大小写
|
||||
return addPattern(Pattern.compile(regex, Pattern.CASE_INSENSITIVE));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,29 +1,110 @@
|
||||
package org.dromara.hutool.core.date.format.parser;
|
||||
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.dromara.hutool.core.regex.ReUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class RegexDateParserTest {
|
||||
|
||||
// hh:mm:ss.SSSSZ hh:mm:ss.SSSS hh:mm:ss hh:mm
|
||||
final String timeRegex = "(" +
|
||||
"\\s(?<hour>\\d{1,2})" +
|
||||
":(?<minute>\\d{1,2})" +
|
||||
"(:(?<second>\\d{1,2}))?" +
|
||||
"(?:[.,](?<ns>\\d{1,9}))?(?<zero>z)?" +
|
||||
"(\\s?(?<m>am|pm))?" +
|
||||
")?";
|
||||
// +08:00 +0800 +08
|
||||
final String zoneOffsetRegex = "(\\s?(?<zoneOffset>[-+]\\d{1,2}:?(?:\\d{2})?))?";
|
||||
// CST UTC (CST)
|
||||
final String zoneNameRegex = "(\\s[(]?(?<zoneName>[a-z ]+)[)]?)?";
|
||||
|
||||
@Test
|
||||
void timeMatchTest() {
|
||||
assertMatch(timeRegex, " 15:04:05");
|
||||
assertMatch(timeRegex, " 15:04:05.3186369");
|
||||
assertMatch(timeRegex, " 15:04:05.3186369z");
|
||||
assertMatch(timeRegex, " 15:04:05.318");
|
||||
assertMatch(timeRegex, " 15:04:05.318");
|
||||
assertMatch(timeRegex, " 15:04");
|
||||
assertMatch(timeRegex, " 05:04pm");
|
||||
assertMatch(timeRegex, " 05:04 PM");
|
||||
assertMatch(timeRegex, " 05:04:12PM");
|
||||
assertMatch(timeRegex, " 05:04:12 PM");
|
||||
assertMatch(timeRegex, " 5:4pm");
|
||||
assertMatch(timeRegex, " 5:4am");
|
||||
}
|
||||
|
||||
@Test
|
||||
void zoneOffsetMatchTest() {
|
||||
assertMatch(zoneOffsetRegex, "+0800");
|
||||
assertMatch(zoneOffsetRegex, "+08");
|
||||
assertMatch(zoneOffsetRegex, "+08:00");
|
||||
assertMatch(zoneOffsetRegex, " +08:00");
|
||||
assertMatch(zoneOffsetRegex, "+0000");
|
||||
assertMatch(zoneOffsetRegex, " -0700");
|
||||
}
|
||||
|
||||
@Test
|
||||
void zoneNameMatchTest() {
|
||||
assertMatch(zoneNameRegex, " (CST)");
|
||||
assertMatch(zoneNameRegex, " CST");
|
||||
assertMatch(zoneNameRegex, " (GMT)");
|
||||
assertMatch(zoneNameRegex, " (CEST)");
|
||||
assertMatch(zoneNameRegex, " (GMT Daylight Time)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsePureTest() {
|
||||
// yyyyMMdd
|
||||
final RegexDateParser parser = RegexDateParser.of("^(?<year>\\d{4})(?<month>\\d{2})(?<day>\\d{2})$");
|
||||
final Date parse = parser.parse("20220101");
|
||||
Assertions.assertEquals("2022-01-01", DateUtil.date(parse).toDateStr());
|
||||
assertEquals("2022-01-01", DateUtil.date(parse).toDateStr());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseMonthFirstTest() {
|
||||
final String dateRegex = "(?<month>\\w+{3,9})\\W+(?<day>\\d{1,2})(?:th)?\\W+(?<year>\\d{2,4})";
|
||||
|
||||
// +08:00
|
||||
// +0800
|
||||
// +08
|
||||
final String zoneOffsetRegex = "(\\s?(?<zoneOffset>[-+]\\d{1,2}:?(?:\\d{2})?))?";
|
||||
|
||||
// May 8, 2009 5:57:51
|
||||
final RegexDateParser parser = RegexDateParser.of("(?<month>\\w+)\\W+(?<day>\\d{1,2})(?:th)?\\W+(?<year>\\d{2,4})(\\s(?<hour>\\d{1,2}):(?<minute>\\d{1,2}):(?<second>\\d{1,2}))?");
|
||||
final RegexDateParser parser = RegexDateParser.of(dateRegex + timeRegex + zoneOffsetRegex);
|
||||
|
||||
final Date parse = parser.parse("May 8, 2009 5:57:51");
|
||||
Assertions.assertEquals("2009-05-08 05:57:51", DateUtil.date(parse).toString());
|
||||
Date parse = parser.parse("May 8, 2009 5:57:51");
|
||||
assertEquals("2009-05-08 05:57:51", DateUtil.date(parse).toString());
|
||||
|
||||
final Date parse2 = parser.parse("May 8, 2009");
|
||||
Assertions.assertEquals("2009-05-08 00:00:00", DateUtil.date(parse2).toString());
|
||||
parse = parser.parse("May 8, 2009 5:57:51 +08:00");
|
||||
assertEquals("2009-05-08 05:57:51", DateUtil.date(parse).toString());
|
||||
|
||||
parse = parser.parse("May 8, 2009 5:57:51 +0800");
|
||||
assertEquals("2009-05-08 05:57:51", DateUtil.date(parse).toString());
|
||||
|
||||
parse = parser.parse("May 8, 2009 5:57:51 +08");
|
||||
assertEquals("2009-05-08 05:57:51", DateUtil.date(parse).toString());
|
||||
|
||||
parse = parser.parse("May 8, 2009");
|
||||
assertEquals("2009-05-08 00:00:00", DateUtil.date(parse).toString());
|
||||
|
||||
parse = parser.parse("May 8th, 2009");
|
||||
assertEquals("2009-05-08 00:00:00", DateUtil.date(parse).toString());
|
||||
|
||||
parse = parser.parse("May 8th, 09");
|
||||
assertEquals("2009-05-08 00:00:00", DateUtil.date(parse).toString());
|
||||
|
||||
parse = parser.parse("may. 8th, 09");
|
||||
assertEquals("2009-05-08 00:00:00", DateUtil.date(parse).toString());
|
||||
}
|
||||
|
||||
private static void assertMatch(final String regex, final String dateStr){
|
||||
assertTrue(ReUtil.isMatch(Pattern.compile(regex, Pattern.CASE_INSENSITIVE), dateStr));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user