mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
3d3bd61b9d
commit
705baea568
@ -785,19 +785,26 @@ public class CalendarUtil {
|
||||
* 使用指定{@link DateParser}解析字符串为{@link Calendar}
|
||||
*
|
||||
* @param str 日期字符串
|
||||
* @param lenient 是否宽容模式
|
||||
* @param parser {@link DateParser}
|
||||
* @return 解析后的 {@link Calendar},解析失败返回{@code null}
|
||||
* @since 5.7.14
|
||||
* @param lenient 是否宽容模式
|
||||
* @return 解析后的 {@link Calendar},解析失败抛出异常
|
||||
* @throws DateException 解析失败抛出此异常
|
||||
*/
|
||||
public static Calendar parse(final CharSequence str, final boolean lenient, final PositionDateParser parser) {
|
||||
public static Calendar parse(final CharSequence str, final PositionDateParser parser,
|
||||
final boolean lenient) throws DateException{
|
||||
Assert.notBlank(str, "Date str must be not blank!");
|
||||
Assert.notNull(parser, "Parser must be not null!");
|
||||
final Calendar calendar = Calendar.getInstance(parser.getTimeZone(), parser.getLocale());
|
||||
calendar.clear();
|
||||
calendar.setLenient(lenient);
|
||||
|
||||
return parser.parse(str.toString(), new ParsePosition(0), calendar) ? calendar : null;
|
||||
final ParsePosition position = new ParsePosition(0);
|
||||
if (parser.parse(str.toString(), position, calendar)) {
|
||||
return calendar;
|
||||
}
|
||||
|
||||
throw new DateException("Parse [{}] with format [{}] error, at: {}",
|
||||
str, parser.getPattern(), position.getErrorIndex());
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@ -142,7 +142,7 @@ public class DateTime extends Date {
|
||||
return new DateTime();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- Constructor start
|
||||
// region ----- Constructors
|
||||
|
||||
/**
|
||||
* 当前时间
|
||||
@ -332,7 +332,7 @@ public class DateTime extends Date {
|
||||
* @see DateFormatPool
|
||||
*/
|
||||
public DateTime(final CharSequence dateStr, final PositionDateParser dateParser) {
|
||||
this(dateStr, dateParser, SystemUtil.getBoolean(SystemUtil.HUTOOL_DATE_LENIENT, true));
|
||||
this(dateStr, dateParser, SystemUtil.getBoolean(SystemUtil.HUTOOL_DATE_LENIENT, false));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -347,9 +347,9 @@ public class DateTime extends Date {
|
||||
this(parse(dateStr, dateParser, lenient));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- Constructor end
|
||||
// endregion
|
||||
|
||||
// -------------------------------------------------------------------- offset start
|
||||
// region ----- offset
|
||||
|
||||
/**
|
||||
* 调整日期和时间<br>
|
||||
@ -388,9 +388,9 @@ public class DateTime extends Date {
|
||||
|
||||
return ObjUtil.clone(this).setTimeInternal(cal.getTimeInMillis());
|
||||
}
|
||||
// -------------------------------------------------------------------- offset end
|
||||
// endregion
|
||||
|
||||
// -------------------------------------------------------------------- Part of Date start
|
||||
// region ----- Part of Date
|
||||
|
||||
/**
|
||||
* 获得日期的某个部分<br>
|
||||
@ -658,7 +658,7 @@ public class DateTime extends Date {
|
||||
final int dayOfWeek = dayOfWeek();
|
||||
return Calendar.SATURDAY == dayOfWeek || Calendar.SUNDAY == dayOfWeek;
|
||||
}
|
||||
// -------------------------------------------------------------------- Part of Date end
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 是否闰年
|
||||
@ -1107,14 +1107,7 @@ public class DateTime extends Date {
|
||||
* @return {@link Calendar}
|
||||
*/
|
||||
private static Calendar parse(final CharSequence dateStr, final PositionDateParser parser, final 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, lenient, parser);
|
||||
if (null == calendar) {
|
||||
throw new DateException("Parse [{}] with format [{}] error!", dateStr, parser.getPattern());
|
||||
}
|
||||
|
||||
final Calendar calendar = CalendarUtil.parse(dateStr, parser, lenient);
|
||||
//noinspection MagicConstant
|
||||
calendar.setFirstDayOfWeek(Week.MONDAY.getValue());
|
||||
return calendar;
|
||||
|
@ -26,6 +26,7 @@ import org.dromara.hutool.core.date.format.parser.RegisterDateParser;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.text.split.SplitUtil;
|
||||
import org.dromara.hutool.core.util.SystemUtil;
|
||||
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import java.text.DateFormat;
|
||||
@ -1956,7 +1957,24 @@ public class DateUtil {
|
||||
* @return {@link SimpleDateFormat}
|
||||
* @since 5.5.5
|
||||
*/
|
||||
public static SimpleDateFormat newSimpleFormat(final String pattern, Locale locale, final TimeZone timeZone) {
|
||||
public static SimpleDateFormat newSimpleFormat(final String pattern,
|
||||
final Locale locale, final TimeZone timeZone) {
|
||||
return newSimpleFormat(pattern, locale, timeZone,
|
||||
SystemUtil.getBoolean(SystemUtil.HUTOOL_DATE_LENIENT, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建{@link SimpleDateFormat},注意此对象非线程安全!<br>
|
||||
* 此对象默认为严格格式模式,即parse时如果格式不正确会报错。
|
||||
*
|
||||
* @param pattern 表达式
|
||||
* @param locale {@link Locale},{@code null}表示默认
|
||||
* @param timeZone {@link TimeZone},{@code null}表示默认
|
||||
* @param lenient 是否宽松模式
|
||||
* @return {@link SimpleDateFormat}
|
||||
*/
|
||||
public static SimpleDateFormat newSimpleFormat(final String pattern,
|
||||
Locale locale, final TimeZone timeZone, final boolean lenient) {
|
||||
if (null == locale) {
|
||||
locale = Locale.getDefault(Locale.Category.FORMAT);
|
||||
}
|
||||
@ -1964,7 +1982,7 @@ public class DateUtil {
|
||||
if (null != timeZone) {
|
||||
format.setTimeZone(timeZone);
|
||||
}
|
||||
format.setLenient(false);
|
||||
format.setLenient(lenient);
|
||||
return format;
|
||||
}
|
||||
|
||||
|
@ -37,8 +37,8 @@ public class CalendarUtilTest {
|
||||
@Test
|
||||
public void parseTest(){
|
||||
Assertions.assertThrows(IllegalArgumentException.class, ()->{
|
||||
final Calendar calendar = CalendarUtil.parse("2021-09-27 00:00:112323", false,
|
||||
DateFormatPool.NORM_DATETIME_FORMAT);
|
||||
final Calendar calendar = CalendarUtil.parse("2021-09-27 00:00:112323",
|
||||
DateFormatPool.NORM_DATETIME_FORMAT, false);
|
||||
|
||||
// https://github.com/dromara/hutool/issues/1849
|
||||
// 在使用严格模式时,秒不正确,抛出异常
|
||||
|
Loading…
x
Reference in New Issue
Block a user