diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1aff6a9a8..292f40901 100755
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,7 @@
* 【core 】 修复Tailer指定初始读取行数的计算错误问题(issue#IA77ML@Gitee)
* 【http 】 修复getFileNameFromDisposition获取头错误问题(issue#3632@Github)
* 【core 】 修复\n#出现在双引号中解析错误问题(issue#IA8WE0@Gitee)
+* 【core 】 修复FastDatePrinter处理YY错误问题(issue#3641@Github)
-------------------------------------------------------------------------------------------------------------
# 5.8.28(2024-05-29)
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 fb5c2704b..9be99cdbe 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
@@ -23,19 +23,24 @@ import java.util.concurrent.ConcurrentMap;
public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
private static final long serialVersionUID = -6305750172255764887L;
- /** 规则列表. */
+ /**
+ * 规则列表.
+ */
private transient Rule[] rules;
- /** 估算最大长度. */
+ /**
+ * 估算最大长度.
+ */
private transient int mMaxLengthEstimate;
// Constructor
// -----------------------------------------------------------------------
+
/**
* 构造,内部使用
*
- * @param pattern 使用{@link java.text.SimpleDateFormat} 相同的日期格式
+ * @param pattern 使用{@link java.text.SimpleDateFormat} 相同的日期格式
* @param timeZone 非空时区{@link TimeZone}
- * @param locale 非空{@link Locale} 日期地理位置
+ * @param locale 非空{@link Locale} 日期地理位置
*/
public FastDatePrinter(String pattern, TimeZone timeZone, Locale locale) {
super(pattern, timeZone, locale);
@@ -50,7 +55,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
rules = rulesList.toArray(new Rule[0]);
int len = 0;
- for (int i = rules.length; --i >= 0;) {
+ for (int i = rules.length; --i >= 0; ) {
len += rules[i].estimateLength();
}
@@ -59,6 +64,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
// Parse the pattern
// -----------------------------------------------------------------------
+
/**
*
* Returns a list of Rules given a pattern. @@ -207,7 +213,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * Performs the parsing of tokens. *
* - * @param pattern the pattern + * @param pattern the pattern * @param indexRef index references * @return parsed token */ @@ -267,7 +273,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * Gets an appropriate rule for the padding required. * * - * @param field the field to get a rule for + * @param field the field to get a rule for * @param padding the padding required * @return a new rule with the correct padding */ @@ -364,8 +370,8 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * * * @param calendar the calendar to format - * @param buf the buffer to format into - * @param the Appendable class type, usually StringBuilder or StringBuffer. + * @param buf the buffer to format into + * @param the Appendable class type, usually StringBuilder or StringBuffer. * @return the specified string buffer */ private B applyRules(Calendar calendar, B buf) { @@ -380,7 +386,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { } /** - *估算生成的日期字符串长度* Inner class that acts as a compound key for time zone names. @@ -1284,8 +1297,8 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { * * @param timeZone the time zone * @param daylight adjust the style for daylight saving time if {@code true} - * @param style the timezone style - * @param locale the timezone locale + * @param style the timezone style + * @param locale the timezone locale */ TimeZoneDisplayKey(final TimeZone timeZone, final boolean daylight, final int style, final Locale locale) { mTimeZone = timeZone; diff --git a/hutool-core/src/test/java/cn/hutool/core/date/FastDateFormatTest.java b/hutool-core/src/test/java/cn/hutool/core/date/FastDateFormatTest.java new file mode 100644 index 000000000..46d67cbcf --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/date/FastDateFormatTest.java @@ -0,0 +1,58 @@ +package cn.hutool.core.date; + +import cn.hutool.core.date.format.FastDateFormat; +import org.junit.Test; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import static org.junit.Assert.assertEquals; + +public class FastDateFormatTest { + private static final TimeZone timezone = TimeZone.getTimeZone("Etc/Utc"); + + private static FastDateFormat getHutoolInstance(String pattern) { + return FastDateFormat.getInstance(pattern, timezone); + } + + @Test + public void yearTest() { + Date date = DateUtil.date(0L); + + assertEquals( + "1970-01-01 00:00:00", + getHutoolInstance("yyyy-MM-dd HH:mm:ss").format(date) + ); + + assertEquals( + "1970-01-01 00:00:00", + getHutoolInstance("YYYY-MM-dd HH:mm:ss").format(date) + ); + + assertEquals( + "1970", + getHutoolInstance("YYYY").format(date) + ); + + assertEquals( + "70", + getHutoolInstance("yy").format(date) + ); + } + + @Test + public void weekYearTest() { + Date date = DateUtil.date(0L); + + assertEquals( + "70", + new SimpleDateFormat("YY").format(date) + ); + + assertEquals( + "70", + getHutoolInstance("YY").format(date) + ); + } +}