From 1c3ff0cbc516545ff5ae756072cb08a7eb68c97f Mon Sep 17 00:00:00 2001 From: 6tail <6tail@6tail.cn> Date: Tue, 6 Feb 2024 17:40:46 +0800 Subject: [PATCH] =?UTF-8?q?v1.3.12=20=E5=85=AB=E5=AD=97=E8=BD=AC=E9=98=B3?= =?UTF-8?q?=E5=8E=86=E7=BB=93=E6=9E=9C=E6=8C=89=E6=97=B6=E9=97=B4=E5=85=88?= =?UTF-8?q?=E5=90=8E=E6=8E=92=E5=BA=8F=EF=BC=8C=E8=BD=AC=E6=8D=A2=E9=80=9F?= =?UTF-8?q?=E5=BA=A6=E5=A4=A7=E5=B9=85=E6=8F=90=E5=8D=87=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 ++ README.md | 2 +- README_EN.md | 2 +- pom.xml | 2 +- src/main/java/com/nlf/calendar/EightChar.java | 36 +------- src/main/java/com/nlf/calendar/Solar.java | 90 +++++++++++-------- .../java/com/nlf/calendar/util/LunarUtil.java | 28 ++---- src/test/java/test/BaZiTest.java | 18 ++-- 8 files changed, 84 insertions(+), 99 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3b8bec9 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + + +## [1.3.12] - 2024-02-06 +1. 八字转阳历结果按时间先后排序,转换速度大幅提升。 diff --git a/README.md b/README.md index 4cb20c6..9f130f7 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历) cn.6tail lunar - 1.3.11 + 1.3.12 ``` diff --git a/README_EN.md b/README_EN.md index 20b47f4..415ca40 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.11 + 1.3.12 ``` diff --git a/pom.xml b/pom.xml index 5aa7e52..6ba3993 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ cn.6tail lunar jar - 1.3.11 + 1.3.12 ${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/EightChar.java b/src/main/java/com/nlf/calendar/EightChar.java index c9c91a2..9422eb6 100644 --- a/src/main/java/com/nlf/calendar/EightChar.java +++ b/src/main/java/com/nlf/calendar/EightChar.java @@ -499,22 +499,8 @@ public String getTaiXiNaYin() { * @return 命宫 */ public String getMingGong() { - int monthZhiIndex = 0; - int timeZhiIndex = 0; - String monthZhi = getMonthZhi(); - String timeZhi = getTimeZhi(); - for (int i = 0, j = MONTH_ZHI.length; i < j; i++) { - if (monthZhi.equals(MONTH_ZHI[i])) { - monthZhiIndex = i; - break; - } - } - for (int i = 0, j = MONTH_ZHI.length; i < j; i++) { - if (timeZhi.equals(MONTH_ZHI[i])) { - timeZhiIndex = i; - break; - } - } + int monthZhiIndex = LunarUtil.find(getMonthZhi(), MONTH_ZHI, 0); + int timeZhiIndex = LunarUtil.find(getTimeZhi(), MONTH_ZHI, 0); int offset = monthZhiIndex + timeZhiIndex; offset = (offset >= 14 ? 26 : 14) - offset; int ganIndex = (lunar.getYearGanIndexExact() + 1) * 2 + offset; @@ -539,22 +525,8 @@ public String getMingGongNaYin() { * @return 身宫 */ public String getShenGong() { - int monthZhiIndex = 0; - int timeZhiIndex = 0; - String monthZhi = getMonthZhi(); - String timeZhi = getTimeZhi(); - for (int i = 0, j = MONTH_ZHI.length; i < j; i++) { - if (monthZhi.equals(MONTH_ZHI[i])) { - monthZhiIndex = i; - break; - } - } - for (int i = 0, j = MONTH_ZHI.length; i < j; i++) { - if (timeZhi.equals(LunarUtil.ZHI[i])) { - timeZhiIndex = i; - break; - } - } + int monthZhiIndex = LunarUtil.find(getMonthZhi(), MONTH_ZHI, 0); + int timeZhiIndex = LunarUtil.find(getTimeZhi(), LunarUtil.ZHI, 0); int offset = monthZhiIndex + timeZhiIndex; while (offset > 12) { offset -= 12; diff --git a/src/main/java/com/nlf/calendar/Solar.java b/src/main/java/com/nlf/calendar/Solar.java index 67d9581..c49dee6 100644 --- a/src/main/java/com/nlf/calendar/Solar.java +++ b/src/main/java/com/nlf/calendar/Solar.java @@ -296,49 +296,67 @@ public static List fromBaZi(String yearGanZhi, String monthGanZhi, String public static List fromBaZi(String yearGanZhi, String monthGanZhi, String dayGanZhi, String timeGanZhi, int sect, int baseYear) { sect = (1 == sect) ? 1 : 2; List l = new ArrayList(); - List years = new ArrayList(); - Solar today = fromDate(new Date()); - int offsetYear = (today.getYear() - 4) % 60 - LunarUtil.getJiaZiIndex(yearGanZhi); - if(offsetYear < 0){ - offsetYear += 60; - } - int startYear = today.getYear() - offsetYear - 1; - int minYear = baseYear - 2; - while (startYear >= minYear) { - years.add(startYear); - startYear -= 60; - } - List hours = new ArrayList(2); - String timeZhi = timeGanZhi.substring(1); - for(int i = 1, j = LunarUtil.ZHI.length; i < j; i++){ - if(LunarUtil.ZHI[i].equals(timeZhi)){ - hours.add((i - 1) * 2); - break; - } + // 月地支距寅月的偏移值 + int m = LunarUtil.find(monthGanZhi.substring(1), LunarUtil.ZHI, -1) - 2; + if (m < 0) { + m += 12; } - 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(); + // 月天干要一致 + if (((LunarUtil.find(yearGanZhi.substring(0, 1), LunarUtil.GAN, -1) + 1) * 2 + m) % 10 != LunarUtil.find(monthGanZhi.substring(0,1), LunarUtil.GAN, -1)) { + return l; + } + // 1年的立春是辛酉,序号57 + int y = LunarUtil.getJiaZiIndex(yearGanZhi) - 57; + if (y < 0) { + y += 60; + } + y++; + // 节令偏移值 + m *= 2; + // 时辰地支转时刻,子时按零点算 + int h = LunarUtil.find(timeGanZhi.substring(1), LunarUtil.ZHI, -1) * 2; + int startYear = baseYear - 1; + + // 结束年 + Calendar c = Calendar.getInstance(TIME_ZONE); + c.setTime(new Date()); + c.set(Calendar.MILLISECOND, 0); + int endYear = c.get(Calendar.YEAR); + + while (y <= endYear) { + if (y >= startYear) { + // 立春为寅月的开始 + List jieQiList = new ArrayList(Lunar.fromYmd(y, 1, 1).getJieQiTable().values()); + // 节令推移,年干支和月干支就都匹配上了 + Solar solarTime = jieQiList.get(4 + m); + if (solarTime.getYear() >= baseYear) { + int mi = 0; + int s = 0; + // 日干支和节令干支的偏移值 + Lunar lunar = solarTime.getLunar(); String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact(); + int d = LunarUtil.getJiaZiIndex(dayGanZhi) - LunarUtil.getJiaZiIndex(dgz); + if (d < 0) { + d += 60; + } + if (d > 0) { + // 从节令推移天数 + solarTime = solarTime.next(d); + } else if (h == solarTime.getHour()) { + // 如果正好是节令当天,且小时和节令的小时数相等的极端情况,把分钟和秒钟带上 + mi = solarTime.getMinute(); + s = solarTime.getSecond(); + } + // 验证一下 + Solar solar = Solar.fromYmdHms(solarTime.getYear(), solarTime.getMonth(), solarTime.getDay(), h, mi, s); + lunar = solar.getLunar(); + 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; } - solar = solar.next(1); } } + y += 60; } 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 150db02..2b27b05 100644 --- a/src/main/java/com/nlf/calendar/util/LunarUtil.java +++ b/src/main/java/com/nlf/calendar/util/LunarUtil.java @@ -809,9 +809,13 @@ private static String hex(int n){ * @return 甲子序号 */ public static int getJiaZiIndex(String ganZhi){ - for(int i=0,j=LunarUtil.JIA_ZI.length;i getTimeJi(String dayGanZhi,String timeGanZhi){ * @return 旬下标,0-5 */ protected static int getXunIndex(String ganZhi){ - String gan = ganZhi.substring(0,1); - String zhi = ganZhi.substring(1); - int ganIndex = 0; - int zhiIndex = 0; - for(int i=0,j=GAN.length;i expected = new ArrayList(); - expected.add("1976-09-21 12:00:00"); expected.add("1916-10-06 12:00:00"); + expected.add("1976-09-21 12:00:00"); Assert.assertEquals(expected, actual); } @@ -285,8 +285,8 @@ public void testBaZi2Solar2() { } List expected = new ArrayList(); - expected.add("1999-07-21 16:00:00"); expected.add("1939-08-05 16:00:00"); + expected.add("1999-07-21 16:00:00"); Assert.assertEquals(expected, actual); } @@ -299,8 +299,8 @@ public void testBaZi2Solar3() { } List expected = new ArrayList(); - expected.add("1960-12-17 12:00:00"); expected.add("1901-01-01 12:00:00"); + expected.add("1960-12-17 12:00:00"); Assert.assertEquals(expected, actual); } @@ -313,8 +313,8 @@ public void testBaZi2Solar4() { } List expected = new ArrayList(); - expected.add("2020-07-21 22:00:00"); expected.add("1960-08-05 22:00:00"); + expected.add("2020-07-21 22:00:00"); Assert.assertEquals(expected, actual); } @@ -368,15 +368,15 @@ public void test13(){ @Test public void test14() { - List l = Solar.fromBaZi("癸卯","甲寅","癸丑","甲子", 2, 1843); + 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"); + expected.add("1843-02-09 00:00:00"); + expected.add("2023-02-25 00:00:00"); Assert.assertEquals(expected, actual); } @@ -389,8 +389,8 @@ public void test15() { } List expected = new ArrayList(); - expected.add("1960-01-15 16:00:00"); expected.add("1900-01-29 16:00:00"); + expected.add("1960-01-15 16:00:00"); Assert.assertEquals(expected, actual); } @@ -432,8 +432,8 @@ public void test19() { } List expected = new ArrayList(); - expected.add("1997-03-12 18:00:00"); expected.add("1937-03-27 18:00:00"); + expected.add("1997-03-12 18:00:00"); Assert.assertEquals(expected, actual); }