修复FastDatePrinter处理YY错误问题

This commit is contained in:
Looly 2024-07-01 16:37:41 +08:00
parent f4d67ede39
commit 3aa08feee2
3 changed files with 99 additions and 27 deletions

View File

@ -28,6 +28,7 @@
* 【core 】 修复Tailer指定初始读取行数的计算错误问题issue#IA77ML@Gitee * 【core 】 修复Tailer指定初始读取行数的计算错误问题issue#IA77ML@Gitee
* 【http 】 修复getFileNameFromDisposition获取头错误问题issue#3632@Github * 【http 】 修复getFileNameFromDisposition获取头错误问题issue#3632@Github
* 【core 】 修复\n#出现在双引号中解析错误问题issue#IA8WE0@Gitee * 【core 】 修复\n#出现在双引号中解析错误问题issue#IA8WE0@Gitee
* 【core 】 修复FastDatePrinter处理YY错误问题issue#3641@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.28(2024-05-29) # 5.8.28(2024-05-29)

View File

@ -23,13 +23,18 @@ import java.util.concurrent.ConcurrentMap;
public class FastDatePrinter extends AbstractDateBasic implements DatePrinter { public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
private static final long serialVersionUID = -6305750172255764887L; private static final long serialVersionUID = -6305750172255764887L;
/** 规则列表. */ /**
* 规则列表.
*/
private transient Rule[] rules; private transient Rule[] rules;
/** 估算最大长度. */ /**
* 估算最大长度.
*/
private transient int mMaxLengthEstimate; private transient int mMaxLengthEstimate;
// Constructor // Constructor
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
/** /**
* 构造内部使用<br> * 构造内部使用<br>
* *
@ -59,6 +64,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
// Parse the pattern // Parse the pattern
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
/** /**
* <p> * <p>
* Returns a list of Rules given a pattern. * Returns a list of Rules given a pattern.
@ -391,6 +397,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
// Serializing // Serializing
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
/** /**
* Create the object after serialization. This implementation reinitializes the transient properties. * Create the object after serialization. This implementation reinitializes the transient properties.
* *
@ -490,6 +497,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
// Rules // Rules
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
/** /**
* 规则 * 规则
*/ */
@ -691,7 +699,6 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
/** /**
* Constructs an instance of {@code UnpaddedMonthField}. * Constructs an instance of {@code UnpaddedMonthField}.
*
*/ */
UnpaddedMonthField() { UnpaddedMonthField() {
} }
@ -1038,7 +1045,12 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
@Override @Override
public void appendTo(Appendable buffer, Calendar calendar) throws IOException { public void appendTo(Appendable buffer, 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 @Override
@ -1269,6 +1281,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* <p> * <p>
* Inner class that acts as a compound key for time zone names. * Inner class that acts as a compound key for time zone names.

View File

@ -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)
);
}
}