From 586a8d3f1e8a68f03db1daeb03aeec3d21902644 Mon Sep 17 00:00:00 2001 From: 6tail <6tail@6tail.cn> Date: Sat, 11 Mar 2023 18:20:54 +0800 Subject: [PATCH] =?UTF-8?q?v1.3.3=20=E4=BC=98=E5=8C=96=E5=85=AB=E5=AD=97?= =?UTF-8?q?=E8=BD=AC=E9=98=B3=E5=8E=86=EF=BC=9B=E4=BF=AE=E5=A4=8D=E5=B0=91?= =?UTF-8?q?=E9=87=8F=E5=86=9C=E5=8E=86=E5=B9=B4=E7=9A=84=E6=9C=88=E4=BB=BD?= =?UTF-8?q?=E9=94=99=E8=AF=AF=EF=BC=9B=E6=96=B0=E5=A2=9E=E5=86=9C=E5=8E=86?= =?UTF-8?q?=E5=B9=B4=E5=A4=A9=E6=95=B0=E5=92=8C=E5=BD=93=E5=B9=B4=E6=9C=88?= =?UTF-8?q?=E4=BB=BD=E5=88=97=E8=A1=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- README_EN.md | 2 +- pom.xml | 2 +- src/main/java/com/nlf/calendar/Lunar.java | 7 +- src/main/java/com/nlf/calendar/LunarYear.java | 79 +++++--- src/main/java/com/nlf/calendar/Solar.java | 40 +++-- src/main/java/com/nlf/calendar/SolarWeek.java | 6 +- .../com/nlf/calendar/util/HolidayUtil.java | 23 ++- .../java/com/nlf/calendar/util/LunarUtil.java | 4 +- src/test/java/test/BaZiTest.java | 41 +++++ src/test/java/test/LunarTest.java | 168 ++++++++++++++++++ src/test/java/test/YearTest.java | 30 ++++ 12 files changed, 338 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 375fc2a..872b6b6 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历) cn.6tail lunar - 1.3.2 + 1.3.3 ``` diff --git a/README_EN.md b/README_EN.md index d3c92e0..afe6358 100644 --- a/README_EN.md +++ b/README_EN.md @@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar. cn.6tail lunar - 1.3.2 + 1.3.3 ``` diff --git a/pom.xml b/pom.xml index 301dab2..6fac81e 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ cn.6tail lunar jar - 1.3.2 + 1.3.3 ${project.groupId}:${project.artifactId} https://github.com/6tail/lunar-java a calendar library for Solar and Chinese Lunar diff --git a/src/main/java/com/nlf/calendar/Lunar.java b/src/main/java/com/nlf/calendar/Lunar.java index 37a81d9..853117b 100644 --- a/src/main/java/com/nlf/calendar/Lunar.java +++ b/src/main/java/com/nlf/calendar/Lunar.java @@ -197,9 +197,7 @@ public Lunar(int lunarYear, int lunarMonth, int lunarDay, int hour, int minute, public Lunar(Solar solar) { LunarYear ly = LunarYear.fromYear(solar.getYear()); for (LunarMonth m : ly.getMonths()) { - // 初一 - Solar firstDay = Solar.fromJulianDay(m.getFirstJulianDay()); - int days = solar.subtract(firstDay); + int days = solar.subtract(Solar.fromJulianDay(m.getFirstJulianDay())); if (days < m.getDayCount()) { year = m.getYear(); month = m.getMonth(); @@ -2899,8 +2897,7 @@ public ShuJiu getShuJiu() { start = Solar.fromYmd(start.getYear(), start.getMonth(), start.getDay()); } - Solar end = Solar.fromYmd(start.getYear(), start.getMonth(), start.getDay()); - end = end.next(81); + Solar end = Solar.fromYmd(start.getYear(), start.getMonth(), start.getDay()).next(81); if (current.isBefore(start) || !current.isBefore(end)) { return null; diff --git a/src/main/java/com/nlf/calendar/LunarYear.java b/src/main/java/com/nlf/calendar/LunarYear.java index 81b7c50..c1ce26a 100644 --- a/src/main/java/com/nlf/calendar/LunarYear.java +++ b/src/main/java/com/nlf/calendar/LunarYear.java @@ -134,7 +134,9 @@ private void compute() { // 冬至前的初一 double w = ShouXingUtil.calcShuo(jq[0]); if (w > jq[0]) { - w -= 29.5306; + if (currentYear != 41 && currentYear != 193 && currentYear != 288 && currentYear != 345 && currentYear != 918 && currentYear != 1013) { + w -= 29.5306; + } } // 递推每月初一 for (int i = 0, j = hs.length; i < j; i++) { @@ -145,39 +147,41 @@ private void compute() { dayCounts[i] = (int) (hs[i + 1] - hs[i]); } - Integer currentYearLeap = LEAP.get(currentYear); - if (null == currentYearLeap) { - currentYearLeap = -1; - if (hs[13] <= jq[24]) { - int i = 1; - while (hs[i + 1] > jq[2 * i] && i < 13) { - i++; + int prevYear = currentYear - 1; + + int leapYear = -1; + int leapIndex = -1; + + Integer leap = LEAP.get(currentYear); + if (null == leap) { + leap = LEAP.get(prevYear); + if (null == leap) { + if (hs[13] <= jq[24]) { + int i = 1; + while (hs[i + 1] > jq[2 * i] && i < 13) { + i++; + } + leapYear = currentYear; + leapIndex = i; } - currentYearLeap = i; + } else { + leapYear = prevYear; + leapIndex = leap - 12; } + } else { + leapYear = currentYear; + leapIndex = leap; } - int prevYear = currentYear - 1; - Integer prevYearLeap = LEAP.get(prevYear); - prevYearLeap = null == prevYearLeap ? -1 : prevYearLeap - 12; - int y = prevYear; int m = 11; for (int i = 0, j = dayCounts.length; i < j; i++) { int cm = m; - boolean isNextLeap = false; - if (y == currentYear && i == currentYearLeap) { - cm = -cm; - } else if (y == prevYear && i == prevYearLeap) { + if (y == leapYear && i == leapIndex) { cm = -cm; } - if (y == currentYear && i + 1 == currentYearLeap) { - isNextLeap = true; - } else if (y == prevYear && i + 1 == prevYearLeap) { - isNextLeap = true; - } this.months.add(new LunarMonth(y, cm, dayCounts[i], hs[i] + Solar.J2000)); - if (!isNextLeap) { + if (y != leapYear || i + 1 != leapIndex) { m++; } if (m == 13) { @@ -196,6 +200,35 @@ public int getYear() { return year; } + /** + * 获取总天数 + * @return 天数 + */ + public int getDayCount() { + int n = 0; + for (LunarMonth m : months) { + if (m.getYear() == year) { + n += m.getDayCount(); + } + } + return n; + } + + /** + * 获取当年的农历月们 + * + * @return 农历月们 + */ + public List getMonthsInYear() { + List l = new ArrayList(); + for (LunarMonth m : months) { + if (m.getYear() == year) { + l.add(m); + } + } + return l; + } + /** * 获取农历月们 * diff --git a/src/main/java/com/nlf/calendar/Solar.java b/src/main/java/com/nlf/calendar/Solar.java index 3580534..6e1257b 100644 --- a/src/main/java/com/nlf/calendar/Solar.java +++ b/src/main/java/com/nlf/calendar/Solar.java @@ -92,6 +92,9 @@ public Solar(int year, int month, int day, int hour, int minute, int second) { if (day < 1 || day > 31) { throw new IllegalArgumentException(String.format("wrong day %d", day)); } + if (hour < 0 || hour > 23) { + throw new IllegalArgumentException(String.format("wrong hour %d", hour)); + } if (minute < 0 || minute > 59) { throw new IllegalArgumentException(String.format("wrong minute %d", minute)); } @@ -296,31 +299,37 @@ public static List fromBaZi(String yearGanZhi, String monthGanZhi, String offsetYear += 60; } int startYear = today.getYear() - offsetYear - 1; - while (true) { + int minYear = baseYear - 2; + while (startYear >= minYear) { years.add(startYear); startYear -= 60; - if (startYear < baseYear) { - years.add(baseYear); - break; - } } - int hour = 0; + List hours = new ArrayList(2); String timeZhi = timeGanZhi.substring(1); for(int i = 0, j = LunarUtil.ZHI.length; i < j; i++){ if(LunarUtil.ZHI[i].equals(timeZhi)){ - hour = (i - 1) * 2; + hours.add((i - 1) * 2); } } - for (Integer y : years) { - inner: for (int x = 0; x < 3; x++) { - int year = y + x; - Solar solar = fromYmdHms(year, 1, 1, hour, 0, 0); - while (solar.getYear() == year) { + if ("子".equals(timeZhi)) { + hours.add(23); + } + for (int hour: hours) { + for (Integer y : years) { + int maxYear = y + 3; + int year = y; + int month = 11; + if (year < baseYear) { + year = baseYear; + month = 1; + } + Solar solar = fromYmdHms(year, month, 1, hour, 0, 0); + while (solar.getYear() <= maxYear) { Lunar lunar = solar.getLunar(); String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact(); if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && dgz.equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) { l.add(solar); - break inner; + break; } solar = solar.next(1); } @@ -722,8 +731,7 @@ public Solar nextYear(int years) { * @return 阳历 */ public Solar nextMonth(int months) { - SolarMonth month = SolarMonth.fromYm(year, this.month); - month = month.next(months); + SolarMonth month = SolarMonth.fromYm(year, this.month).next(months); int y = month.getYear(); int m = month.getMonth(); int d = day; @@ -803,7 +811,7 @@ public Solar next(int days, boolean onlyWorkday) { Solar solar = fromYmdHms(year, month, day, hour, minute, second); if (days != 0) { int rest = Math.abs(days); - int add = days < 1 ? -1 : 1; + int add = days < 0 ? -1 : 1; while (rest > 0) { solar = solar.next(add); boolean work = true; diff --git a/src/main/java/com/nlf/calendar/SolarWeek.java b/src/main/java/com/nlf/calendar/SolarWeek.java index 639e364..82f6f7e 100644 --- a/src/main/java/com/nlf/calendar/SolarWeek.java +++ b/src/main/java/com/nlf/calendar/SolarWeek.java @@ -214,10 +214,8 @@ public SolarWeek next(int weeks, boolean separateMonth) { week = new SolarWeek(solar.getYear(), solar.getMonth(), solar.getDay(), start); } } else { - int size = SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start); - if (size == index) { - Solar firstDay = week.getFirstDay(); - Solar lastDay = firstDay.next(6); + if (SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start) == index) { + Solar lastDay = week.getFirstDay().next(6); week = new SolarWeek(lastDay.getYear(), lastDay.getMonth(), lastDay.getDay(), start); weekMonth = week.getMonth(); } else { diff --git a/src/main/java/com/nlf/calendar/util/HolidayUtil.java b/src/main/java/com/nlf/calendar/util/HolidayUtil.java index 86f2ab0..b6d4573 100644 --- a/src/main/java/com/nlf/calendar/util/HolidayUtil.java +++ b/src/main/java/com/nlf/calendar/util/HolidayUtil.java @@ -90,12 +90,11 @@ private static String findBackward(String key){ private static List findHolidaysForward(String key){ List l = new ArrayList(); String s = findForward(key); - if(null==s) { - return l; - } - while(s.startsWith(key)){ - l.add(buildHolidayForward(s)); - s = s.substring(SIZE); + if(null!=s) { + while (s.startsWith(key)) { + l.add(buildHolidayForward(s)); + s = s.substring(SIZE); + } } return l; } @@ -103,14 +102,12 @@ private static List findHolidaysForward(String key){ private static List findHolidaysBackward(String key){ List l = new ArrayList(); String s = findBackward(key); - if(null==s) { - return l; - } - while(s.endsWith(key)){ - l.add(buildHolidayBackward(s)); - s = s.substring(0,s.length()-SIZE); + if(null!=s) { + while (s.endsWith(key)) { + l.add(0, buildHolidayBackward(s)); + s = s.substring(0, s.length() - SIZE); + } } - Collections.reverse(l); return l; } diff --git a/src/main/java/com/nlf/calendar/util/LunarUtil.java b/src/main/java/com/nlf/calendar/util/LunarUtil.java index 5db5657..1322d82 100644 --- a/src/main/java/com/nlf/calendar/util/LunarUtil.java +++ b/src/main/java/com/nlf/calendar/util/LunarUtil.java @@ -23,7 +23,7 @@ public class LunarUtil{ public static final String[] WU_HOU = {"蚯蚓结","麋角解","水泉动","雁北乡","鹊始巢","雉始雊","鸡始乳","征鸟厉疾","水泽腹坚","东风解冻","蛰虫始振","鱼陟负冰","獭祭鱼","候雁北","草木萌动","桃始华","仓庚鸣","鹰化为鸠","玄鸟至","雷乃发声","始电","桐始华","田鼠化为鴽","虹始见","萍始生","鸣鸠拂奇羽","戴胜降于桑","蝼蝈鸣","蚯蚓出","王瓜生","苦菜秀","靡草死","麦秋至","螳螂生","鵙始鸣","反舌无声","鹿角解","蜩始鸣","半夏生","温风至","蟋蟀居壁","鹰始挚","腐草为萤","土润溽暑","大雨行时","凉风至","白露降","寒蝉鸣","鹰乃祭鸟","天地始肃","禾乃登","鸿雁来","玄鸟归","群鸟养羞","雷始收声","蛰虫坯户","水始涸","鸿雁来宾","雀入大水为蛤","菊有黄花","豺乃祭兽","草木黄落","蛰虫咸俯","水始冰","地始冻","雉入大水为蜃","虹藏不见","天气上升地气下降","闭塞而成冬","鹖鴠不鸣","虎始交","荔挺出"}; /** 天干 */ public static final String[] GAN = {"","甲","乙","丙","丁","戊","己","庚","辛","壬","癸"}; - /** 喜神方位,《喜神方位歌》:甲己在艮乙庚乾,丙辛坤位喜神安.  丁壬只在离宫坐,戊癸原在在巽间。 */ + /** 喜神方位,《喜神方位歌》:甲己在艮乙庚乾,丙辛坤位喜神安。丁壬只在离宫坐,戊癸原在在巽间。 */ public static final String[] POSITION_XI = {"","艮","乾","坤","离","巽","艮","乾","坤","离","巽"}; /** 阳贵方位,《阳贵神歌》:甲戊坤艮位,乙己是坤坎,庚辛居离艮,丙丁兑与乾,震巽属何日,壬癸贵神安。 */ public static final String[] POSITION_YANG_GUI = {"","坤","坤","兑","乾","艮","坎","离","艮","震","巽"}; @@ -33,7 +33,7 @@ public class LunarUtil{ public static final String[] POSITION_FU = {"","巽","巽","震","震","坎","离","坤","坤","乾","兑"}; /** 流派2《福神方位歌》:甲己正北是福神,丙辛西北乾宫存,乙庚坤位戊癸艮,丁壬巽上好追寻。 */ public static final String[] POSITION_FU_2 = {"","坎","坤","乾","巽","艮","坎","坤","乾","巽","艮"}; - /** 财神方位,《财神方位歌》:甲乙东北是财神,丙丁向在西南寻,戊己正北坐方位, 庚辛正东去安身,壬癸原来正南坐,便是财神方位真。 */ + /** 财神方位,《财神方位歌》:甲乙东北是财神,丙丁向在西南寻,戊己正北坐方位,庚辛正东去安身,壬癸原来正南坐,便是财神方位真。 */ public static final String[] POSITION_CAI = {"","艮","艮","坤","坤","坎","坎","震","震","离","离"}; /** 年太岁方位 */ public static final String[] POSITION_TAI_SUI_YEAR = {"坎","艮","艮","震","巽","巽","离","坤","坤","兑","坎","坎"}; diff --git a/src/test/java/test/BaZiTest.java b/src/test/java/test/BaZiTest.java index 9f89c9f..f6c7f9f 100644 --- a/src/test/java/test/BaZiTest.java +++ b/src/test/java/test/BaZiTest.java @@ -365,4 +365,45 @@ public void test13(){ Assert.assertEquals("日柱", "戊子", eightChar.getDay()); Assert.assertEquals("时柱", "甲寅", eightChar.getTime()); } + + @Test + public void test14() { + List l = Solar.fromBaZi("癸卯","甲寅","癸丑","甲子", 2, 1843); + List actual = new ArrayList(); + for (Solar solar : l) { + actual.add(solar.toYmdHms()); + } + + List expected = new ArrayList(); + expected.add("2023-02-24 23:00:00"); + expected.add("1843-02-08 23:00:00"); + Assert.assertEquals(expected, actual); + } + + @Test + public void test15() { + List l = Solar.fromBaZi("己亥","丁丑","壬寅","戊申"); + List actual = new ArrayList(); + for (Solar solar : l) { + actual.add(solar.toYmdHms()); + } + + List expected = new ArrayList(); + expected.add("1960-01-15 16:00:00"); + expected.add("1900-01-29 16:00:00"); + Assert.assertEquals(expected, actual); + } + + @Test + public void test16() { + List l = Solar.fromBaZi("己亥","丙子","癸酉","庚申"); + List actual = new ArrayList(); + for (Solar solar : l) { + actual.add(solar.toYmdHms()); + } + + List expected = new ArrayList(); + expected.add("1959-12-17 16:00:00"); + Assert.assertEquals(expected, actual); + } } diff --git a/src/test/java/test/LunarTest.java b/src/test/java/test/LunarTest.java index 08d952f..7bee6af 100644 --- a/src/test/java/test/LunarTest.java +++ b/src/test/java/test/LunarTest.java @@ -459,4 +459,172 @@ public void test63() { Assert.assertEquals("1582-10-15", lunar.getSolar().toString()); } + @Test + public void test64() { + Lunar lunar = Lunar.fromYmd(1518, 1, 1); + Assert.assertEquals("1518-02-10", lunar.getSolar().toString()); + } + + @Test + public void test65() { + Lunar lunar = Lunar.fromYmd(793, 1, 1); + Assert.assertEquals("0793-02-15", lunar.getSolar().toString()); + } + + @Test + public void test66() { + Lunar lunar = Lunar.fromYmd(2025, -6, 1); + Assert.assertEquals("2025-07-25", lunar.getSolar().toString()); + } + + @Test + public void test67() { + Lunar lunar = Lunar.fromYmd(2025, 6, 1); + Assert.assertEquals("2025-06-25", lunar.getSolar().toString()); + } + + @Test + public void test68() { + Lunar lunar = Lunar.fromYmd(193, 1, 1); + Assert.assertEquals("0193-02-19", lunar.getSolar().toString()); + } + + @Test + public void test69() { + Lunar lunar = Lunar.fromYmd(288, 1, 1); + Assert.assertEquals("0288-02-19", lunar.getSolar().toString()); + } + + @Test + public void test70() { + Lunar lunar = Lunar.fromYmd(755, 1, 1); + Assert.assertEquals("0755-02-16", lunar.getSolar().toString()); + } + + @Test + public void test71() { + Lunar lunar = Lunar.fromYmd(41, 1, 1); + Assert.assertEquals("0041-02-20", lunar.getSolar().toString()); + } + + @Test + public void test72() { + Lunar lunar = Lunar.fromYmd(57, 1, 1); + Assert.assertEquals("0057-02-23", lunar.getSolar().toString()); + } + + @Test + public void test73() { + Lunar lunar = Lunar.fromYmd(345, 1, 1); + Assert.assertEquals("0345-02-18", lunar.getSolar().toString()); + } + + @Test + public void test74() { + Lunar lunar = Lunar.fromYmd(459, 1, 1); + Assert.assertEquals("0459-02-18", lunar.getSolar().toString()); + } + + @Test + public void test75() { + Lunar lunar = Lunar.fromYmd(497, 1, 1); + Assert.assertEquals("0497-02-18", lunar.getSolar().toString()); + } + + @Test + public void test76() { + Lunar lunar = Lunar.fromYmd(516, 1, 1); + Assert.assertEquals("0516-02-18", lunar.getSolar().toString()); + } + + @Test + public void test77() { + Lunar lunar = Lunar.fromYmd(554, 1, 1); + Assert.assertEquals("0554-02-18", lunar.getSolar().toString()); + } + + @Test + public void test78() { + Lunar lunar = Lunar.fromYmd(698, 1, 1); + Assert.assertEquals("0698-02-16", lunar.getSolar().toString()); + } + + @Test + public void test79() { + Lunar lunar = Lunar.fromYmd(793, 1, 1); + Assert.assertEquals("0793-02-15", lunar.getSolar().toString()); + } + + @Test + public void test80() { + Lunar lunar = Lunar.fromYmd(918, 1, 1); + Assert.assertEquals("0918-02-14", lunar.getSolar().toString()); + } + + @Test + public void test81() { + Lunar lunar = Lunar.fromYmd(1013, 1, 1); + Assert.assertEquals("1013-02-13", lunar.getSolar().toString()); + } + + @Test + public void test82() { + Lunar lunar = Lunar.fromYmd(1051, 1, 1); + Assert.assertEquals("1051-02-14", lunar.getSolar().toString()); + } + + @Test + public void test83() { + Lunar lunar = Lunar.fromYmd(1070, 1, 1); + Assert.assertEquals("1070-02-14", lunar.getSolar().toString()); + } + + @Test + public void test84() { + Lunar lunar = Lunar.fromYmd(1127, 1, 1); + Assert.assertEquals("1127-02-13", lunar.getSolar().toString()); + } + + @Test + public void test85() { + Lunar lunar = Lunar.fromYmd(1146, 1, 1); + Assert.assertEquals("1146-02-13", lunar.getSolar().toString()); + } + + @Test + public void test86() { + Lunar lunar = Lunar.fromYmd(1165, 1, 1); + Assert.assertEquals("1165-02-13", lunar.getSolar().toString()); + } + + @Test + public void test87() { + Lunar lunar = Lunar.fromYmd(1423, 1, 1); + Assert.assertEquals("1423-02-11", lunar.getSolar().toString()); + } + + @Test + public void test88() { + Lunar lunar = Lunar.fromYmd(1442, 1, 1); + Assert.assertEquals("1442-02-11", lunar.getSolar().toString()); + } + + @Test + public void test89() { + Lunar lunar = Lunar.fromYmd(1461, 1, 1); + Assert.assertEquals("1461-02-10", lunar.getSolar().toString()); + } + + @Test + public void test90() { + Lunar lunar = Lunar.fromYmd(1556, 1, 1); + Assert.assertEquals("1556-02-11", lunar.getSolar().toString()); + } + + @Test + public void test91() { + Lunar lunar = Lunar.fromYmd(1537, 1, 1); + Assert.assertEquals("1537-02-10", lunar.getSolar().toString()); + } + } diff --git a/src/test/java/test/YearTest.java b/src/test/java/test/YearTest.java index 3341af5..0f91c52 100644 --- a/src/test/java/test/YearTest.java +++ b/src/test/java/test/YearTest.java @@ -139,4 +139,34 @@ public void test17(){ Assert.assertEquals("八运",year.getYun()); } + @Test + public void test18(){ + LunarYear year = new LunarYear(2023); + Assert.assertEquals(384, year.getDayCount()); + } + + @Test + public void test19(){ + LunarYear year = new LunarYear(1517); + Assert.assertEquals(384, year.getDayCount()); + } + + @Test + public void test20(){ + LunarYear year = new LunarYear(1518); + Assert.assertEquals(355, year.getDayCount()); + } + + @Test + public void test21(){ + LunarYear year = new LunarYear(2022); + Assert.assertEquals(355, year.getDayCount()); + } + + @Test + public void test22(){ + LunarYear year = new LunarYear(2021); + Assert.assertEquals(354, year.getDayCount()); + } + }