Merge branch 'v5-dev' of gitee.com:dromara/hutool into v5-

dev
This commit is contained in:
dazer007 2021-07-14 03:36:52 +00:00 committed by Gitee
commit f88339522e
9 changed files with 180 additions and 82 deletions

View File

@ -3,16 +3,19 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.7.5 (2021-07-12) # 5.7.5 (2021-07-14)
### 🐣新特性 ### 🐣新特性
* 【core 】 DateUtil增加ceiling重载可选是否归零毫秒 * 【core 】 DateUtil增加ceiling重载可选是否归零毫秒
* 【core 】 IterUtil增加firstMatch方法 * 【core 】 IterUtil增加firstMatch方法
* 【core 】 增加NanoId * 【core 】 增加NanoId
* 【core 】 MapBuilder增加put方法pr#367@Gitee
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复FileUtil.normalize处理上级路径的问题issue#I3YPEH@Gitee * 【core 】 修复FileUtil.normalize处理上级路径的问题issue#I3YPEH@Gitee
* 【core 】 修复ClassScanner扫描空包遗漏问题 * 【core 】 修复ClassScanner扫描空包遗漏问题
* 【core 】 修复FastDatePrinter歧义问题pr#366@Gitee
* 【core 】 修复DateUtil.format格式化Instant报错问题issue#I40CY2@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -2,6 +2,7 @@ package cn.hutool.core.date;
import cn.hutool.core.date.format.FastDateFormat; import cn.hutool.core.date.format.FastDateFormat;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
@ -39,7 +40,7 @@ public class DatePattern {
/** /**
* 年月格式 {@link FastDateFormat}yyyy-MM * 年月格式 {@link FastDateFormat}yyyy-MM
*/ */
public static final DateTimeFormatter NORM_MONTH_FORMATTER = DateTimeFormatter.ofPattern(NORM_MONTH_PATTERN); public static final DateTimeFormatter NORM_MONTH_FORMATTER = createFormatter(NORM_MONTH_PATTERN);
/** /**
* 简单年月格式yyyyMM * 简单年月格式yyyyMM
@ -52,7 +53,7 @@ public class DatePattern {
/** /**
* 简单年月格式 {@link FastDateFormat}yyyyMM * 简单年月格式 {@link FastDateFormat}yyyyMM
*/ */
public static final DateTimeFormatter SIMPLE_MONTH_FORMATTER = DateTimeFormatter.ofPattern(SIMPLE_MONTH_PATTERN); public static final DateTimeFormatter SIMPLE_MONTH_FORMATTER = createFormatter(SIMPLE_MONTH_PATTERN);
/** /**
* 标准日期格式yyyy-MM-dd * 标准日期格式yyyy-MM-dd
@ -65,7 +66,7 @@ public class DatePattern {
/** /**
* 标准日期格式 {@link FastDateFormat}yyyy-MM-dd * 标准日期格式 {@link FastDateFormat}yyyy-MM-dd
*/ */
public static final DateTimeFormatter NORM_DATE_FORMATTER = DateTimeFormatter.ofPattern(NORM_DATE_PATTERN); public static final DateTimeFormatter NORM_DATE_FORMATTER = createFormatter(NORM_DATE_PATTERN);
/** /**
* 标准时间格式HH:mm:ss * 标准时间格式HH:mm:ss
@ -78,7 +79,7 @@ public class DatePattern {
/** /**
* 标准日期格式 {@link FastDateFormat}HH:mm:ss * 标准日期格式 {@link FastDateFormat}HH:mm:ss
*/ */
public static final DateTimeFormatter NORM_TIME_FORMATTER = DateTimeFormatter.ofPattern(NORM_TIME_PATTERN); public static final DateTimeFormatter NORM_TIME_FORMATTER = createFormatter(NORM_TIME_PATTERN);
/** /**
* 标准日期时间格式精确到分yyyy-MM-dd HH:mm * 标准日期时间格式精确到分yyyy-MM-dd HH:mm
@ -91,7 +92,7 @@ public class DatePattern {
/** /**
* 标准日期格式 {@link FastDateFormat}yyyy-MM-dd HH:mm * 标准日期格式 {@link FastDateFormat}yyyy-MM-dd HH:mm
*/ */
public static final DateTimeFormatter NORM_DATETIME_MINUTE_FORMATTER = DateTimeFormatter.ofPattern(NORM_DATETIME_MINUTE_PATTERN); public static final DateTimeFormatter NORM_DATETIME_MINUTE_FORMATTER = createFormatter(NORM_DATETIME_MINUTE_PATTERN);
/** /**
* 标准日期时间格式精确到秒yyyy-MM-dd HH:mm:ss * 标准日期时间格式精确到秒yyyy-MM-dd HH:mm:ss
@ -104,7 +105,7 @@ public class DatePattern {
/** /**
* 标准日期时间格式精确到秒 {@link FastDateFormat}yyyy-MM-dd HH:mm:ss * 标准日期时间格式精确到秒 {@link FastDateFormat}yyyy-MM-dd HH:mm:ss
*/ */
public static final DateTimeFormatter NORM_DATETIME_FORMATTER = DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN); public static final DateTimeFormatter NORM_DATETIME_FORMATTER = createFormatter(NORM_DATETIME_PATTERN);
/** /**
* 标准日期时间格式精确到毫秒yyyy-MM-dd HH:mm:ss.SSS * 标准日期时间格式精确到毫秒yyyy-MM-dd HH:mm:ss.SSS
@ -117,7 +118,7 @@ public class DatePattern {
/** /**
* 标准日期时间格式精确到毫秒 {@link FastDateFormat}yyyy-MM-dd HH:mm:ss.SSS * 标准日期时间格式精确到毫秒 {@link FastDateFormat}yyyy-MM-dd HH:mm:ss.SSS
*/ */
public static final DateTimeFormatter NORM_DATETIME_MS_FORMATTER = DateTimeFormatter.ofPattern(NORM_DATETIME_MS_PATTERN); public static final DateTimeFormatter NORM_DATETIME_MS_FORMATTER = createFormatter(NORM_DATETIME_MS_PATTERN);
/** /**
* ISO8601日期时间格式精确到毫秒yyyy-MM-dd HH:mm:ss,SSS * ISO8601日期时间格式精确到毫秒yyyy-MM-dd HH:mm:ss,SSS
@ -130,7 +131,7 @@ public class DatePattern {
/** /**
* 标准日期格式 {@link FastDateFormat}yyyy-MM-dd HH:mm:ss,SSS * 标准日期格式 {@link FastDateFormat}yyyy-MM-dd HH:mm:ss,SSS
*/ */
public static final DateTimeFormatter ISO8601_FORMATTER = DateTimeFormatter.ofPattern(ISO8601_PATTERN); public static final DateTimeFormatter ISO8601_FORMATTER = createFormatter(ISO8601_PATTERN);
/** /**
* 标准日期格式yyyy年MM月dd日 * 标准日期格式yyyy年MM月dd日
@ -143,7 +144,7 @@ public class DatePattern {
/** /**
* 标准日期格式 {@link FastDateFormat}yyyy年MM月dd日 * 标准日期格式 {@link FastDateFormat}yyyy年MM月dd日
*/ */
public static final DateTimeFormatter CHINESE_DATE_FORMATTER = DateTimeFormatter.ofPattern(ISO8601_PATTERN); public static final DateTimeFormatter CHINESE_DATE_FORMATTER = createFormatter(ISO8601_PATTERN);
/** /**
* 标准日期格式yyyy年MM月dd日 HH时mm分ss秒 * 标准日期格式yyyy年MM月dd日 HH时mm分ss秒
@ -156,7 +157,7 @@ public class DatePattern {
/** /**
* 标准日期格式 {@link FastDateFormat}yyyy年MM月dd日HH时mm分ss秒 * 标准日期格式 {@link FastDateFormat}yyyy年MM月dd日HH时mm分ss秒
*/ */
public static final DateTimeFormatter CHINESE_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(CHINESE_DATE_TIME_PATTERN); public static final DateTimeFormatter CHINESE_DATE_TIME_FORMATTER = createFormatter(CHINESE_DATE_TIME_PATTERN);
//-------------------------------------------------------------------------------------------------------------------------------- Pure //-------------------------------------------------------------------------------------------------------------------------------- Pure
/** /**
@ -170,7 +171,7 @@ public class DatePattern {
/** /**
* 标准日期格式 {@link FastDateFormat}yyyyMMdd * 标准日期格式 {@link FastDateFormat}yyyyMMdd
*/ */
public static final DateTimeFormatter PURE_DATE_FORMATTER = DateTimeFormatter.ofPattern(PURE_DATE_PATTERN); public static final DateTimeFormatter PURE_DATE_FORMATTER = createFormatter(PURE_DATE_PATTERN);
/** /**
* 标准日期格式HHmmss * 标准日期格式HHmmss
@ -183,7 +184,7 @@ public class DatePattern {
/** /**
* 标准日期格式 {@link FastDateFormat}HHmmss * 标准日期格式 {@link FastDateFormat}HHmmss
*/ */
public static final DateTimeFormatter PURE_TIME_FORMATTER = DateTimeFormatter.ofPattern(PURE_TIME_PATTERN); public static final DateTimeFormatter PURE_TIME_FORMATTER = createFormatter(PURE_TIME_PATTERN);
/** /**
* 标准日期格式yyyyMMddHHmmss * 标准日期格式yyyyMMddHHmmss
@ -196,7 +197,7 @@ public class DatePattern {
/** /**
* 标准日期格式 {@link FastDateFormat}yyyyMMddHHmmss * 标准日期格式 {@link FastDateFormat}yyyyMMddHHmmss
*/ */
public static final DateTimeFormatter PURE_DATETIME_FORMATTER = DateTimeFormatter.ofPattern(PURE_DATETIME_PATTERN); public static final DateTimeFormatter PURE_DATETIME_FORMATTER = createFormatter(PURE_DATETIME_PATTERN);
/** /**
* 标准日期格式yyyyMMddHHmmssSSS * 标准日期格式yyyyMMddHHmmssSSS
@ -209,7 +210,7 @@ public class DatePattern {
/** /**
* 标准日期格式 {@link FastDateFormat}yyyyMMddHHmmssSSS * 标准日期格式 {@link FastDateFormat}yyyyMMddHHmmssSSS
*/ */
public static final DateTimeFormatter PURE_DATETIME_MS_FORMATTER = DateTimeFormatter.ofPattern(PURE_DATETIME_MS_PATTERN); public static final DateTimeFormatter PURE_DATETIME_MS_FORMATTER = createFormatter(PURE_DATETIME_MS_PATTERN);
//-------------------------------------------------------------------------------------------------------------------------------- Others //-------------------------------------------------------------------------------------------------------------------------------- Others
/** /**
@ -283,4 +284,16 @@ public class DatePattern {
* UTC时间{@link FastDateFormat}yyyy-MM-dd'T'HH:mm:ssZ * UTC时间{@link FastDateFormat}yyyy-MM-dd'T'HH:mm:ssZ
*/ */
public static final FastDateFormat UTC_MS_WITH_ZONE_OFFSET_FORMAT = FastDateFormat.getInstance(UTC_MS_WITH_ZONE_OFFSET_PATTERN, TimeZone.getTimeZone("UTC")); public static final FastDateFormat UTC_MS_WITH_ZONE_OFFSET_FORMAT = FastDateFormat.getInstance(UTC_MS_WITH_ZONE_OFFSET_PATTERN, TimeZone.getTimeZone("UTC"));
/**
* 创建并为 {@link DateTimeFormatter} 赋予默认时区和位置信息默认值为系统默认值
*
* @param pattern 日期格式
* @return {@link DateTimeFormatter}
* @since 5.7.5
*/
private static DateTimeFormatter createFormatter(String pattern) {
return DateTimeFormatter.ofPattern(pattern, Locale.getDefault())
.withZone(ZoneId.systemDefault());
}
} }

View File

@ -546,7 +546,8 @@ public class DateUtil extends CalendarUtil {
if (format.getZone() == null) { if (format.getZone() == null) {
format = format.withZone(ZoneId.systemDefault()); format = format.withZone(ZoneId.systemDefault());
} }
return format.format(date.toInstant()); /// return format.format(date.toInstant());
return TemporalAccessorUtil.format(date.toInstant(), format);
} }
/** /**

View File

@ -56,7 +56,6 @@ public class TemporalAccessorUtil extends TemporalUtil{
formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
} }
try { try {
return formatter.format(time); return formatter.format(time);
} catch (UnsupportedTemporalTypeException e){ } catch (UnsupportedTemporalTypeException e){
@ -66,6 +65,9 @@ public class TemporalAccessorUtil extends TemporalUtil{
}else if(time instanceof LocalTime && e.getMessage().contains("YearOfEra")){ }else if(time instanceof LocalTime && e.getMessage().contains("YearOfEra")){
// 用户传入LocalTime但是要求格式化带有日期部分转换为LocalDateTime重试 // 用户传入LocalTime但是要求格式化带有日期部分转换为LocalDateTime重试
return formatter.format(((LocalTime) time).atDate(LocalDate.now())); return formatter.format(((LocalTime) time).atDate(LocalDate.now()));
} else if(time instanceof Instant){
// 时间戳没有时区信息赋予默认时区
return formatter.format(((Instant) time).atZone(ZoneId.systemDefault()));
} }
throw e; throw e;
} }

View File

@ -37,7 +37,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param timeZone 非空时区{@link TimeZone} * @param timeZone 非空时区{@link TimeZone}
* @param locale 非空{@link Locale} 日期地理位置 * @param locale 非空{@link Locale} 日期地理位置
*/ */
public FastDatePrinter(final String pattern, final TimeZone timeZone, final Locale locale) { public FastDatePrinter(String pattern, TimeZone timeZone, Locale locale) {
super(pattern, timeZone, locale); super(pattern, timeZone, locale);
init(); init();
} }
@ -211,7 +211,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param indexRef index references * @param indexRef index references
* @return parsed token * @return parsed token
*/ */
protected String parseToken(final String pattern, final int[] indexRef) { protected String parseToken(String pattern, int[] indexRef) {
final StringBuilder buf = new StringBuilder(); final StringBuilder buf = new StringBuilder();
int i = indexRef[0]; int i = indexRef[0];
@ -271,7 +271,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param padding the padding required * @param padding the padding required
* @return a new rule with the correct padding * @return a new rule with the correct padding
*/ */
protected NumberRule selectNumberRule(final int field, final int padding) { protected NumberRule selectNumberRule(int field, int padding) {
switch (padding) { switch (padding) {
case 1: case 1:
return new UnpaddedNumberField(field); return new UnpaddedNumberField(field);
@ -293,53 +293,53 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param obj the object to format * @param obj the object to format
* @return The formatted value. * @return The formatted value.
*/ */
String format(final Object obj) { String format(Object obj) {
if (obj instanceof Date) { if (obj instanceof Date) {
return format((Date) obj); return format((Date) obj);
} else if (obj instanceof Calendar) { } else if (obj instanceof Calendar) {
return format((Calendar) obj); return format((Calendar) obj);
} else if (obj instanceof Long) { } else if (obj instanceof Long) {
return format(((Long) obj)); return format(((Long) obj).longValue());
} else { } else {
throw new IllegalArgumentException("Unknown class: " + (obj == null ? "<null>" : obj.getClass().getName())); throw new IllegalArgumentException("Unknown class: " + (obj == null ? "<null>" : obj.getClass().getName()));
} }
} }
@Override @Override
public String format(final long millis) { public String format(long millis) {
final Calendar c = Calendar.getInstance(timeZone, locale); final Calendar c = Calendar.getInstance(timeZone, locale);
c.setTimeInMillis(millis); c.setTimeInMillis(millis);
return applyRulesToString(c); return applyRulesToString(c);
} }
@Override @Override
public String format(final Date date) { public String format(Date date) {
final Calendar c = Calendar.getInstance(timeZone, locale); final Calendar c = Calendar.getInstance(timeZone, locale);
c.setTime(date); c.setTime(date);
return applyRulesToString(c); return applyRulesToString(c);
} }
@Override @Override
public String format(final Calendar calendar) { public String format(Calendar calendar) {
return format(calendar, new StringBuilder(mMaxLengthEstimate)).toString(); return format(calendar, new StringBuilder(mMaxLengthEstimate)).toString();
} }
@Override @Override
public <B extends Appendable> B format(final long millis, final B buf) { public <B extends Appendable> B format(long millis, B buf) {
final Calendar c = Calendar.getInstance(timeZone, locale); final Calendar c = Calendar.getInstance(timeZone, locale);
c.setTimeInMillis(millis); c.setTimeInMillis(millis);
return applyRules(c, buf); return applyRules(c, buf);
} }
@Override @Override
public <B extends Appendable> B format(final Date date, final B buf) { public <B extends Appendable> B format(Date date, B buf) {
final Calendar c = Calendar.getInstance(timeZone, locale); final Calendar c = Calendar.getInstance(timeZone, locale);
c.setTime(date); c.setTime(date);
return applyRules(c, buf); return applyRules(c, buf);
} }
@Override @Override
public <B extends Appendable> B format(Calendar calendar, final B buf) { public <B extends Appendable> B format(Calendar calendar, B buf) {
// do not pass in calendar directly, this will cause TimeZone of FastDatePrinter to be ignored // do not pass in calendar directly, this will cause TimeZone of FastDatePrinter to be ignored
if (!calendar.getTimeZone().equals(timeZone)) { if (!calendar.getTimeZone().equals(timeZone)) {
calendar = (Calendar) calendar.clone(); calendar = (Calendar) calendar.clone();
@ -354,7 +354,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param c the Calender to apply the rules to. * @param c the Calender to apply the rules to.
* @return a String representation of the given Calendar. * @return a String representation of the given Calendar.
*/ */
private String applyRulesToString(final Calendar c) { private String applyRulesToString(Calendar c) {
return applyRules(c, new StringBuilder(mMaxLengthEstimate)).toString(); return applyRules(c, new StringBuilder(mMaxLengthEstimate)).toString();
} }
@ -368,7 +368,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param <B> the Appendable class type, usually StringBuilder or StringBuffer. * @param <B> the Appendable class type, usually StringBuilder or StringBuffer.
* @return the specified string buffer * @return the specified string buffer
*/ */
private <B extends Appendable> B applyRules(final Calendar calendar, final B buf) { private <B extends Appendable> B applyRules(Calendar calendar, B buf) {
try { try {
for (final Rule rule : this.rules) { for (final Rule rule : this.rules) {
rule.appendTo(buf, calendar); rule.appendTo(buf, calendar);
@ -398,7 +398,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @throws IOException if there is an IO issue. * @throws IOException if there is an IO issue.
* @throws ClassNotFoundException if a class cannot be found. * @throws ClassNotFoundException if a class cannot be found.
*/ */
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject(); in.defaultReadObject();
init(); init();
} }
@ -409,7 +409,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param buffer the buffer to append to. * @param buffer the buffer to append to.
* @param value the value to append digits from. * @param value the value to append digits from.
*/ */
private static void appendDigits(final Appendable buffer, final int value) throws IOException { private static void appendDigits(Appendable buffer, int value) throws IOException {
buffer.append((char) (value / 10 + '0')); buffer.append((char) (value / 10 + '0'));
buffer.append((char) (value % 10 + '0')); buffer.append((char) (value % 10 + '0'));
} }
@ -422,7 +422,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param buffer the buffer to append to. * @param buffer the buffer to append to.
* @param value the value to append digits from. * @param value the value to append digits from.
*/ */
private static void appendFullDigits(final Appendable buffer, int value, int minFieldWidth) throws IOException { private static void appendFullDigits(Appendable buffer, int value, int minFieldWidth) throws IOException {
// specialized paths for 1 to 4 digits -> avoid the memory allocation from the temporary work array // specialized paths for 1 to 4 digits -> avoid the memory allocation from the temporary work array
// see LANG-1248 // see LANG-1248
if (value < 10000) { if (value < 10000) {
@ -568,7 +568,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* *
* @param value the string literal * @param value the string literal
*/ */
StringLiteral(final String value) { StringLiteral(String value) {
mValue = value; mValue = value;
} }
@ -584,7 +584,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
buffer.append(mValue); buffer.append(mValue);
} }
} }
@ -604,7 +604,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param field the field * @param field the field
* @param values the field values * @param values the field values
*/ */
TextField(final int field, final String[] values) { TextField(int field, String[] values) {
mField = field; mField = field;
mValues = values; mValues = values;
} }
@ -628,7 +628,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
buffer.append(mValues[calendar.get(mField)]); buffer.append(mValues[calendar.get(mField)]);
} }
} }
@ -646,7 +646,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* *
* @param field the field * @param field the field
*/ */
UnpaddedNumberField(final int field) { UnpaddedNumberField(int field) {
mField = field; mField = field;
} }
@ -662,7 +662,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
appendTo(buffer, calendar.get(mField)); appendTo(buffer, calendar.get(mField));
} }
@ -670,7 +670,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public final void appendTo(final Appendable buffer, final int value) throws IOException { public final void appendTo(Appendable buffer, int value) throws IOException {
if (value < 10) { if (value < 10) {
buffer.append((char) (value + '0')); buffer.append((char) (value + '0'));
} else if (value < 100) { } else if (value < 100) {
@ -708,7 +708,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
appendTo(buffer, calendar.get(Calendar.MONTH) + 1); appendTo(buffer, calendar.get(Calendar.MONTH) + 1);
} }
@ -716,7 +716,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public final void appendTo(final Appendable buffer, final int value) throws IOException { public final void appendTo(Appendable buffer, int value) throws IOException {
if (value < 10) { if (value < 10) {
buffer.append((char) (value + '0')); buffer.append((char) (value + '0'));
} else { } else {
@ -740,7 +740,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param field the field * @param field the field
* @param size size of the output field * @param size size of the output field
*/ */
PaddedNumberField(final int field, final int size) { PaddedNumberField(int field, int size) {
if (size < 3) { if (size < 3) {
// Should use UnpaddedNumberField or TwoDigitNumberField. // Should use UnpaddedNumberField or TwoDigitNumberField.
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@ -761,7 +761,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
appendTo(buffer, calendar.get(mField)); appendTo(buffer, calendar.get(mField));
} }
@ -769,7 +769,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public final void appendTo(final Appendable buffer, final int value) throws IOException { public final void appendTo(Appendable buffer, int value) throws IOException {
appendFullDigits(buffer, value, mSize); appendFullDigits(buffer, value, mSize);
} }
} }
@ -787,7 +787,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* *
* @param field the field * @param field the field
*/ */
TwoDigitNumberField(final int field) { TwoDigitNumberField(int field) {
mField = field; mField = field;
} }
@ -803,7 +803,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
appendTo(buffer, calendar.get(mField)); appendTo(buffer, calendar.get(mField));
} }
@ -811,7 +811,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public final void appendTo(final Appendable buffer, final int value) throws IOException { public final void appendTo(Appendable buffer, int value) throws IOException {
if (value < 100) { if (value < 100) {
appendDigits(buffer, value); appendDigits(buffer, value);
} else { } else {
@ -846,7 +846,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
appendTo(buffer, calendar.get(Calendar.YEAR) % 100); appendTo(buffer, calendar.get(Calendar.YEAR) % 100);
} }
@ -854,7 +854,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public final void appendTo(final Appendable buffer, final int value) throws IOException { public final void appendTo(Appendable buffer, int value) throws IOException {
appendDigits(buffer, value); appendDigits(buffer, value);
} }
} }
@ -885,7 +885,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
appendTo(buffer, calendar.get(Calendar.MONTH) + 1); appendTo(buffer, calendar.get(Calendar.MONTH) + 1);
} }
@ -893,7 +893,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public final void appendTo(final Appendable buffer, final int value) throws IOException { public final void appendTo(Appendable buffer, int value) throws IOException {
appendDigits(buffer, value); appendDigits(buffer, value);
} }
} }
@ -927,7 +927,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
int value = calendar.get(Calendar.HOUR); int value = calendar.get(Calendar.HOUR);
if (value == 0) { if (value == 0) {
value = calendar.getLeastMaximum(Calendar.HOUR) + 1; value = calendar.getLeastMaximum(Calendar.HOUR) + 1;
@ -939,7 +939,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final int value) throws IOException { public void appendTo(Appendable buffer, int value) throws IOException {
mRule.appendTo(buffer, value); mRule.appendTo(buffer, value);
} }
} }
@ -957,7 +957,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* *
* @param rule the rule * @param rule the rule
*/ */
TwentyFourHourField(final NumberRule rule) { TwentyFourHourField(NumberRule rule) {
mRule = rule; mRule = rule;
} }
@ -973,7 +973,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
int value = calendar.get(Calendar.HOUR_OF_DAY); int value = calendar.get(Calendar.HOUR_OF_DAY);
if (value == 0) { if (value == 0) {
value = calendar.getMaximum(Calendar.HOUR_OF_DAY) + 1; value = calendar.getMaximum(Calendar.HOUR_OF_DAY) + 1;
@ -985,7 +985,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final int value) throws IOException { public void appendTo(Appendable buffer, int value) throws IOException {
mRule.appendTo(buffer, value); mRule.appendTo(buffer, value);
} }
} }
@ -998,7 +998,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
private static class DayInWeekField implements NumberRule { private static class DayInWeekField implements NumberRule {
private final NumberRule mRule; private final NumberRule mRule;
DayInWeekField(final NumberRule rule) { DayInWeekField(NumberRule rule) {
mRule = rule; mRule = rule;
} }
@ -1008,13 +1008,13 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
} }
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
final int value = calendar.get(Calendar.DAY_OF_WEEK); final int value = calendar.get(Calendar.DAY_OF_WEEK);
mRule.appendTo(buffer, value != Calendar.SUNDAY ? value - 1 : 7); mRule.appendTo(buffer, value != Calendar.SUNDAY ? value - 1 : 7);
} }
@Override @Override
public void appendTo(final Appendable buffer, final int value) throws IOException { public void appendTo(Appendable buffer, int value) throws IOException {
mRule.appendTo(buffer, value); mRule.appendTo(buffer, value);
} }
} }
@ -1037,12 +1037,12 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
} }
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
mRule.appendTo(buffer, calendar.getWeekYear()); mRule.appendTo(buffer, calendar.getWeekYear());
} }
@Override @Override
public void appendTo(final Appendable buffer, final int value) throws IOException { public void appendTo(Appendable buffer, int value) throws IOException {
mRule.appendTo(buffer, value); mRule.appendTo(buffer, value);
} }
} }
@ -1062,7 +1062,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param locale the locale to use * @param locale the locale to use
* @return the textual name of the time zone * @return the textual name of the time zone
*/ */
static String getTimeZoneDisplay(final TimeZone tz, final boolean daylight, final int style, final Locale locale) { static String getTimeZoneDisplay(TimeZone tz, boolean daylight, int style, Locale locale) {
final TimeZoneDisplayKey key = new TimeZoneDisplayKey(tz, daylight, style, locale); final TimeZoneDisplayKey key = new TimeZoneDisplayKey(tz, daylight, style, locale);
String value = C_TIME_ZONE_DISPLAY_CACHE.get(key); String value = C_TIME_ZONE_DISPLAY_CACHE.get(key);
if (value == null) { if (value == null) {
@ -1094,7 +1094,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param locale the locale * @param locale the locale
* @param style the style * @param style the style
*/ */
TimeZoneNameRule(final TimeZone timeZone, final Locale locale, final int style) { TimeZoneNameRule(TimeZone timeZone, Locale locale, int style) {
mLocale = locale; mLocale = locale;
mStyle = style; mStyle = style;
@ -1117,7 +1117,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
final TimeZone zone = calendar.getTimeZone(); final TimeZone zone = calendar.getTimeZone();
if (calendar.get(Calendar.DST_OFFSET) != 0) { if (calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(zone, true, mStyle, mLocale)); buffer.append(getTimeZoneDisplay(zone, true, mStyle, mLocale));
@ -1143,7 +1143,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* *
* @param colon add colon between HH and MM in the output if {@code true} * @param colon add colon between HH and MM in the output if {@code true}
*/ */
TimeZoneNumberRule(final boolean colon) { TimeZoneNumberRule(boolean colon) {
mColon = colon; mColon = colon;
} }
@ -1159,7 +1159,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { public void appendTo(Appendable buffer, Calendar calendar) throws IOException {
int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET); int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
@ -1202,7 +1202,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* @param tokenLen a token indicating the length of the TimeZone String to be formatted. * @param tokenLen a token indicating the length of the TimeZone String to be formatted.
* @return a Iso8601_Rule that can format TimeZone String of length {@code tokenLen}. If no such rule exists, an IllegalArgumentException will be thrown. * @return a Iso8601_Rule that can format TimeZone String of length {@code tokenLen}. If no such rule exists, an IllegalArgumentException will be thrown.
*/ */
static Iso8601_Rule getRule(final int tokenLen) { static Iso8601_Rule getRule(int tokenLen) {
switch (tokenLen) { switch (tokenLen) {
case 1: case 1:
return Iso8601_Rule.ISO8601_HOURS; return Iso8601_Rule.ISO8601_HOURS;
@ -1222,7 +1222,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
* *
* @param length The number of characters in output (unless Z is output) * @param length The number of characters in output (unless Z is output)
*/ */
Iso8601_Rule(final int length) { Iso8601_Rule(int length) {
this.length = length; this.length = length;
} }

View File

@ -2,6 +2,7 @@ package cn.hutool.core.map;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier;
/** /**
* Map创建类 * Map创建类
@ -10,7 +11,7 @@ import java.util.Map;
* @param <V> Value类型 * @param <V> Value类型
* @since 3.1.1 * @since 3.1.1
*/ */
public class MapBuilder<K, V> implements Serializable{ public class MapBuilder<K, V> implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final Map<K, V> map; private final Map<K, V> map;
@ -30,8 +31,8 @@ public class MapBuilder<K, V> implements Serializable{
/** /**
* 创建Builder * 创建Builder
* *
* @param <K> Key类型 * @param <K> Key类型
* @param <V> Value类型 * @param <V> Value类型
* @param isLinked true创建LinkedHashMapfalse创建HashMap * @param isLinked true创建LinkedHashMapfalse创建HashMap
* @return MapBuilder * @return MapBuilder
* @since 5.3.0 * @since 5.3.0
@ -74,6 +75,50 @@ public class MapBuilder<K, V> implements Serializable{
return this; return this;
} }
/**
* 链式Map创建
*
* @param k Key类型
* @param supplier Value类型结果提供方
* @return 当前类
* @since 5.7.5
*/
public MapBuilder<K, V> put(K k, Supplier<V> supplier) {
return put(k, supplier.get());
}
/**
* 链式Map创建
*
* @param condition put条件
* @param k Key类型
* @param v Value类型
* @return 当前类
* @since 5.7.5
*/
public MapBuilder<K, V> put(boolean condition, K k, V v) {
if (condition) {
put(k, v);
}
return this;
}
/**
* 链式Map创建
*
* @param condition put条件
* @param k Key类型
* @param supplier Value类型结果提供方
* @return 当前类
* @since 5.7.5
*/
public MapBuilder<K, V> put(boolean condition, K k, Supplier<V> supplier) {
if (condition) {
put(k, supplier);
}
return this;
}
/** /**
* 链式Map创建 * 链式Map创建
* *
@ -107,7 +152,7 @@ public class MapBuilder<K, V> implements Serializable{
/** /**
* 将map转成字符串 * 将map转成字符串
* *
* @param separator entry之间的连接符 * @param separator entry之间的连接符
* @param keyValueSeparator kv之间的连接符 * @param keyValueSeparator kv之间的连接符
* @return 连接字符串 * @return 连接字符串
*/ */
@ -118,7 +163,7 @@ public class MapBuilder<K, V> implements Serializable{
/** /**
* 将map转成字符串 * 将map转成字符串
* *
* @param separator entry之间的连接符 * @param separator entry之间的连接符
* @param keyValueSeparator kv之间的连接符 * @param keyValueSeparator kv之间的连接符
* @return 连接后的字符串 * @return 连接后的字符串
*/ */
@ -129,9 +174,9 @@ public class MapBuilder<K, V> implements Serializable{
/** /**
* 将map转成字符串 * 将map转成字符串
* *
* @param separator entry之间的连接符 * @param separator entry之间的连接符
* @param keyValueSeparator kv之间的连接符 * @param keyValueSeparator kv之间的连接符
* @param isIgnoreNull 是否忽略null的键和值 * @param isIgnoreNull 是否忽略null的键和值
* @return 连接后的字符串 * @return 连接后的字符串
*/ */
public String join(String separator, final String keyValueSeparator, boolean isIgnoreNull) { public String join(String separator, final String keyValueSeparator, boolean isIgnoreNull) {

View File

@ -155,11 +155,7 @@ public class Calculator {
* @return 优先级 * @return 优先级
*/ */
public boolean compare(char cur, char peek) {// 如果是peek优先级高于cur返回true默认都是peek优先级要低 public boolean compare(char cur, char peek) {// 如果是peek优先级高于cur返回true默认都是peek优先级要低
boolean result = false; return operatPriority[(peek) - 40] >= operatPriority[(cur) - 40];
if (operatPriority[(peek) - 40] >= operatPriority[(cur) - 40]) {
result = true;
}
return result;
} }
/** /**

View File

@ -897,4 +897,14 @@ public class DateUtilTest {
Assert.assertEquals("2021-07-14 23:59:59", DateUtil.format(date, DatePattern.NORM_DATETIME_FORMAT)); Assert.assertEquals("2021-07-14 23:59:59", DateUtil.format(date, DatePattern.NORM_DATETIME_FORMAT));
Assert.assertEquals("2021-07-14 23:59:59", DateUtil.format(date, DatePattern.NORM_DATETIME_PATTERN)); Assert.assertEquals("2021-07-14 23:59:59", DateUtil.format(date, DatePattern.NORM_DATETIME_PATTERN));
} }
@Test
public void formatNormDateTimeFormatterTest(){
String format = DateUtil.format(DateUtil.parse("2021-07-14 10:05:38"), DatePattern.NORM_DATETIME_FORMATTER);
Assert.assertEquals("2021-07-14 10:05:38", format);
format = DateUtil.format(LocalDateTimeUtil.parse("2021-07-14T10:05:38"),
"yyyy-MM-dd HH:mm:ss");
Assert.assertEquals("2021-07-14 10:05:38", format);
}
} }

View File

@ -0,0 +1,28 @@
package cn.hutool.core.map;
import org.junit.Assert;
import org.junit.Test;
import java.util.Map;
public class MapBuilderTest {
@Test
public void conditionPutTest() {
Map<String, String> map = MapBuilder.<String, String>create()
.put(true, "a", "1")
.put(false, "b", "2")
.put(true, "c", () -> getValue(3))
.put(false, "d", () -> getValue(4))
.build();
Assert.assertEquals(map.get("a"), "1");
Assert.assertFalse(map.containsKey("b"));
Assert.assertEquals(map.get("c"), "3");
Assert.assertFalse(map.containsKey("d"));
}
public String getValue(int value) {
return String.valueOf(value);
}
}