diff --git a/CHANGELOG.md b/CHANGELOG.md
index a027e3e84..b5587845b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -65,6 +65,7 @@
* 【core 】 FileUtil.getMimeType增加rar、7z支持(issue#I4ZBN0@Gitee)
* 【json 】 JSON修复transient设置无效问题(issue#2212@Github)
* 【core 】 修复IterUtil.getElementType获取结果为null的问题(issue#2222@Github)
+* 【core 】 修复农历转公历在闰月时错误(issue#I4ZSGJ@Gitee)
-------------------------------------------------------------------------------------------------------------
# 5.7.22 (2022-03-01)
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 16ef7f540..4fd9a6525 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
@@ -113,7 +113,8 @@ public class ChineseDate {
}
/**
- * 构造方法传入日期
+ * 构造方法传入日期
+ * 此方法自动判断闰月,如果chineseMonth为本年的闰月,则按照闰月计算
*
* @param chineseYear 农历年
* @param chineseMonth 农历月,1表示一月(正月)
@@ -121,28 +122,28 @@ public class ChineseDate {
* @since 5.2.4
*/
public ChineseDate(int chineseYear, int chineseMonth, int chineseDay) {
- this(chineseYear, chineseMonth, chineseDay, chineseMonth == LunarInfo.leapMonth(chineseYear) + 1);
+ this(chineseYear, chineseMonth, chineseDay, chineseMonth == LunarInfo.leapMonth(chineseYear));
}
/**
- * 构造方法传入日期
+ * 构造方法传入日期
+ * 通过isLeapMonth参数区分是否闰月,如五月是闰月,当isLeapMonth为{@code true}时,表示润五月,{@code false}表示五月
*
* @param chineseYear 农历年
- * @param chineseMonth 农历月,1表示一月(正月)
+ * @param chineseMonth 农历月,1表示一月(正月),如果isLeapMonth为{@code true},1表示润一月
* @param chineseDay 农历日,1表示初一
* @param isLeapMonth 当前月份是否闰月
* @since 5.7.18
*/
public ChineseDate(int chineseYear, int chineseMonth, int chineseDay, boolean isLeapMonth) {
this.day = chineseDay;
- this.month = chineseMonth;
// 当月是闰月的后边的月定义为闰月,如润的是五月,则5表示五月,6表示润五月
this.isLeapMonth = isLeapMonth;
+ // 闰月时,农历月份+1,如6表示润五月
+ this.month = isLeapMonth ? chineseMonth + 1 : chineseMonth;
this.year = chineseYear;
- //先判断传入的月份是不是闰月
- int leapMonth = LunarInfo.leapMonth(chineseYear);
- final DateTime dateTime = lunar2solar(chineseYear, chineseMonth, chineseDay, chineseMonth == leapMonth);
+ final DateTime dateTime = lunar2solar(chineseYear, chineseMonth, chineseDay, isLeapMonth);
if (null != dateTime) {
//初始化公历年
this.gday = dateTime.dayOfMonth();
diff --git a/hutool-core/src/main/java/cn/hutool/core/date/Week.java b/hutool-core/src/main/java/cn/hutool/core/date/Week.java
index afffe7b0c..c6cf0ee76 100644
--- a/hutool-core/src/main/java/cn/hutool/core/date/Week.java
+++ b/hutool-core/src/main/java/cn/hutool/core/date/Week.java
@@ -145,7 +145,7 @@ public enum Week {
/**
* 将 {@link Calendar}星期相关值转换为Week枚举对象
*
- * @param calendarWeekIntValue Calendar中关于Week的int值
+ * @param calendarWeekIntValue Calendar中关于Week的int值,1表示Sunday
* @return Week
* @see #SUNDAY
* @see #MONDAY
@@ -156,10 +156,10 @@ public enum Week {
* @see #SATURDAY
*/
public static Week of(int calendarWeekIntValue) {
- if (calendarWeekIntValue >= ENUMS.length || calendarWeekIntValue < 0) {
+ if (calendarWeekIntValue > ENUMS.length || calendarWeekIntValue < 1) {
return null;
}
- return ENUMS[calendarWeekIntValue];
+ return ENUMS[calendarWeekIntValue - 1];
}
/**
@@ -172,7 +172,7 @@ public enum Week {
*/
public static Week of(String name) throws IllegalArgumentException {
Assert.notBlank(name);
- Week of = of(ArrayUtil.indexOfIgnoreCase(ALIASES, name));
+ Week of = of(ArrayUtil.indexOfIgnoreCase(ALIASES, name) + 1);
if (null == of) {
of = Week.valueOf(name.toUpperCase());
}
@@ -195,8 +195,9 @@ public enum Week {
*/
public static Week of(DayOfWeek dayOfWeek) {
Assert.notNull(dayOfWeek);
- int week = dayOfWeek.ordinal() + 1;
- if (7 == week) {
+ int week = dayOfWeek.getValue() + 1;
+ if(8 == week){
+ // 周日
week = 1;
}
return of(week);
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 20914552c..bb983173a 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
@@ -57,10 +57,12 @@ public class ChineseDateTest {
Assert.assertEquals("六月", chineseDate.getChineseMonth());
chineseDate = new ChineseDate(2020,4,15);
- Assert.assertEquals("四月", chineseDate.getChineseMonth());
+ Assert.assertEquals("2020-06-06 00:00:00", chineseDate.getGregorianDate().toString());
+ Assert.assertEquals("闰四月", chineseDate.getChineseMonth());
chineseDate = new ChineseDate(2020,5,15);
- Assert.assertEquals("闰四月", chineseDate.getChineseMonth());
+ Assert.assertEquals("2020-07-05 00:00:00", chineseDate.getGregorianDate().toString());
+ Assert.assertEquals("五月", chineseDate.getChineseMonth());
}
@Test
@@ -82,12 +84,14 @@ public class ChineseDateTest {
@Test
public void dateTest2(){
+ //noinspection ConstantConditions
ChineseDate date = new ChineseDate(DateUtil.parse("2020-10-19"));
Assert.assertEquals("庚子鼠年 九月初三", date.toString());
}
@Test
public void dateTest2_2(){
+ //noinspection ConstantConditions
ChineseDate date = new ChineseDate(DateUtil.parse("2020-07-20"));
Assert.assertEquals("庚子鼠年 五月三十", date.toString());
}
@@ -95,13 +99,16 @@ public class ChineseDateTest {
@Test
public void dateTest3(){
// 初一,offset为0测试
+ //noinspection ConstantConditions
ChineseDate date = new ChineseDate(DateUtil.parse("2099-03-22"));
Assert.assertEquals("己未羊年 闰二月初一", date.toString());
}
@Test
public void leapMonthTest(){
+ //noinspection ConstantConditions
final ChineseDate c1 = new ChineseDate(DateUtil.parse("2028-05-28"));
+ //noinspection ConstantConditions
final ChineseDate c2 = new ChineseDate(DateUtil.parse("2028-06-27"));
Assert.assertEquals("戊申猴年 五月初五", c1.toString());
@@ -120,14 +127,17 @@ public class ChineseDateTest {
public void day19700101Test(){
// https://gitee.com/dromara/hutool/issues/I4UTPK
Date date = DateUtil.parse("1970-01-01");
+ //noinspection ConstantConditions
ChineseDate chineseDate = new ChineseDate(date);
Assert.assertEquals("己酉鸡年 冬月廿四", chineseDate.toString());
date = DateUtil.parse("1970-01-02");
+ //noinspection ConstantConditions
chineseDate = new ChineseDate(date);
Assert.assertEquals("己酉鸡年 冬月廿五", chineseDate.toString());
date = DateUtil.parse("1970-01-03");
+ //noinspection ConstantConditions
chineseDate = new ChineseDate(date);
Assert.assertEquals("己酉鸡年 冬月廿六", chineseDate.toString());
}
@@ -136,7 +146,18 @@ public class ChineseDateTest {
public void day19000101Test(){
// 1900-01-31之前不支持
Date date = DateUtil.parse("1900-01-31");
+ //noinspection ConstantConditions
ChineseDate chineseDate = new ChineseDate(date);
Assert.assertEquals("庚子鼠年 正月初一", chineseDate.toString());
}
+
+ @Test
+ public void getGregorianDateTest(){
+ // https://gitee.com/dromara/hutool/issues/I4ZSGJ
+ ChineseDate chineseDate = new ChineseDate(1998, 5, 1);
+ Assert.assertEquals("1998-06-24 00:00:00", chineseDate.getGregorianDate().toString());
+
+ chineseDate = new ChineseDate(1998, 5, 1, false);
+ Assert.assertEquals("1998-05-26 00:00:00", chineseDate.getGregorianDate().toString());
+ }
}
diff --git a/hutool-core/src/test/java/cn/hutool/core/date/WeekTest.java b/hutool-core/src/test/java/cn/hutool/core/date/WeekTest.java
index 944232e7a..26f9f95c7 100644
--- a/hutool-core/src/test/java/cn/hutool/core/date/WeekTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/date/WeekTest.java
@@ -37,6 +37,7 @@ public class WeekTest {
Assert.assertEquals(Week.SATURDAY, Week.of("SATURDAY"));
}
+ @Test
public void ofTest2(){
Assert.assertEquals(Week.SUNDAY, Week.of(DayOfWeek.SUNDAY));
Assert.assertEquals(Week.MONDAY, Week.of(DayOfWeek.MONDAY));
diff --git a/hutool-cron/src/test/java/cn/hutool/cron/pattern/CronPatternNextMatchTest.java b/hutool-cron/src/test/java/cn/hutool/cron/pattern/CronPatternNextMatchTest.java
index aad62aeb4..c3a52a1ea 100644
--- a/hutool-cron/src/test/java/cn/hutool/cron/pattern/CronPatternNextMatchTest.java
+++ b/hutool-cron/src/test/java/cn/hutool/cron/pattern/CronPatternNextMatchTest.java
@@ -1,8 +1,10 @@
package cn.hutool.cron.pattern;
import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.date.Week;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
+import org.junit.Ignore;
import org.junit.Test;
import java.util.Calendar;
@@ -19,4 +21,13 @@ public class CronPatternNextMatchTest {
Console.log(DateUtil.date(calendar));
Assert.assertTrue(pattern.match(calendar, true));
}
+
+ @Test
+ @Ignore
+ public void calendarTest(){
+ final Calendar ca = Calendar.getInstance();
+ ca.set(Calendar.DAY_OF_WEEK, Week.SATURDAY.getValue());
+
+ Console.log(DateUtil.date(ca));
+ }
}