1
0
mirror of synced 2025-11-06 05:00:34 +08:00

v1.3.11 修复命宫、身宫的错误。

This commit is contained in:
6tail
2024-01-30 21:00:30 +08:00
parent 1411d8017f
commit e15a916e55
5 changed files with 90 additions and 32 deletions

View File

@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
<dependency> <dependency>
<groupId>cn.6tail</groupId> <groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId> <artifactId>lunar</artifactId>
<version>1.3.10</version> <version>1.3.11</version>
</dependency> </dependency>
``` ```

View File

@@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
<dependency> <dependency>
<groupId>cn.6tail</groupId> <groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId> <artifactId>lunar</artifactId>
<version>1.3.10</version> <version>1.3.11</version>
</dependency> </dependency>
``` ```

View File

@@ -7,7 +7,7 @@
<groupId>cn.6tail</groupId> <groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId> <artifactId>lunar</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>1.3.10</version> <version>1.3.11</version>
<name>${project.groupId}:${project.artifactId}</name> <name>${project.groupId}:${project.artifactId}</name>
<url>https://github.com/6tail/lunar-java</url> <url>https://github.com/6tail/lunar-java</url>
<description>a calendar library for Solar and Chinese Lunar</description> <description>a calendar library for Solar and Chinese Lunar</description>

View File

@@ -501,27 +501,27 @@ public class EightChar {
public String getMingGong() { public String getMingGong() {
int monthZhiIndex = 0; int monthZhiIndex = 0;
int timeZhiIndex = 0; int timeZhiIndex = 0;
String monthZhi = getMonthZhi();
String timeZhi = getTimeZhi();
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) { for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
String zhi = MONTH_ZHI[i]; if (monthZhi.equals(MONTH_ZHI[i])) {
if (lunar.getMonthZhiExact().equals(zhi)) {
monthZhiIndex = 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; timeZhiIndex = i;
break;
} }
} }
int zhiIndex = 26 - (monthZhiIndex + timeZhiIndex); int offset = monthZhiIndex + timeZhiIndex;
if (zhiIndex > 12) { offset = (offset >= 14 ? 26 : 14) - offset;
zhiIndex -= 12; int ganIndex = (lunar.getYearGanIndexExact() + 1) * 2 + offset;
while (ganIndex > 10) {
ganIndex -= 10;
} }
int jiaZiIndex = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()) - (monthZhiIndex - zhiIndex); return LunarUtil.GAN[ganIndex] + MONTH_ZHI[offset];
if (jiaZiIndex >= 60) {
jiaZiIndex -= 60;
}
if (jiaZiIndex < 0) {
jiaZiIndex += 60;
}
return LunarUtil.JIA_ZI[jiaZiIndex];
} }
/** /**
@@ -541,27 +541,29 @@ public class EightChar {
public String getShenGong() { public String getShenGong() {
int monthZhiIndex = 0; int monthZhiIndex = 0;
int timeZhiIndex = 0; int timeZhiIndex = 0;
String monthZhi = getMonthZhi();
String timeZhi = getTimeZhi();
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) { for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
String zhi = MONTH_ZHI[i]; if (monthZhi.equals(MONTH_ZHI[i])) {
if (lunar.getMonthZhiExact().equals(zhi)) {
monthZhiIndex = 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; timeZhiIndex = i;
break;
} }
} }
int zhiIndex = 2 + monthZhiIndex + timeZhiIndex; int offset = monthZhiIndex + timeZhiIndex;
if (zhiIndex > 12) { while (offset > 12) {
zhiIndex -= 12; offset -= 12;
} }
int jiaZiIndex = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()) - (monthZhiIndex - zhiIndex); int ganIndex = (lunar.getYearGanIndexExact() + 1) * 2 + (offset % 12);
if (jiaZiIndex >= 60) { while (ganIndex > 10) {
jiaZiIndex -= 60; ganIndex -= 10;
} }
if (jiaZiIndex < 0) { return LunarUtil.GAN[ganIndex] + MONTH_ZHI[offset];
jiaZiIndex += 60;
}
return LunarUtil.JIA_ZI[jiaZiIndex];
} }
/** /**

View File

@@ -234,7 +234,7 @@ public class BaZiTest {
@Test @Test
public void testShenGong1() { public void testShenGong1() {
Lunar lunar = new Solar(1994, 12, 6, 2, 0, 0).getLunar(); Lunar lunar = new Solar(1994, 12, 6, 2, 0, 0).getLunar();
Assert.assertEquals("身宫", "", lunar.getEightChar().getShenGong()); Assert.assertEquals("身宫", "", lunar.getEightChar().getShenGong());
} }
@Test @Test
@@ -412,7 +412,7 @@ public class BaZiTest {
Solar solar = new Solar(1986, 5, 29, 13, 37, 0); Solar solar = new Solar(1986, 5, 29, 13, 37, 0);
Lunar lunar = solar.getLunar(); Lunar lunar = solar.getLunar();
EightChar eightChar = lunar.getEightChar(); EightChar eightChar = lunar.getEightChar();
Assert.assertEquals("", eightChar.getShenGong()); Assert.assertEquals("", eightChar.getShenGong());
} }
@Test @Test
@@ -437,4 +437,60 @@ public class BaZiTest {
Assert.assertEquals(expected, actual); Assert.assertEquals(expected, actual);
} }
@Test
public void test20() {
List<Solar> l = Solar.fromBaZi("乙未","己卯","丁丑","甲辰");
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}
List<String> expected = new ArrayList<String>();
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());
}
} }