Browse Source

fix date bug

Looly 5 years ago
parent
commit
308fa0f9db

+ 2 - 1
CHANGELOG.md

@@ -8,9 +8,10 @@
 ### 新特性
 * 【core   】     ConsoleTable代码优化(pr#190@Gitee)
 * 【http   】     HttpRequest增加setProxy重载(pr#190@Gitee)
-* 【http   】     XmlUtil.cleanComment(pr#191@Gitee)
+* 【core   】     XmlUtil.cleanComment(pr#191@Gitee)
 
 ### Bug修复
+* 【core   】     解决农历判断节日未判断大小月导致的问题(issue#I1XHSF@Gitee)
 
 -------------------------------------------------------------------------------------------------------------
 

+ 3 - 3
hutool-core/src/main/java/cn/hutool/core/date/ChineseDate.java

@@ -167,7 +167,7 @@ public class ChineseDate {
 	 * @return 是否为闰月
 	 * @since 5.4.2
 	 */
-	public boolean isLeapMonth(){
+	public boolean isLeapMonth() {
 		return ChineseMonth.isLeapMonth(this.year, this.month);
 	}
 
@@ -230,7 +230,7 @@ public class ChineseDate {
 	 * @return 获得农历节日
 	 */
 	public String getFestivals() {
-		return StrUtil.join(",", LunarFestival.getFestivals(this.month, this.day));
+		return StrUtil.join(",", LunarFestival.getFestivals(this.year, this.month, day));
 	}
 
 	/**
@@ -258,7 +258,7 @@ public class ChineseDate {
 	 * @return 获得天干地支的年月日信息
 	 */
 	public String getCyclicalYMD() {
-		if (gyear >= LunarInfo.BASE_YEAR && gmonth > 0 && gday > 0){
+		if (gyear >= LunarInfo.BASE_YEAR && gmonth > 0 && gday > 0) {
 			return (cyclicalm(gyear, gmonth, gday));
 		}
 		return null;

+ 19 - 1
hutool-core/src/main/java/cn/hutool/core/date/chinese/LunarFestival.java

@@ -17,6 +17,7 @@ public class LunarFestival {
 	// 来自:https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E4%BC%A0%E7%BB%9F%E8%8A%82%E6%97%A5/396100
 	private static final TableMap<Pair<Integer, Integer>, String> L_FTV = new TableMap<>(16);
 	static{
+		// 节日
 		L_FTV.put(new Pair<>(1, 1), "春节");
 		L_FTV.put(new Pair<>(1, 2), "犬日");
 		L_FTV.put(new Pair<>(1, 3), "猪日");
@@ -78,7 +79,6 @@ public class LunarFestival {
 		L_FTV.put(new Pair<>(12, 8), "腊八节");
 		L_FTV.put(new Pair<>(12, 16), "尾牙");
 		L_FTV.put(new Pair<>(12, 23), "小年");
-		L_FTV.put(new Pair<>(12, 29), "除夕");
 		L_FTV.put(new Pair<>(12, 30), "除夕");
 	}
 
@@ -88,6 +88,24 @@ public class LunarFestival {
 	 * @param month 月
 	 * @param day   日
 	 * @return 获得农历节日
+	 * @since 5.4.5
+	 */
+	public static List<String> getFestivals(int year, int month, int day) {
+		// 春节判断,如果12月是小月,则29为除夕,否则30为除夕
+		if(12 == month && 29 == day){
+			if(29 == LunarInfo.monthDays(year, month)){
+				day++;
+			}
+		}
+		return getFestivals(month, day);
+	}
+
+	/**
+	 * 获得节日列表,此方法无法判断月是否为大月或小月
+	 *
+	 * @param month 月
+	 * @param day   日
+	 * @return 获得农历节日
 	 */
 	public static List<String> getFestivals(int month, int day) {
 		return L_FTV.getValues(new Pair<>(month, day));

+ 8 - 0
hutool-core/src/test/java/cn/hutool/core/date/ChineseDateTest.java

@@ -1,5 +1,6 @@
 package cn.hutool.core.date;
 
+import cn.hutool.core.util.StrUtil;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -77,4 +78,11 @@ public class ChineseDateTest {
 		chineseDate = new ChineseDate(2020,4,15);
 		Assert.assertEquals("闰四月", chineseDate.getChineseMonth());
 	}
+
+	@Test
+	public void getFestivalsTest(){
+		// issue#I1XHSF@Gitee,2023-01-20对应农历腊月29,非除夕
+		ChineseDate chineseDate = new ChineseDate(DateUtil.parseDate("2023-01-20"));
+		Assert.assertTrue(StrUtil.isEmpty(chineseDate.getFestivals()));
+	}
 }