mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method
This commit is contained in:
parent
9642fa8862
commit
1072639f3b
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.7.14 (2021-09-27)
|
||||
# 5.7.14 (2021-09-28)
|
||||
|
||||
### 🐣新特性
|
||||
* 【extra 】 修复HttpCookie设置cookies的方法,不符合RFC6265规范问题(issue#I4B70D@Gitee)
|
||||
@ -13,6 +13,7 @@
|
||||
* 【core 】 DateConverter修改返回java.util.Date而非DateTime(issue#I4BOAP@Gitee)
|
||||
* 【core 】 增加IterableIter、ComputeIter
|
||||
* 【core 】 CsvConfig增加disableComment方法(issue#1842@Github)
|
||||
* 【core 】 DateTime构造和DateUtil.parse可选是否宽松模式(issue#1849@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【http 】 修复HttpCookie设置cookies的方法,不符合RFC6265规范问题(pr#418@Gitee)
|
||||
|
@ -287,7 +287,19 @@ public class DateTime extends Date {
|
||||
* @see DatePattern
|
||||
*/
|
||||
public DateTime(CharSequence dateStr, DateParser dateParser) {
|
||||
this(parse(dateStr, dateParser));
|
||||
this(dateStr, dateParser, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param dateStr Date字符串
|
||||
* @param dateParser 格式化器 {@link DateParser},可以使用 {@link FastDateFormat}
|
||||
* @param lenient 是否宽容模式
|
||||
* @see DatePattern
|
||||
*/
|
||||
public DateTime(CharSequence dateStr, DateParser dateParser, boolean lenient) {
|
||||
this(parse(dateStr, dateParser, lenient));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- Constructor end
|
||||
@ -1017,13 +1029,14 @@ public class DateTime extends Date {
|
||||
*
|
||||
* @param dateStr 日期字符串
|
||||
* @param parser {@link FastDateFormat}
|
||||
* @return {@link Date}
|
||||
* @param lenient 是否宽容模式
|
||||
* @return {@link Calendar}
|
||||
*/
|
||||
private static Calendar parse(CharSequence dateStr, DateParser parser) {
|
||||
private static Calendar parse(CharSequence dateStr, DateParser parser, boolean lenient) {
|
||||
Assert.notNull(parser, "Parser or DateFromat must be not null !");
|
||||
Assert.notBlank(dateStr, "Date String must be not blank !");
|
||||
|
||||
final Calendar calendar = CalendarUtil.parse(dateStr, true, parser);
|
||||
final Calendar calendar = CalendarUtil.parse(dateStr, lenient, parser);
|
||||
if (null == calendar) {
|
||||
throw new DateException("Parse [{}] with format [{}] error!", dateStr, parser.getPattern());
|
||||
}
|
||||
|
@ -683,6 +683,19 @@ public class DateUtil extends CalendarUtil {
|
||||
return new DateTime(dateStr, parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建DateTime对象
|
||||
*
|
||||
* @param dateStr Date字符串
|
||||
* @param parser 格式化器,{@link FastDateFormat}
|
||||
* @param lenient 是否宽容模式
|
||||
* @return DateTime对象
|
||||
* @since 5.7.14
|
||||
*/
|
||||
public static DateTime parse(CharSequence dateStr, DateParser parser, boolean lenient) {
|
||||
return new DateTime(dateStr, parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建DateTime对象
|
||||
*
|
||||
@ -834,20 +847,20 @@ public class DateUtil extends CalendarUtil {
|
||||
if (length <= patternLength - 4 && length >= patternLength - 6) {
|
||||
return parse(utcString, DatePattern.UTC_MS_FORMAT);
|
||||
}
|
||||
} else if(StrUtil.contains(utcString, '+')){
|
||||
} else if (StrUtil.contains(utcString, '+')) {
|
||||
// 去除类似2019-06-01T19:45:43 +08:00加号前的空格
|
||||
utcString = utcString.replace(" +", "+");
|
||||
final String zoneOffset = StrUtil.subAfter(utcString, '+', true);
|
||||
if(StrUtil.isBlank(zoneOffset)){
|
||||
if (StrUtil.isBlank(zoneOffset)) {
|
||||
throw new DateException("Invalid format: [{}]", utcString);
|
||||
}
|
||||
if(false == StrUtil.contains(zoneOffset, ':')){
|
||||
if (false == StrUtil.contains(zoneOffset, ':')) {
|
||||
// +0800转换为+08:00
|
||||
final String pre = StrUtil.subBefore(utcString, '+', true);
|
||||
utcString = pre + "+" + zoneOffset.substring(0, 2) + ":" + "00";
|
||||
}
|
||||
|
||||
if(StrUtil.contains(utcString, CharUtil.DOT)) {
|
||||
if (StrUtil.contains(utcString, CharUtil.DOT)) {
|
||||
// 带毫秒,格式类似:2018-09-13T05:34:31.999+08:00
|
||||
return parse(utcString, DatePattern.UTC_MS_WITH_XXX_OFFSET_FORMAT);
|
||||
} else {
|
||||
@ -929,7 +942,7 @@ public class DateUtil extends CalendarUtil {
|
||||
return parse(dateStr, DatePattern.PURE_DATETIME_FORMAT);
|
||||
} else if (length == DatePattern.PURE_DATETIME_MS_PATTERN.length()) {
|
||||
return parse(dateStr, DatePattern.PURE_DATETIME_MS_FORMAT);
|
||||
}else if (length == DatePattern.PURE_DATE_PATTERN.length()) {
|
||||
} else if (length == DatePattern.PURE_DATE_PATTERN.length()) {
|
||||
return parse(dateStr, DatePattern.PURE_DATE_FORMAT);
|
||||
} else if (length == DatePattern.PURE_TIME_PATTERN.length()) {
|
||||
return parse(dateStr, DatePattern.PURE_TIME_FORMAT);
|
||||
@ -964,7 +977,7 @@ public class DateUtil extends CalendarUtil {
|
||||
if (indexOfDot > 0) {
|
||||
final int length1 = dateStr.length();
|
||||
// yyyy-MM-dd HH:mm:ss.SSS 或者 yyyy-MM-dd HH:mm:ss.SSSSSS
|
||||
if(length1 - indexOfDot > 4) {
|
||||
if (length1 - indexOfDot > 4) {
|
||||
// 类似yyyy-MM-dd HH:mm:ss.SSSSSS,采取截断操作
|
||||
dateStr = StrUtil.subPre(dateStr, indexOfDot + 4);
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package cn.hutool.core.date;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -133,10 +132,12 @@ public class DateTimeTest {
|
||||
Assert.assertEquals(1, date.weekOfYear());
|
||||
}
|
||||
|
||||
@Test
|
||||
/**
|
||||
* 严格模式下,不允许非常规的数字,如秒部分最多59,99则报错
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void ofTest(){
|
||||
String a = "2021-09-27 00:00:99";
|
||||
final DateTime dateTime = new DateTime(a, DatePattern.NORM_DATETIME_FORMAT);
|
||||
Console.log(dateTime);
|
||||
new DateTime(a, DatePattern.NORM_DATETIME_FORMAT, false);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user