diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cf74e1e8..f0c56fb08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.4.2 (2020-09-08) +# 5.4.2 (2020-09-09) ### 新特性 * 【core 】 lock放在try外边(pr#1050@Github) @@ -28,6 +28,7 @@ * 【http 】 修复GET请求附带body导致变POST的问题 * 【core 】 修复double相等判断问题(pr#175@Gitee) * 【core 】 修复DateSizeUtil.format越界问题(issue#1069@Github) +* 【core 】 修复ChineseDate.getChineseMonth问题(issue#I1UG72@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/date/ChineseDate.java b/hutool-core/src/main/java/cn/hutool/core/date/ChineseDate.java index 98c5bbe21..6c9b10687 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/ChineseDate.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/ChineseDate.java @@ -161,6 +161,16 @@ public class ChineseDate { return this.month; } + /** + * 当前农历月份是否为闰月 + * + * @return 是否为闰月 + * @since 5.4.2 + */ + public boolean isLeapMonth(){ + return ChineseMonth.isLeapMonth(this.year, this.month); + } + /** * 获得农历月份(中文,例如二月,十二月,或者润一月) @@ -168,7 +178,7 @@ public class ChineseDate { * @return 返回农历月份 */ public String getChineseMonth() { - return ChineseMonth.getChineseMonthName(this.leap, this.month, false); + return ChineseMonth.getChineseMonthName(isLeapMonth(), this.month, false); } /** @@ -177,7 +187,7 @@ public class ChineseDate { * @return 返回农历月份称呼 */ public String getChineseMonthName() { - return ChineseMonth.getChineseMonthName(this.leap, this.month, true); + return ChineseMonth.getChineseMonthName(isLeapMonth(), this.month, true); } /** diff --git a/hutool-core/src/main/java/cn/hutool/core/date/chinese/ChineseMonth.java b/hutool-core/src/main/java/cn/hutool/core/date/chinese/ChineseMonth.java index c327ee11b..65faa8022 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/chinese/ChineseMonth.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/chinese/ChineseMonth.java @@ -11,17 +11,27 @@ public class ChineseMonth { private static final String[] MONTH_NAME = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"}; private static final String[] MONTH_NAME_TRADITIONAL = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊"}; + /** + * 当前农历月份是否为闰月 + * + * @return 是否为闰月 + * @since 5.4.2 + */ + public static boolean isLeapMonth(int year, int month){ + return month == LunarInfo.leapMonth(year); + } + /** * 获得农历月称呼
* 当为传统表示时,表示为二月,腊月,或者润正月等 * 当为非传统表示时,二月,十二月,或者润一月等 * - * @param isLeep 是否闰月 + * @param isLeapMonth 是否闰月 * @param month 月份,从1开始 * @param isTraditional 是否传统表示,例如一月传统表示为正月 * @return 返回农历月份称呼 */ - public static String getChineseMonthName(boolean isLeep, int month, boolean isTraditional) { - return (isLeep ? "闰" : "") + (isTraditional ? MONTH_NAME_TRADITIONAL : MONTH_NAME)[month - 1] + "月"; + public static String getChineseMonthName(boolean isLeapMonth, int month, boolean isTraditional) { + return (isLeapMonth ? "闰" : "") + (isTraditional ? MONTH_NAME_TRADITIONAL : MONTH_NAME)[month - 1] + "月"; } } diff --git a/hutool-core/src/test/java/cn/hutool/core/date/ChineseDateTest.java b/hutool-core/src/test/java/cn/hutool/core/date/ChineseDateTest.java index 210282e10..53bd1dca6 100644 --- a/hutool-core/src/test/java/cn/hutool/core/date/ChineseDateTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/date/ChineseDateTest.java @@ -68,4 +68,13 @@ public class ChineseDateTest { String cyclicalYMD = chineseDate.getCyclicalYMD(); Assert.assertEquals("庚子年甲申月癸卯日",cyclicalYMD); } + + @Test + public void getChineseMonthTest(){ + ChineseDate chineseDate = new ChineseDate(2020,6,15); + Assert.assertEquals("六月", chineseDate.getChineseMonth()); + + chineseDate = new ChineseDate(2020,4,15); + Assert.assertEquals("闰四月", chineseDate.getChineseMonth()); + } }