From 622294e74b917795a0291604238496458b7e4062 Mon Sep 17 00:00:00 2001 From: 6tail <6tail@6tail.cn> Date: Thu, 19 Nov 2020 15:02:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8A=82=E6=97=A5=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E9=94=99=E8=AF=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/nlf/calendar/Solar.java | 9 ++--- .../java/com/nlf/calendar/util/SolarUtil.java | 1 + src/test/java/test/FestivalTest.java | 33 +++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 src/test/java/test/FestivalTest.java diff --git a/src/main/java/com/nlf/calendar/Solar.java b/src/main/java/com/nlf/calendar/Solar.java index 9a63ee7..f22b52c 100644 --- a/src/main/java/com/nlf/calendar/Solar.java +++ b/src/main/java/com/nlf/calendar/Solar.java @@ -324,15 +324,10 @@ public List getFestivals(){ l.add(f); } //计算几月第几个星期几对应的节日 - //第几周 - int weekInMonth = calendar.get(Calendar.WEEK_OF_MONTH); + int weeks = (int)Math.ceil(day/7D); //星期几,0代表星期天 int week = getWeek(); - //星期天很奇葩,会多算一周,需要减掉 - if(0==week){ - weekInMonth--; - } - f = SolarUtil.WEEK_FESTIVAL.get(month+"-"+weekInMonth+"-"+week); + f = SolarUtil.WEEK_FESTIVAL.get(month+"-"+weeks+"-"+week); if(null!=f){ l.add(f); } diff --git a/src/main/java/com/nlf/calendar/util/SolarUtil.java b/src/main/java/com/nlf/calendar/util/SolarUtil.java index 73bbaaa..dc9ab24 100644 --- a/src/main/java/com/nlf/calendar/util/SolarUtil.java +++ b/src/main/java/com/nlf/calendar/util/SolarUtil.java @@ -114,6 +114,7 @@ public class SolarUtil{ put("10-13",Collections.nCopies(1,"中国少年先锋队诞辰日")); put("10-25",Collections.nCopies(1,"抗美援朝纪念日")); put("11-12",Collections.nCopies(1,"孙中山诞辰纪念日")); + put("11-17",Collections.nCopies(1,"国际大学生节")); put("11-28",Collections.nCopies(1,"恩格斯诞辰纪念日")); put("12-1",Collections.nCopies(1,"世界艾滋病日")); put("12-12",Collections.nCopies(1,"西安事变纪念日")); diff --git a/src/test/java/test/FestivalTest.java b/src/test/java/test/FestivalTest.java new file mode 100644 index 0000000..02d4f55 --- /dev/null +++ b/src/test/java/test/FestivalTest.java @@ -0,0 +1,33 @@ +package test; + +import com.nlf.calendar.Solar; +import org.junit.Assert; +import org.junit.Test; + +/** + * 节日测试 + * + * @author 6tail + */ +public class FestivalTest { + @Test + public void test() { + Solar solar = Solar.fromYmd(2020, 11, 26); + Assert.assertEquals("[感恩节]", solar.getFestivals() + ""); + + solar = Solar.fromYmd(2020, 6, 21); + Assert.assertEquals("[父亲节]", solar.getFestivals() + ""); + + solar = Solar.fromYmd(2021, 5, 9); + Assert.assertEquals("[母亲节]", solar.getFestivals() + ""); + + solar = Solar.fromYmd(1986, 11, 27); + Assert.assertEquals("[感恩节]", solar.getFestivals() + ""); + + solar = Solar.fromYmd(1985, 6, 16); + Assert.assertEquals("[父亲节]", solar.getFestivals() + ""); + + solar = Solar.fromYmd(1984, 5, 13); + Assert.assertEquals("[母亲节]", solar.getFestivals() + ""); + } +}