diff --git a/CHANGELOG.md b/CHANGELOG.md index 434d8b8c2..bdea9025b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,16 +3,19 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.5 (2021-07-12) +# 5.7.5 (2021-07-14) ### 🐣新特性 * 【core 】 DateUtil增加ceiling重载,可选是否归零毫秒 * 【core 】 IterUtil增加firstMatch方法 * 【core 】 增加NanoId +* 【core 】 MapBuilder增加put方法(pr#367@Gitee) ### 🐞Bug修复 * 【core 】 修复FileUtil.normalize处理上级路径的问题(issue#I3YPEH@Gitee) * 【core 】 修复ClassScanner扫描空包遗漏问题 +* 【core 】 修复FastDatePrinter歧义问题(pr#366@Gitee) +* 【core 】 修复DateUtil.format格式化Instant报错问题(issue#I40CY2@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/date/DatePattern.java b/hutool-core/src/main/java/cn/hutool/core/date/DatePattern.java index 236435ead..8feeac0ce 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/DatePattern.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/DatePattern.java @@ -2,6 +2,7 @@ package cn.hutool.core.date; import cn.hutool.core.date.format.FastDateFormat; +import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Locale; import java.util.TimeZone; @@ -39,7 +40,7 @@ public class DatePattern { /** * 年月格式 {@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 @@ -52,7 +53,7 @@ public class DatePattern { /** * 简单年月格式 {@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 @@ -65,7 +66,7 @@ public class DatePattern { /** * 标准日期格式 {@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 @@ -78,7 +79,7 @@ public class DatePattern { /** * 标准日期格式 {@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 @@ -91,7 +92,7 @@ public class DatePattern { /** * 标准日期格式 {@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 @@ -104,7 +105,7 @@ public class DatePattern { /** * 标准日期时间格式,精确到秒 {@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 @@ -117,7 +118,7 @@ public class DatePattern { /** * 标准日期时间格式,精确到毫秒 {@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 @@ -130,7 +131,7 @@ public class DatePattern { /** * 标准日期格式 {@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日 @@ -143,7 +144,7 @@ public class DatePattern { /** * 标准日期格式 {@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秒 @@ -156,7 +157,7 @@ public class DatePattern { /** * 标准日期格式 {@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 /** @@ -170,7 +171,7 @@ public class DatePattern { /** * 标准日期格式 {@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 @@ -183,7 +184,7 @@ public class DatePattern { /** * 标准日期格式 {@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 @@ -196,7 +197,7 @@ public class DatePattern { /** * 标准日期格式 {@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 @@ -209,7 +210,7 @@ public class DatePattern { /** * 标准日期格式 {@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 /** @@ -283,4 +284,16 @@ public class DatePattern { * 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")); + + /** + * 创建并为 {@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()); + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java b/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java index 2347b18ba..b3994f820 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java @@ -546,7 +546,8 @@ public class DateUtil extends CalendarUtil { if (format.getZone() == null) { format = format.withZone(ZoneId.systemDefault()); } - return format.format(date.toInstant()); + /// return format.format(date.toInstant()); + return TemporalAccessorUtil.format(date.toInstant(), format); } /** diff --git a/hutool-core/src/main/java/cn/hutool/core/date/TemporalAccessorUtil.java b/hutool-core/src/main/java/cn/hutool/core/date/TemporalAccessorUtil.java index e6d33578a..f2918c55e 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/TemporalAccessorUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/TemporalAccessorUtil.java @@ -56,7 +56,6 @@ public class TemporalAccessorUtil extends TemporalUtil{ formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; } - try { return formatter.format(time); } catch (UnsupportedTemporalTypeException e){ @@ -66,6 +65,9 @@ public class TemporalAccessorUtil extends TemporalUtil{ }else if(time instanceof LocalTime && e.getMessage().contains("YearOfEra")){ // 用户传入LocalTime,但是要求格式化带有日期部分,转换为LocalDateTime重试 return formatter.format(((LocalTime) time).atDate(LocalDate.now())); + } else if(time instanceof Instant){ + // 时间戳没有时区信息,赋予默认时区 + return formatter.format(((Instant) time).atZone(ZoneId.systemDefault())); } throw e; } diff --git a/hutool-core/src/main/java/cn/hutool/core/date/format/FastDatePrinter.java b/hutool-core/src/main/java/cn/hutool/core/date/format/FastDatePrinter.java index 4d60f18f4..e4be92723 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/format/FastDatePrinter.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/format/FastDatePrinter.java @@ -37,7 +37,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param timeZone 非空时区{@link TimeZone} * @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); init(); } @@ -211,7 +211,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param indexRef index references * @return parsed token */ - protected String parseToken(final String pattern, final int[] indexRef) { + protected String parseToken(String pattern, int[] indexRef) { final StringBuilder buf = new StringBuilder(); int i = indexRef[0]; @@ -271,7 +271,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param padding the padding required * @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) { case 1: return new UnpaddedNumberField(field); @@ -293,53 +293,53 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param obj the object to format * @return The formatted value. */ - String format(final Object obj) { + String format(Object obj) { if (obj instanceof Date) { return format((Date) obj); } else if (obj instanceof Calendar) { return format((Calendar) obj); } else if (obj instanceof Long) { - return format(((Long) obj)); + return format(((Long) obj).longValue()); } else { throw new IllegalArgumentException("Unknown class: " + (obj == null ? "" : obj.getClass().getName())); } } @Override - public String format(final long millis) { + public String format(long millis) { final Calendar c = Calendar.getInstance(timeZone, locale); c.setTimeInMillis(millis); return applyRulesToString(c); } @Override - public String format(final Date date) { + public String format(Date date) { final Calendar c = Calendar.getInstance(timeZone, locale); c.setTime(date); return applyRulesToString(c); } @Override - public String format(final Calendar calendar) { + public String format(Calendar calendar) { return format(calendar, new StringBuilder(mMaxLengthEstimate)).toString(); } @Override - public B format(final long millis, final B buf) { + public B format(long millis, B buf) { final Calendar c = Calendar.getInstance(timeZone, locale); c.setTimeInMillis(millis); return applyRules(c, buf); } @Override - public B format(final Date date, final B buf) { + public B format(Date date, B buf) { final Calendar c = Calendar.getInstance(timeZone, locale); c.setTime(date); return applyRules(c, buf); } @Override - public B format(Calendar calendar, final B buf) { + public B format(Calendar calendar, B buf) { // do not pass in calendar directly, this will cause TimeZone of FastDatePrinter to be ignored if (!calendar.getTimeZone().equals(timeZone)) { calendar = (Calendar) calendar.clone(); @@ -354,7 +354,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param c the Calender to apply the rules to. * @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(); } @@ -368,7 +368,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param the Appendable class type, usually StringBuilder or StringBuffer. * @return the specified string buffer */ - private B applyRules(final Calendar calendar, final B buf) { + private B applyRules(Calendar calendar, B buf) { try { for (final Rule rule : this.rules) { rule.appendTo(buf, calendar); @@ -398,7 +398,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @throws IOException if there is an IO issue. * @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(); init(); } @@ -409,7 +409,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param buffer the buffer to append to. * @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')); } @@ -422,7 +422,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param buffer the buffer to append to. * @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 // see LANG-1248 if (value < 10000) { @@ -568,7 +568,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * * @param value the string literal */ - StringLiteral(final String value) { + StringLiteral(String value) { mValue = value; } @@ -584,7 +584,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @Override - public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { + public void appendTo(Appendable buffer, Calendar calendar) throws IOException { buffer.append(mValue); } } @@ -604,7 +604,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param field the field * @param values the field values */ - TextField(final int field, final String[] values) { + TextField(int field, String[] values) { mField = field; mValues = values; } @@ -628,7 +628,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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)]); } } @@ -646,7 +646,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * * @param field the field */ - UnpaddedNumberField(final int field) { + UnpaddedNumberField(int field) { mField = field; } @@ -662,7 +662,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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)); } @@ -670,7 +670,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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) { buffer.append((char) (value + '0')); } else if (value < 100) { @@ -708,7 +708,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); } @@ -716,7 +716,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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) { buffer.append((char) (value + '0')); } else { @@ -740,7 +740,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param field the field * @param size size of the output field */ - PaddedNumberField(final int field, final int size) { + PaddedNumberField(int field, int size) { if (size < 3) { // Should use UnpaddedNumberField or TwoDigitNumberField. throw new IllegalArgumentException(); @@ -761,7 +761,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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)); } @@ -769,7 +769,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); } } @@ -787,7 +787,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * * @param field the field */ - TwoDigitNumberField(final int field) { + TwoDigitNumberField(int field) { mField = field; } @@ -803,7 +803,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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)); } @@ -811,7 +811,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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) { appendDigits(buffer, value); } else { @@ -846,7 +846,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); } @@ -854,7 +854,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); } } @@ -885,7 +885,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); } @@ -893,7 +893,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); } } @@ -927,7 +927,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); if (value == 0) { value = calendar.getLeastMaximum(Calendar.HOUR) + 1; @@ -939,7 +939,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); } } @@ -957,7 +957,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * * @param rule the rule */ - TwentyFourHourField(final NumberRule rule) { + TwentyFourHourField(NumberRule rule) { mRule = rule; } @@ -973,7 +973,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); if (value == 0) { value = calendar.getMaximum(Calendar.HOUR_OF_DAY) + 1; @@ -985,7 +985,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); } } @@ -998,7 +998,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { private static class DayInWeekField implements NumberRule { private final NumberRule mRule; - DayInWeekField(final NumberRule rule) { + DayInWeekField(NumberRule rule) { mRule = rule; } @@ -1008,13 +1008,13 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { } @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); mRule.appendTo(buffer, value != Calendar.SUNDAY ? value - 1 : 7); } @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); } } @@ -1037,12 +1037,12 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { } @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()); } @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); } } @@ -1062,7 +1062,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param locale the locale to use * @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); String value = C_TIME_ZONE_DISPLAY_CACHE.get(key); if (value == null) { @@ -1094,7 +1094,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * @param locale the locale * @param style the style */ - TimeZoneNameRule(final TimeZone timeZone, final Locale locale, final int style) { + TimeZoneNameRule(TimeZone timeZone, Locale locale, int style) { mLocale = locale; mStyle = style; @@ -1117,7 +1117,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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(); if (calendar.get(Calendar.DST_OFFSET) != 0) { 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} */ - TimeZoneNumberRule(final boolean colon) { + TimeZoneNumberRule(boolean colon) { mColon = colon; } @@ -1159,7 +1159,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * {@inheritDoc} */ @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); @@ -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. * @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) { case 1: 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) */ - Iso8601_Rule(final int length) { + Iso8601_Rule(int length) { this.length = length; } diff --git a/hutool-core/src/main/java/cn/hutool/core/map/MapBuilder.java b/hutool-core/src/main/java/cn/hutool/core/map/MapBuilder.java index 3787a2ef8..68e14483d 100644 --- a/hutool-core/src/main/java/cn/hutool/core/map/MapBuilder.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/MapBuilder.java @@ -2,6 +2,7 @@ package cn.hutool.core.map; import java.io.Serializable; import java.util.Map; +import java.util.function.Supplier; /** * Map创建类 @@ -10,7 +11,7 @@ import java.util.Map; * @param Value类型 * @since 3.1.1 */ -public class MapBuilder implements Serializable{ +public class MapBuilder implements Serializable { private static final long serialVersionUID = 1L; private final Map map; @@ -30,8 +31,8 @@ public class MapBuilder implements Serializable{ /** * 创建Builder * - * @param Key类型 - * @param Value类型 + * @param Key类型 + * @param Value类型 * @param isLinked true创建LinkedHashMap,false创建HashMap * @return MapBuilder * @since 5.3.0 @@ -74,6 +75,50 @@ public class MapBuilder implements Serializable{ return this; } + /** + * 链式Map创建 + * + * @param k Key类型 + * @param supplier Value类型结果提供方 + * @return 当前类 + * @since 5.7.5 + */ + public MapBuilder put(K k, Supplier supplier) { + return put(k, supplier.get()); + } + + /** + * 链式Map创建 + * + * @param condition put条件 + * @param k Key类型 + * @param v Value类型 + * @return 当前类 + * @since 5.7.5 + */ + public MapBuilder 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 put(boolean condition, K k, Supplier supplier) { + if (condition) { + put(k, supplier); + } + return this; + } + /** * 链式Map创建 * @@ -107,7 +152,7 @@ public class MapBuilder implements Serializable{ /** * 将map转成字符串 * - * @param separator entry之间的连接符 + * @param separator entry之间的连接符 * @param keyValueSeparator kv之间的连接符 * @return 连接字符串 */ @@ -118,7 +163,7 @@ public class MapBuilder implements Serializable{ /** * 将map转成字符串 * - * @param separator entry之间的连接符 + * @param separator entry之间的连接符 * @param keyValueSeparator kv之间的连接符 * @return 连接后的字符串 */ @@ -129,13 +174,13 @@ public class MapBuilder implements Serializable{ /** * 将map转成字符串 * - * @param separator entry之间的连接符 + * @param separator entry之间的连接符 * @param keyValueSeparator kv之间的连接符 - * @param isIgnoreNull 是否忽略null的键和值 + * @param isIgnoreNull 是否忽略null的键和值 * @return 连接后的字符串 */ public String join(String separator, final String keyValueSeparator, boolean isIgnoreNull) { return MapUtil.join(this.map, separator, keyValueSeparator, isIgnoreNull); } -} \ No newline at end of file +} diff --git a/hutool-core/src/main/java/cn/hutool/core/math/Calculator.java b/hutool-core/src/main/java/cn/hutool/core/math/Calculator.java index 5a63258d6..b69fcc594 100644 --- a/hutool-core/src/main/java/cn/hutool/core/math/Calculator.java +++ b/hutool-core/src/main/java/cn/hutool/core/math/Calculator.java @@ -155,11 +155,7 @@ public class Calculator { * @return 优先级 */ public boolean compare(char cur, char peek) {// 如果是peek优先级高于cur,返回true,默认都是peek优先级要低 - boolean result = false; - if (operatPriority[(peek) - 40] >= operatPriority[(cur) - 40]) { - result = true; - } - return result; + return operatPriority[(peek) - 40] >= operatPriority[(cur) - 40]; } /** diff --git a/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java index 3d8b0172a..bb4f7fa4e 100644 --- a/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java @@ -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_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); + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/map/MapBuilderTest.java b/hutool-core/src/test/java/cn/hutool/core/map/MapBuilderTest.java new file mode 100644 index 000000000..71e088950 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/map/MapBuilderTest.java @@ -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 map = MapBuilder.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); + } +}