fix chinese date bug

This commit is contained in:
Looly 2020-09-09 15:00:29 +08:00
parent 3c8236af95
commit 1b8e921ab2
4 changed files with 36 additions and 6 deletions

View File

@ -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
-------------------------------------------------------------------------------------------------------------

View File

@ -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);
}
/**

View File

@ -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);
}
/**
* 获得农历月称呼<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] + "";
}
}

View File

@ -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());
}
}