From e15a916e556610648b3c1819ad161370439344b4 Mon Sep 17 00:00:00 2001 From: 6tail <6tail@6tail.cn> Date: Tue, 30 Jan 2024 21:00:30 +0800 Subject: [PATCH] =?UTF-8?q?v1.3.11=20=E4=BF=AE=E5=A4=8D=E5=91=BD=E5=AE=AB?= =?UTF-8?q?=E3=80=81=E8=BA=AB=E5=AE=AB=E7=9A=84=E9=94=99=E8=AF=AF=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/EightChar.java | 56 ++++++++--------- src/test/java/test/BaZiTest.java | 60 ++++++++++++++++++- 5 files changed, 90 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 58b4d44..4cb20c6 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历) cn.6tail lunar - 1.3.10 + 1.3.11 ``` diff --git a/README_EN.md b/README_EN.md index e8660f6..20b47f4 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.10 + 1.3.11 ``` diff --git a/pom.xml b/pom.xml index 2ed65a8..5aa7e52 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ cn.6tail lunar jar - 1.3.10 + 1.3.11 ${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 17a92b6..c9c91a2 100644 --- a/src/main/java/com/nlf/calendar/EightChar.java +++ b/src/main/java/com/nlf/calendar/EightChar.java @@ -501,27 +501,27 @@ public String getTaiXiNaYin() { 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++) { - String zhi = MONTH_ZHI[i]; - if (lunar.getMonthZhiExact().equals(zhi)) { + if (monthZhi.equals(MONTH_ZHI[i])) { monthZhiIndex = i; + break; } - if (lunar.getTimeZhi().equals(zhi)) { + } + for (int i = 0, j = MONTH_ZHI.length; i < j; i++) { + if (timeZhi.equals(MONTH_ZHI[i])) { timeZhiIndex = i; + break; } } - int zhiIndex = 26 - (monthZhiIndex + timeZhiIndex); - if (zhiIndex > 12) { - zhiIndex -= 12; - } - int jiaZiIndex = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()) - (monthZhiIndex - zhiIndex); - if (jiaZiIndex >= 60) { - jiaZiIndex -= 60; - } - if (jiaZiIndex < 0) { - jiaZiIndex += 60; + int offset = monthZhiIndex + timeZhiIndex; + offset = (offset >= 14 ? 26 : 14) - offset; + int ganIndex = (lunar.getYearGanIndexExact() + 1) * 2 + offset; + while (ganIndex > 10) { + ganIndex -= 10; } - return LunarUtil.JIA_ZI[jiaZiIndex]; + return LunarUtil.GAN[ganIndex] + MONTH_ZHI[offset]; } /** @@ -541,27 +541,29 @@ public String getMingGongNaYin() { 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++) { - String zhi = MONTH_ZHI[i]; - if (lunar.getMonthZhiExact().equals(zhi)) { + if (monthZhi.equals(MONTH_ZHI[i])) { monthZhiIndex = i; + break; } - if (lunar.getTimeZhi().equals(zhi)) { + } + for (int i = 0, j = MONTH_ZHI.length; i < j; i++) { + if (timeZhi.equals(LunarUtil.ZHI[i])) { timeZhiIndex = i; + break; } } - int zhiIndex = 2 + monthZhiIndex + timeZhiIndex; - if (zhiIndex > 12) { - zhiIndex -= 12; + int offset = monthZhiIndex + timeZhiIndex; + while (offset > 12) { + offset -= 12; } - int jiaZiIndex = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()) - (monthZhiIndex - zhiIndex); - if (jiaZiIndex >= 60) { - jiaZiIndex -= 60; - } - if (jiaZiIndex < 0) { - jiaZiIndex += 60; + int ganIndex = (lunar.getYearGanIndexExact() + 1) * 2 + (offset % 12); + while (ganIndex > 10) { + ganIndex -= 10; } - return LunarUtil.JIA_ZI[jiaZiIndex]; + return LunarUtil.GAN[ganIndex] + MONTH_ZHI[offset]; } /** diff --git a/src/test/java/test/BaZiTest.java b/src/test/java/test/BaZiTest.java index 6a992ac..6bdd7bb 100644 --- a/src/test/java/test/BaZiTest.java +++ b/src/test/java/test/BaZiTest.java @@ -234,7 +234,7 @@ public void testShenGong() { @Test public void testShenGong1() { Lunar lunar = new Solar(1994, 12, 6, 2, 0, 0).getLunar(); - Assert.assertEquals("身宫", "丁丑", lunar.getEightChar().getShenGong()); + Assert.assertEquals("身宫", "乙丑", lunar.getEightChar().getShenGong()); } @Test @@ -412,7 +412,7 @@ public void test17() { Solar solar = new Solar(1986, 5, 29, 13, 37, 0); Lunar lunar = solar.getLunar(); EightChar eightChar = lunar.getEightChar(); - Assert.assertEquals("辛丑", eightChar.getShenGong()); + Assert.assertEquals("己丑", eightChar.getShenGong()); } @Test @@ -437,4 +437,60 @@ public void test19() { Assert.assertEquals(expected, actual); } + @Test + public void test20() { + List l = Solar.fromBaZi("乙未","己卯","丁丑","甲辰"); + List actual = new ArrayList(); + for (Solar solar : l) { + actual.add(solar.toYmdHms()); + } + + List expected = new ArrayList(); + expected.add("1955-03-17 08:00:00"); + Assert.assertEquals(expected, actual); + } + + @Test + public void test21() { + Lunar lunar = Solar.fromYmdHms(2024, 1, 29, 9, 30, 0).getLunar(); + EightChar eightChar = lunar.getEightChar(); + Assert.assertEquals("命宫", "癸亥", eightChar.getMingGong()); + Assert.assertEquals("身宫", "己未", eightChar.getShenGong()); + } + + @Test + public void test22() { + Assert.assertEquals("身宫", "丙寅", Solar.fromYmdHms(1990, 1, 27, 0, 0, 0).getLunar().getEightChar().getShenGong()); + } + + @Test + public void test23() { + Assert.assertEquals("甲戌", Solar.fromYmdHms(2019, 3, 7, 8, 0, 0).getLunar().getEightChar().getMingGong()); + } + + @Test + public void test24() { + Assert.assertEquals("丁丑", Solar.fromYmdHms(2019, 3, 27, 2, 0, 0).getLunar().getEightChar().getMingGong()); + } + + @Test + public void test25() { + Assert.assertEquals("丙寅", Lunar.fromYmdHms(1994, 5, 20, 18, 0 ,0).getEightChar().getMingGong()); + } + + @Test + public void test26() { + Lunar lunar = Solar.fromYmdHms(1986, 2, 16, 8, 0, 0).getLunar(); + EightChar eightChar = lunar.getEightChar(); + Assert.assertEquals("命宫", "己亥", eightChar.getMingGong()); + Assert.assertEquals("身宫", "乙未", eightChar.getShenGong()); + } + + @Test + public void test27() { + Lunar lunar = Solar.fromYmdHms(1972, 11, 27, 10, 0, 0).getLunar(); + EightChar eightChar = lunar.getEightChar(); + Assert.assertEquals("身宫", "乙巳", eightChar.getShenGong()); + } + }