DateUtil.parse支持毫秒时间戳

This commit is contained in:
Looly 2023-12-12 13:35:00 +08:00
parent 2a164a7692
commit 011f68e018
2 changed files with 12 additions and 0 deletions

View File

@ -16,6 +16,7 @@ import org.dromara.hutool.core.date.DateException;
import org.dromara.hutool.core.date.DatePattern;
import org.dromara.hutool.core.date.DateTime;
import org.dromara.hutool.core.date.format.DefaultDateBasic;
import org.dromara.hutool.core.math.NumberUtil;
/**
* 纯数字的日期字符串解析支持格式包括
@ -24,6 +25,7 @@ import org.dromara.hutool.core.date.format.DefaultDateBasic;
* <li>yyyyMMddHHmmssSSS</li>
* <li>yyyyMMdd</li>
* <li>HHmmss</li>
* <li>毫秒时间戳</li>
* </ul>
*
* @author looly
@ -49,6 +51,9 @@ public class PureDateParser extends DefaultDateBasic implements DateParser {
return new DateTime(source, DatePattern.PURE_DATE_FORMAT);
} else if (length == DatePattern.PURE_TIME_PATTERN.length()) {
return new DateTime(source, DatePattern.PURE_TIME_FORMAT);
} else if(length == 13){
// 时间戳
return new DateTime(NumberUtil.parseLong(source));
}
throw new DateException("No pure format fit for date String [{}] !", source);

View File

@ -1130,4 +1130,11 @@ public class DateUtilTest {
Assertions.assertNotNull(parse);
Assertions.assertEquals("2019-10-22 09:56:03", parse.toString());
}
@Test
public void issueI8NMP7Test() {
final String str = "1702262524444";
final DateTime parse = DateUtil.parse(str);
Assertions.assertEquals("2023-12-11 10:42:04", Objects.requireNonNull(parse).toString());
}
}