diff --git a/src/main/java/com/nlf/calendar/Lunar.java b/src/main/java/com/nlf/calendar/Lunar.java index 5101857..bb0e9ee 100644 --- a/src/main/java/com/nlf/calendar/Lunar.java +++ b/src/main/java/com/nlf/calendar/Lunar.java @@ -30,6 +30,8 @@ public class Lunar{ public static final String QI_SOLAR_SECOND = "大寒"; /** 1弧度对应的角秒 */ private static final double SECOND_PER_RAD = 180 * 3600 / Math.PI; + /** 1天对应的毫秒 */ + private static final long MS_PER_DAY = 86400000L; /** 节气表,国标以冬至为首个节气 */ private static final String[] JIE_QI = {"冬至","小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪"}; private static final double[] NUT_B = {2.1824, -33.75705, 36e-6, -1720, 920, 3.5069, 1256.66393, 11e-6, -132, 57, 1.3375, 16799.4182, -51e-6, -23, 10, 4.3649, -67.5141, 72e-6, 21, -9, 0.04, -628.302, 0, -14, 0, 2.36, 8328.691, 0, 7, 0, 3.46, 1884.966, 0, -5, 2, 5.44, 16833.175, 0, -4, 2, 3.69, 25128.110, 0, -3, 0, 3.55, 628.362, 0, 2, 0}; @@ -2636,7 +2638,7 @@ public ShuJiu getShuJiu(){ return null; } - int days = (int)((currentCalendar.getTimeInMillis()-startCalendar.getTimeInMillis())/(1000*60*60*24)); + int days = (int)((currentCalendar.getTimeInMillis()-startCalendar.getTimeInMillis())/MS_PER_DAY); return new ShuJiu(LunarUtil.NUMBER[days/9+1]+"九",days%9+1); } @@ -2668,7 +2670,7 @@ public Fu getFu(){ return null; } - int days = (int)((currentCalendar.getTimeInMillis()-startCalendar.getTimeInMillis())/(1000*60*60*24)); + int days = (int)((currentCalendar.getTimeInMillis()-startCalendar.getTimeInMillis())/MS_PER_DAY); if(days<10){ return new Fu("初伏",days+1); } @@ -2676,7 +2678,7 @@ public Fu getFu(){ // 第4个庚日,中伏第1天 startCalendar.add(Calendar.DATE,10); - days = (int)((currentCalendar.getTimeInMillis()-startCalendar.getTimeInMillis())/(1000*60*60*24)); + days = (int)((currentCalendar.getTimeInMillis()-startCalendar.getTimeInMillis())/MS_PER_DAY); if(days<10){ return new Fu("中伏",days+1); } @@ -2688,7 +2690,7 @@ public Fu getFu(){ liQiuCalendar.set(liQiu.getYear(),liQiu.getMonth()-1,liQiu.getDay(),0,0,0); liQiuCalendar.set(Calendar.MILLISECOND,0); - days = (int)((currentCalendar.getTimeInMillis()-startCalendar.getTimeInMillis())/(1000*60*60*24)); + days = (int)((currentCalendar.getTimeInMillis()-startCalendar.getTimeInMillis())/MS_PER_DAY); // 末伏 if(liQiuCalendar.compareTo(startCalendar)<=0){ if(days<10){ @@ -2701,11 +2703,46 @@ public Fu getFu(){ } // 末伏第1天 startCalendar.add(Calendar.DATE,10); - days = (int)((currentCalendar.getTimeInMillis()-startCalendar.getTimeInMillis())/(1000*60*60*24)); + days = (int)((currentCalendar.getTimeInMillis()-startCalendar.getTimeInMillis())/MS_PER_DAY); if(days<10){ return new Fu("末伏",days+1); } } return null; } + + /** + * 获取六曜 + * @return 六曜 + */ + public String getLiuYao(){ + return LunarUtil.LIU_YAO[(Math.abs(month)-1+day-1)%6]; + } + + /** + * 获取物候 + * @return 物候 + */ + public String getWuHou(){ + JieQi jieQi = getPrevJieQi(); + String name = jieQi.getName(); + int offset = 0; + for(int i=0,j=JIE_QI.length;i> OTHER_FESTIVAL = new HashMap>(){ private static final long serialVersionUID = -1; { put("1-1",Collections.nCopies(1,"弥勒佛圣诞")); put("1-8",Collections.nCopies(1,"五殿阎罗天子诞")); put("1-9",Collections.nCopies(1,"玉皇上帝诞")); + put("1-13",Collections.nCopies(1,"杨公忌")); put("2-1",Collections.nCopies(1,"一殿秦广王诞")); put("2-2",Collections.nCopies(1,"福德土地正神诞")); put("2-3",Collections.nCopies(1,"文昌帝君诞")); put("2-6",Collections.nCopies(1,"东华帝君诞")); put("2-8",Collections.nCopies(1,"释迦牟尼佛出家")); - put("2-15",Collections.nCopies(1,"释迦牟尼佛般涅槃")); + put("2-11",Collections.nCopies(1,"杨公忌")); + put("2-15",Arrays.asList("释迦牟尼佛涅槃","太上老君诞")); put("2-17",Collections.nCopies(1,"东方杜将军诞")); - put("2-18",Collections.nCopies(1,"至圣先师孔子讳辰")); + put("2-18",Arrays.asList("四殿五官王诞","至圣先师孔子讳辰")); put("2-19",Collections.nCopies(1,"观音大士诞")); put("2-21",Collections.nCopies(1,"普贤菩萨诞")); put("3-1",Collections.nCopies(1,"二殿楚江王诞")); put("3-3",Collections.nCopies(1,"玄天上帝诞")); put("3-8",Collections.nCopies(1,"六殿卞城王诞")); - put("3-15",Collections.nCopies(1,"昊天上帝诞")); + put("3-9",Collections.nCopies(1,"杨公忌")); + put("3-12",Collections.nCopies(1,"中央五道诞")); + put("3-15",Arrays.asList("玄坛诞","昊天上帝诞")); put("3-16",Collections.nCopies(1,"准提菩萨诞")); - put("3-19",Collections.nCopies(1,"中岳大帝诞")); + put("3-18",Arrays.asList("中岳大帝诞","后土娘娘诞")); put("3-20",Collections.nCopies(1,"子孙娘娘诞")); put("3-27",Collections.nCopies(1,"七殿泰山王诞")); put("3-28",Collections.nCopies(1,"苍颉至圣先师诞")); put("4-1",Collections.nCopies(1,"八殿都市王诞")); put("4-4",Collections.nCopies(1,"文殊菩萨诞")); - put("4-8",Collections.nCopies(1,"释迦牟尼佛诞")); + put("4-7",Collections.nCopies(1,"杨公忌")); + put("4-8",Arrays.asList("释迦牟尼佛诞","九殿平等王诞")); put("4-14",Collections.nCopies(1,"纯阳祖师诞")); put("4-15",Collections.nCopies(1,"钟离祖师诞")); put("4-17",Collections.nCopies(1,"十殿转轮王诞")); put("4-18",Collections.nCopies(1,"紫徽大帝诞")); put("4-20",Collections.nCopies(1,"眼光圣母诞")); put("5-1",Collections.nCopies(1,"南极长生大帝诞")); + put("5-5",Collections.nCopies(1,"杨公忌")); put("5-8",Collections.nCopies(1,"南方五道诞")); put("5-11",Collections.nCopies(1,"天下都城隍诞")); put("5-12",Collections.nCopies(1,"炳灵公诞")); @@ -193,10 +203,16 @@ public class LunarUtil{ put("5-16",Collections.nCopies(1,"天地元气造化万物之辰")); put("5-18",Collections.nCopies(1,"张天师诞")); put("5-22",Collections.nCopies(1,"孝娥神诞")); - put("6-19",Collections.nCopies(1,"观世音菩萨成道日")); - put("6-24",Collections.nCopies(1,"关帝诞")); + put("6-3",Collections.nCopies(1,"杨公忌")); + put("6-10",Collections.nCopies(1,"金粟如来诞")); + put("6-13",Collections.nCopies(1,"井泉龙王诞")); + put("6-19",Collections.nCopies(1,"观音大士涅槃")); + put("6-23",Collections.nCopies(1,"南方火神诞")); + put("6-24",Arrays.asList("雷祖诞","关帝诞")); + put("7-1",Collections.nCopies(1,"杨公忌")); put("7-7",Collections.nCopies(1,"魁星诞")); - put("7-13",Arrays.asList("长真谭真人诞","大势至菩萨诞")); + put("7-12",Collections.nCopies(1,"长真谭真人诞")); + put("7-13",Collections.nCopies(1,"大势至菩萨诞")); put("7-15",Collections.nCopies(1,"中元节")); put("7-18",Collections.nCopies(1,"西王母诞")); put("7-19",Collections.nCopies(1,"太岁诞")); @@ -212,31 +228,42 @@ public class LunarUtil{ put("8-18",Collections.nCopies(1,"天人兴福之辰")); put("8-23",Collections.nCopies(1,"汉恒候张显王诞")); put("8-24",Collections.nCopies(1,"灶君夫人诞")); - put("8-29",Collections.nCopies(1,"至圣先师孔子诞")); - put("9-1",Collections.nCopies(1,"北斗九星降世")); - put("9-3",Collections.nCopies(1,"五瘟神诞")); - put("9-9",Collections.nCopies(1,"酆都大帝诞")); + put("8-27",Arrays.asList("至圣先师孔子诞","杨公忌")); + put("9-1",Collections.nCopies(1,"北斗九星降")); + put("9-2",Collections.nCopies(1,"北斗九星降")); + put("9-3",Arrays.asList("北斗九星降","五瘟神诞")); + put("9-4",Collections.nCopies(1,"北斗九星降")); + put("9-5",Collections.nCopies(1,"北斗九星降")); + put("9-6",Collections.nCopies(1,"北斗九星降")); + put("9-7",Collections.nCopies(1,"北斗九星降")); + put("9-8",Collections.nCopies(1,"北斗九星降")); + put("9-9",Arrays.asList("北斗九星降","酆都大帝诞")); put("9-13",Collections.nCopies(1,"孟婆尊神诞")); put("9-17",Collections.nCopies(1,"金龙四大王诞")); put("9-19",Collections.nCopies(1,"观世音菩萨出家")); + put("9-25",Collections.nCopies(1,"杨公忌")); put("9-30",Collections.nCopies(1,"药师琉璃光佛诞")); put("10-1",Collections.nCopies(1,"寒衣节")); put("10-3",Collections.nCopies(1,"三茅诞")); put("10-5",Collections.nCopies(1,"达摩祖师诞")); put("10-8",Collections.nCopies(1,"佛涅槃日")); put("10-15",Collections.nCopies(1,"下元节")); + put("10-23",Collections.nCopies(1,"杨公忌")); + put("10-27",Collections.nCopies(1,"北极紫薇大帝降")); put("11-4",Collections.nCopies(1,"至圣先师孔子诞")); put("11-6",Collections.nCopies(1,"西岳大帝诞")); put("11-11",Collections.nCopies(1,"太乙救苦天尊诞")); put("11-17",Collections.nCopies(1,"阿弥陀佛诞")); put("11-19",Collections.nCopies(1,"太阳日宫诞")); + put("11-21",Collections.nCopies(1,"杨公忌")); put("11-23",Collections.nCopies(1,"张仙诞")); + put("11-25",Collections.nCopies(1,"掠刷大夫降")); put("11-26",Collections.nCopies(1,"北方五道诞")); put("12-8",Collections.nCopies(1,"释迦如来成佛之辰")); put("12-16",Collections.nCopies(1,"南岳大帝诞")); + put("12-19",Collections.nCopies(1,"杨公忌")); put("12-21",Collections.nCopies(1,"天猷上帝诞")); - put("12-23",Collections.nCopies(1,"小年")); - put("12-24",Collections.nCopies(1,"子时灶君上天朝玉帝")); + put("12-23",Arrays.asList("小年","五岳神降")); put("12-29",Collections.nCopies(1,"华严菩萨诞")); } }; diff --git a/src/test/java/test/LiuYaoTest.java b/src/test/java/test/LiuYaoTest.java new file mode 100644 index 0000000..6a79aaf --- /dev/null +++ b/src/test/java/test/LiuYaoTest.java @@ -0,0 +1,71 @@ +package test; + +import com.nlf.calendar.Lunar; +import com.nlf.calendar.Solar; +import org.junit.Assert; +import org.junit.Test; + +/** + * 六曜测试 + * + * @author 6tail + */ +public class LiuYaoTest { + + @Test + public void test1(){ + Solar solar = new Solar(2020,4,23); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(lunar.toString(),"佛灭",lunar.getLiuYao()); + } + + @Test + public void test2(){ + Solar solar = new Solar(2021,1,15); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(lunar.toString(),"友引",lunar.getLiuYao()); + } + + @Test + public void test3(){ + Solar solar = new Solar(2017,1,5); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(lunar.toString(),"先胜",lunar.getLiuYao()); + } + + @Test + public void test4(){ + Solar solar = new Solar(2020,4,10); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(lunar.toString(),"友引",lunar.getLiuYao()); + } + + @Test + public void test5(){ + Solar solar = new Solar(2020,6,11); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(lunar.toString(),"大安",lunar.getLiuYao()); + } + + @Test + public void test6(){ + Solar solar = new Solar(2020,6,1); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(lunar.toString(),"先胜",lunar.getLiuYao()); + } + + @Test + public void test7(){ + Solar solar = new Solar(2020,12,8); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(lunar.toString(),"先负",lunar.getLiuYao()); + } + + @Test + public void test8(){ + Solar solar = new Solar(2020,12,11); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(lunar.toString(),"赤口",lunar.getLiuYao()); + } + +} diff --git a/src/test/java/test/WuHouTest.java b/src/test/java/test/WuHouTest.java new file mode 100644 index 0000000..71161ad --- /dev/null +++ b/src/test/java/test/WuHouTest.java @@ -0,0 +1,71 @@ +package test; + +import com.nlf.calendar.Lunar; +import com.nlf.calendar.Solar; +import org.junit.Assert; +import org.junit.Test; + +/** + * 物候测试 + * + * @author 6tail + */ +public class WuHouTest { + + @Test + public void test1(){ + Solar solar = new Solar(2020,4,23); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(solar.toString(),"萍始生",lunar.getWuHou()); + } + + @Test + public void test2(){ + Solar solar = new Solar(2021,1,15); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(solar.toString(),"雉始雊",lunar.getWuHou()); + } + + @Test + public void test3(){ + Solar solar = new Solar(2017,1,5); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(solar.toString(),"雁北乡",lunar.getWuHou()); + } + + @Test + public void test4(){ + Solar solar = new Solar(2020,4,10); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(solar.toString(),"田鼠化为鴽",lunar.getWuHou()); + } + + @Test + public void test5(){ + Solar solar = new Solar(2020,6,11); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(solar.toString(),"鵙始鸣",lunar.getWuHou()); + } + + @Test + public void test6(){ + Solar solar = new Solar(2020,6,1); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(solar.toString(),"麦秋至",lunar.getWuHou()); + } + + @Test + public void test7(){ + Solar solar = new Solar(2020,12,8); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(solar.toString(),"鹖鴠不鸣",lunar.getWuHou()); + } + + @Test + public void test8(){ + Solar solar = new Solar(2020,12,11); + Lunar lunar = solar.getLunar(); + Assert.assertEquals(solar.toString(),"鹖鴠不鸣",lunar.getWuHou()); + } + +}