mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add RegexDateParser
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package org.dromara.hutool.core.date;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateBuilderTest {
|
||||
@Test
|
||||
public void testNormal() {
|
||||
final DateBuilder builder = new DateBuilder();
|
||||
builder.setYear(2019);
|
||||
builder.setMonth(10);
|
||||
builder.setDay(1);
|
||||
final Date date = builder.toDate();
|
||||
|
||||
Assertions.assertEquals("2019-10-01", DateUtil.date(date).toDateStr());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalDateTime() {
|
||||
final DateBuilder builder = DateBuilder.of()
|
||||
.setYear(2019)
|
||||
.setMonth(10)
|
||||
.setDay(1)
|
||||
.setHour(10)
|
||||
.setMinute(20)
|
||||
.setSecond(30)
|
||||
.setNs(900000000)
|
||||
.setZone(TimeZone.getDefault());
|
||||
|
||||
final LocalDateTime dateTime = builder.toLocalDateTime();
|
||||
Assertions.assertEquals("2019-10-01T10:20:30.900", builder.toLocalDateTime().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTimestamp() {
|
||||
final String timestamp = "946656000";
|
||||
final DateBuilder dateBuilder = DateBuilder.of().setUnixsecond(Long.parseLong(timestamp));
|
||||
Assertions.assertEquals("2000-01-01T00:00", dateBuilder.toLocalDateTime().toString());
|
||||
}
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
package org.dromara.hutool.core.date;
|
||||
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class IssueI8IUTBTest {
|
||||
@Test
|
||||
void parseTest() {
|
||||
final DateTime parse = DateUtil.parse("May 8, 2009 5:57:51 PM");
|
||||
Console.log(parse);
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package org.dromara.hutool.core.date.format.parser;
|
||||
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class RegexDateParserTest {
|
||||
@Test
|
||||
void parsePureTest() {
|
||||
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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user