mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复FastDatePrinter处理YY错误问题
This commit is contained in:
parent
f4d67ede39
commit
3aa08feee2
@ -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)
|
||||
|
@ -23,13 +23,18 @@ 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
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* 构造,内部使用<br>
|
||||
*
|
||||
@ -59,6 +64,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
|
||||
|
||||
// Parse the pattern
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Returns a list of Rules given a pattern.
|
||||
@ -391,6 +397,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
|
||||
|
||||
// Serializing
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Create the object after serialization. This implementation reinitializes the transient properties.
|
||||
*
|
||||
@ -490,6 +497,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
|
||||
|
||||
// Rules
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* 规则
|
||||
*/
|
||||
@ -691,7 +699,6 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
|
||||
|
||||
/**
|
||||
* Constructs an instance of {@code UnpaddedMonthField}.
|
||||
*
|
||||
*/
|
||||
UnpaddedMonthField() {
|
||||
}
|
||||
@ -1038,7 +1045,12 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
|
||||
|
||||
@Override
|
||||
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
|
||||
@ -1269,6 +1281,7 @@ public class FastDatePrinter extends AbstractDateBasic implements DatePrinter {
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Inner class that acts as a compound key for time zone names.
|
||||
|
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user