fix date bug

This commit is contained in:
Looly 2020-10-09 15:25:21 +08:00
parent ecedb5fd82
commit 308fa0f9db
4 changed files with 32 additions and 5 deletions

View File

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

View File

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

View File

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

View File

@ -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@Gitee2023-01-20对应农历腊月29非除夕
ChineseDate chineseDate = new ChineseDate(DateUtil.parseDate("2023-01-20"));
Assert.assertTrue(StrUtil.isEmpty(chineseDate.getFestivals()));
}
}