Merge pull request #3441 from lanluyug/v6-dev

ChineseDate类添加hashcode与equals方法及其单元测试
This commit is contained in:
Golden Looly 2023-12-23 23:19:43 +08:00 committed by GitHub
commit 1463172c2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -403,6 +403,27 @@ public class ChineseDate {
return String.format("%s%s年 %s%s", getCyclical(), getChineseZodiac(), getChineseMonthName(), getChineseDay());
}
@Override
public int hashCode() {
return getChineseDay().hashCode() ^ getChineseMonth().hashCode()
^ Integer.valueOf(getChineseYear()).hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof ChineseDate) {
ChineseDate other = (ChineseDate) obj;
return day == other.day
&& month == other.month
&& year == other.year
&& isLeapMonth == other.isLeapMonth;
}
return false;
}
// ------------------------------------------------------- private method start
/**

View File

@ -174,4 +174,23 @@ public class ChineseDateTest {
chineseDate = new ChineseDate(1998, 5, 1, false);
Assertions.assertEquals("1998-05-26 00:00:00", chineseDate.getGregorianDate().toString());
}
@Test
public void equalsTest(){
// 二月初一
Date date1 = new Date(2023 - 1900, 1, 20);
// 润二月初一
Date date2 = new Date(2023 - 1900, 2, 22);
ChineseDate chineseDate1 = new ChineseDate(date1);
ChineseDate chineseDate2 = new ChineseDate(date2);
ChineseDate chineseDate3 = new ChineseDate(date2);
Assertions.assertEquals("2023-02-01", chineseDate1.toStringNormal());
Assertions.assertEquals("2023-02-01", chineseDate2.toStringNormal());
Assertions.assertEquals("2023-02-01", chineseDate3.toStringNormal());
Assertions.assertNotEquals(chineseDate1, chineseDate2);
Assertions.assertEquals(chineseDate2, chineseDate3);
}
}