mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
Merge pull request #3441 from lanluyug/v6-dev
ChineseDate类添加hashcode与equals方法及其单元测试
This commit is contained in:
commit
1463172c2d
@ -403,6 +403,27 @@ public class ChineseDate {
|
|||||||
return String.format("%s%s年 %s%s", getCyclical(), getChineseZodiac(), getChineseMonthName(), getChineseDay());
|
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
|
// ------------------------------------------------------- private method start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -174,4 +174,23 @@ public class ChineseDateTest {
|
|||||||
chineseDate = new ChineseDate(1998, 5, 1, false);
|
chineseDate = new ChineseDate(1998, 5, 1, false);
|
||||||
Assertions.assertEquals("1998-05-26 00:00:00", chineseDate.getGregorianDate().toString());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user