修复ChineseDate传入农历日期非闰月时获取公历错误问题

This commit is contained in:
Looly
2022-11-10 09:58:26 +08:00
parent 3b98f64924
commit ece502c526
3 changed files with 11 additions and 4 deletions

View File

@@ -80,7 +80,7 @@ public class ChineseDate {
year = iYear;
// 计算农历月份
int leapMonth = LunarInfo.leapMonth(iYear); // 闰哪个月,1-12
final int leapMonth = LunarInfo.leapMonth(iYear); // 闰哪个月,1-12
// 用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天
int month;
int daysOfMonth;
@@ -136,6 +136,11 @@ public class ChineseDate {
* @since 5.7.18
*/
public ChineseDate(int chineseYear, int chineseMonth, int chineseDay, boolean isLeapMonth) {
if(chineseMonth != LunarInfo.leapMonth(chineseYear)){
// issue#I5YB1A用户传入的月份可能非闰月此时此参数无效。
isLeapMonth = false;
}
this.day = chineseDay;
// 当月是闰月的后边的月定义为闰月如润的是五月则5表示五月6表示润五月
this.isLeapMonth = isLeapMonth;

View File

@@ -1,13 +1,14 @@
package cn.hutool.core.date.chinese;
import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Test;
public class IssueI5YB1ATest {
@Test
public void chineseDateTest() {
public void chineseDateTest() {
// 四月非闰月因此isLeapMonth参数无效
final ChineseDate date = new ChineseDate(2023, 4, 8, true);
Console.log(date.getGregorianDate());
Assert.assertEquals("2023-05-26 00:00:00", date.getGregorianDate().toString());
}
}