From 2c4ddba2ab276781926f811a621e0708e6a6ad7e Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 1 Jul 2024 16:37:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DFastDatePrinter=E5=A4=84?= =?UTF-8?q?=E7=90=86YY=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/date/format/FastDatePrinter.java | 7 ++- .../core/date/format/FastDateFormatTest.java | 58 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 hutool-core/src/test/java/org/dromara/hutool/core/date/format/FastDateFormatTest.java diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/format/FastDatePrinter.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/format/FastDatePrinter.java index c602a1814..139ec8aa2 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/format/FastDatePrinter.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/format/FastDatePrinter.java @@ -967,7 +967,12 @@ public class FastDatePrinter extends SimpleDateBasic implements DatePrinter { @Override public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { - mRule.appendTo(buffer, calendar.getWeekYear()); + int weekYear = calendar.getWeekYear(); + if (mRule instanceof TwoDigitYearField) { + // issue#3641 + weekYear %= 100; + } + mRule.appendTo(buffer, weekYear); } @Override diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/date/format/FastDateFormatTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/date/format/FastDateFormatTest.java new file mode 100644 index 000000000..53f115678 --- /dev/null +++ b/hutool-core/src/test/java/org/dromara/hutool/core/date/format/FastDateFormatTest.java @@ -0,0 +1,58 @@ +package org.dromara.hutool.core.date.format; + +import org.dromara.hutool.core.date.DateUtil; +import org.junit.jupiter.api.Test; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class FastDateFormatTest { + private static final TimeZone timezone = TimeZone.getTimeZone("Etc/Utc"); + + private static FastDateFormat getHutoolInstance(final String pattern) { + return FastDateFormat.getInstance(pattern, timezone); + } + + @Test + public void yearTest() { + final 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() { + final Date date = DateUtil.date(0L); + + assertEquals( + "70", + new SimpleDateFormat("YY").format(date) + ); + + assertEquals( + "70", + getHutoolInstance("YY").format(date) + ); + } +}