From 308fa0f9db19306d63fd38dcaa01528620145546 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 9 Oct 2020 15:25:21 +0800 Subject: [PATCH] fix date bug --- CHANGELOG.md | 3 ++- .../java/cn/hutool/core/date/ChineseDate.java | 6 +++--- .../core/date/chinese/LunarFestival.java | 20 ++++++++++++++++++- .../cn/hutool/core/date/ChineseDateTest.java | 8 ++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80e55e771..7e5bb6fd3 100644 --- a/CHANGELOG.md +++ b/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) ------------------------------------------------------------------------------------------------------------- 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 735f35c2e..368cd302d 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 @@ -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; diff --git a/hutool-core/src/main/java/cn/hutool/core/date/chinese/LunarFestival.java b/hutool-core/src/main/java/cn/hutool/core/date/chinese/LunarFestival.java index 975302100..533c7365b 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/chinese/LunarFestival.java +++ b/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, 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 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 getFestivals(int month, int day) { return L_FTV.getValues(new Pair<>(month, day)); 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 53bd1dca6..877064d84 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 @@ -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())); + } }