This commit is contained in:
Looly 2023-03-31 22:13:06 +08:00
parent 3c33ab7fc2
commit 470e5415f0
2 changed files with 14 additions and 0 deletions

View File

@ -12,6 +12,8 @@
package cn.hutool.core.date;
import cn.hutool.core.lang.Assert;
import java.util.Calendar;
import java.util.Date;
@ -72,6 +74,10 @@ public class Zodiac {
* @return 星座名
*/
public static String getZodiac(final int month, final int day) {
Assert.checkBetween(month,
Month.JANUARY.getValue(),
Month.DECEMBER.getValue(),
"Unsupported month value, must be [0,12]");
// 在分隔日前为前一个星座否则为后一个星座
return day < DAY_ARR[month] ? ZODIACS[month] : ZODIACS[month + 1];
}

View File

@ -31,4 +31,12 @@ public class ZodiacTest {
Assertions.assertNull(Zodiac.getChineseZodiac(1899));
Assertions.assertNull(Zodiac.getChineseZodiac((Calendar) null));
}
@Test
public void getZodiacOutOfRangeTest() {
// https://github.com/dromara/hutool/issues/3036
Assertions.assertThrows(IllegalArgumentException.class, ()->{
DateUtil.getZodiac(Month.UNDECIMBER.getValue(), 10);
});
}
}