浏览代码

fix chinese date bug

Looly 5 年之前
父节点
当前提交
1b8e921ab2

+ 2 - 1
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)
 
 -------------------------------------------------------------------------------------------------------------
 

+ 12 - 2
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);
 	}
 
 	/**

+ 13 - 3
hutool-core/src/main/java/cn/hutool/core/date/chinese/ChineseMonth.java

@@ -12,16 +12,26 @@ public class ChineseMonth {
 	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);
+	}
+
+	/**
 	 * 获得农历月称呼<br>
 	 * 当为传统表示时,表示为二月,腊月,或者润正月等
 	 * 当为非传统表示时,二月,十二月,或者润一月等
 	 *
-	 * @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] + "月";
 	}
 }

+ 9 - 0
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());
+	}
 }