1
0
mirror of synced 2026-10-22 22:46:11 +08:00

Compare commits

...

1 Commits

Author SHA1 Message Date
6tail
3e256ddbd3 v1.2.17 修复九星错误;修复太岁方位错误。 2022-01-07 20:29:04 +08:00
7 changed files with 300 additions and 51 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.2.16</version> <version>1.2.17</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.2.16</version> <version>1.2.17</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.2.16</version> <version>1.2.17</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

@@ -1207,36 +1207,200 @@ public class Lunar {
} }
/** /**
* 获取日太岁方位 * 获取年太岁方位(默认流派2新年以立春零点起算)
* *
* @return 日太岁方位,如艮 * @return 太岁方位,如艮
*/ */
public String getDayPositionTaiSui() { public String getYearPositionTaiSui() {
return getYearPositionTaiSui(2);
}
/**
* 获取年太岁方位
*
* @param sect 流派:2为新年以立春零点起算;1为新年以正月初一起算;3为新年以立春节气交接的时刻起算
* @return 太岁方位,如艮
*/
public String getYearPositionTaiSui(int sect) {
int yearZhiIndex;
switch (sect) {
case 1:
yearZhiIndex = this.yearZhiIndex;
break;
case 3:
yearZhiIndex = this.yearZhiIndexExact;
break;
default:
yearZhiIndex = this.yearZhiIndexByLiChun;
}
return LunarUtil.POSITION_TAI_SUI_YEAR[yearZhiIndex];
}
/**
* 获取年太岁方位描述(默认流派2新年以立春零点起算)
*
* @return 太岁方位描述,如东北
*/
public String getYearPositionTaiSuiDesc() {
return getYearPositionTaiSuiDesc(2);
}
/**
* 获取年太岁方位描述
*
* @param sect 流派:2为新年以立春零点起算;1为新年以正月初一起算;3为新年以立春节气交接的时刻起算
* @return 太岁方位描述,如东北
*/
public String getYearPositionTaiSuiDesc(int sect) {
return LunarUtil.POSITION_DESC.get(getYearPositionTaiSui(sect));
}
protected String getMonthPositionTaiSui(int monthZhiIndex, int monthGanIndex){
String p; String p;
String gz = getDayInGanZhi(); int m = monthZhiIndex - LunarUtil.BASE_MONTH_ZHI_INDEX;
if ("甲子,乙丑,丙寅,丁卯,戊辰,已巳".contains(gz)) { if (m < 0) {
p = "震"; m += 12;
} else if ("丙子,丁丑,戊寅,已卯,庚辰,辛巳".contains(gz)) { }
p = "离"; switch(m) {
} else if ("戊子,已丑,庚寅,辛卯,壬辰,癸巳".contains(gz)) { case 0:
p = "中"; case 4:
} else if ("庚子,辛丑,壬寅,癸卯,甲辰,乙巳".contains(gz)) { case 8:
p = "兑"; p = "艮";
} else if ("壬子,癸丑,甲寅,乙卯,丙辰,丁巳".contains(gz)) { break;
p = "坎"; case 2:
} else{ case 6:
p = LunarYear.fromYear(this.getYear()).getPositionTaiSui(); case 10:
p = "坤";
break;
case 3:
case 7:
case 11:
p = "巽";
break;
default:
p = LunarUtil.POSITION_GAN[monthGanIndex];
} }
return p; return p;
} }
/** /**
* 获取日太岁方位描述 * 获取月太岁方位(默认流派2新的一月以节交接当天零点起算)
* *
* @return 日太岁方位描述,如东北 * @return 太岁方位,如艮
*/
public String getMonthPositionTaiSui() {
return getMonthPositionTaiSui(2);
}
/**
* 获取月太岁方位
*
* @param sect 流派:2为新的一月以节交接当天零点起算;3为新的一月以节交接准确时刻起算
* @return 太岁方位,如艮
*/
public String getMonthPositionTaiSui(int sect) {
int monthZhiIndex;
int monthGanIndex;
switch (sect) {
case 3:
monthZhiIndex = this.monthZhiIndexExact;
monthGanIndex = this.monthGanIndexExact;
break;
default:
monthZhiIndex = this.monthZhiIndex;
monthGanIndex = this.monthGanIndex;
}
return getMonthPositionTaiSui(monthZhiIndex, monthGanIndex);
}
/**
* 获取月太岁方位描述(默认流派2新的一月以节交接当天零点起算)
*
* @return 太岁方位描述,如东北
*/
public String getMonthPositionTaiSuiDesc() {
return getMonthPositionTaiSuiDesc(2);
}
/**
* 获取月太岁方位描述
*
* @param sect 流派:2为新的一月以节交接当天零点起算;3为新的一月以节交接准确时刻起算
* @return 太岁方位描述,如东北
*/
public String getMonthPositionTaiSuiDesc(int sect) {
return LunarUtil.POSITION_DESC.get(getMonthPositionTaiSui(sect));
}
protected String getDayPositionTaiSui(String dayInGanZhi, int yearZhiIndex){
String p;
if ("甲子,乙丑,丙寅,丁卯,戊辰,已巳".contains(dayInGanZhi)) {
p = "震";
} else if ("丙子,丁丑,戊寅,已卯,庚辰,辛巳".contains(dayInGanZhi)) {
p = "离";
} else if ("戊子,已丑,庚寅,辛卯,壬辰,癸巳".contains(dayInGanZhi)) {
p = "中";
} else if ("庚子,辛丑,壬寅,癸卯,甲辰,乙巳".contains(dayInGanZhi)) {
p = "兑";
} else if ("壬子,癸丑,甲寅,乙卯,丙辰,丁巳".contains(dayInGanZhi)) {
p = "坎";
} else{
p = LunarUtil.POSITION_TAI_SUI_YEAR[yearZhiIndex];
}
return p;
}
/**
* 获取日太岁方位(默认流派2新年以立春零点起算)
*
* @return 太岁方位,如艮
*/
public String getDayPositionTaiSui() {
return getDayPositionTaiSui(2);
}
/**
* 获取日太岁方位
*
* @param sect 流派:2新年以立春零点起算;1新年以正月初一起算;3新年以立春节气交接的时刻起算
* @return 太岁方位,如艮
*/
public String getDayPositionTaiSui(int sect) {
String dayInGanZhi;
int yearZhiIndex;
switch (sect) {
case 1:
dayInGanZhi = getDayInGanZhi();
yearZhiIndex = this.yearZhiIndex;
break;
case 3:
dayInGanZhi = getDayInGanZhi();
yearZhiIndex = this.yearZhiIndexExact;
break;
default:
dayInGanZhi = getDayInGanZhiExact2();
yearZhiIndex = this.yearZhiIndexByLiChun;
}
return getDayPositionTaiSui(dayInGanZhi, yearZhiIndex);
}
/**
* 获取日太岁方位描述(默认流派2新年以立春零点起算)
*
* @return 太岁方位描述,如东北
*/ */
public String getDayPositionTaiSuiDesc() { public String getDayPositionTaiSuiDesc() {
return LunarUtil.POSITION_DESC.get(getDayPositionTaiSui()); return getDayPositionTaiSuiDesc(2);
}
/**
* 获取日太岁方位描述
*
* @param sect 流派:2新年以立春零点起算;1新年以正月初一起算;3新年以立春节气交接的时刻起算
* @return 太岁方位描述,如东北
*/
public String getDayPositionTaiSuiDesc(int sect) {
return LunarUtil.POSITION_DESC.get(getDayPositionTaiSui(sect));
} }
/** /**
@@ -1855,37 +2019,92 @@ public class Lunar {
return LunarUtil.YUE_XIANG[day]; return LunarUtil.YUE_XIANG[day];
} }
protected NineStar getYearNineStar(String yearInGanZhi) {
int index = LunarUtil.getJiaZiIndex(yearInGanZhi) + 1;
int yearOffset = 0;
if (index != LunarUtil.getJiaZiIndex(this.getYearInGanZhi()) + 1) {
yearOffset = -1;
}
int yuan = ((this.year + yearOffset + 2696) / 60) % 3;
int offset = (62 + yuan * 3 - index) % 9;
if(0 == offset){
offset = 9;
}
return NineStar.fromIndex(offset - 1);
}
/** /**
* 获取值年九星(流年紫白星起例歌诀:年上吉星论甲子,逐年星逆中宫起;上中下作三元汇,一上四中七下兑。) * 获取值年九星(默认流派2新年以立春零点起算。流年紫白星起例歌诀:年上吉星论甲子,逐年星逆中宫起;上中下作三元汇,一上四中七下兑。)
* *
* @return 值年九星 * @return 值年九星
*/ */
public NineStar getYearNineStar() { public NineStar getYearNineStar() {
return LunarYear.fromYear(year).getNineStar(); return getYearNineStar(2);
}
/**
* 获取值年九星(流年紫白星起例歌诀:年上吉星论甲子,逐年星逆中宫起;上中下作三元汇,一上四中七下兑。)
*
* @param sect 流派:2为新年以立春零点起算;1为新年以正月初一起算;3为新年以立春节气交接的时刻起算
* @return 值年九星
*/
public NineStar getYearNineStar(int sect) {
String yearInGanZhi;
switch (sect) {
case 1:
yearInGanZhi = this.getYearInGanZhi();
break;
case 3:
yearInGanZhi = this.getYearInGanZhiExact();
break;
default:
yearInGanZhi = this.getYearInGanZhiByLiChun();
}
return getYearNineStar(yearInGanZhi);
}
protected NineStar getMonthNineStar(int yearZhiIndex, int monthZhiIndex) {
int index = yearZhiIndex % 3;
int n = 27 - (index * 3);
if (monthZhiIndex < LunarUtil.BASE_MONTH_ZHI_INDEX) {
n -= 3;
}
int offset = (n - monthZhiIndex) % 9;
return NineStar.fromIndex(offset);
}
/**
* 获取值月九星(流派2新的一月以节交接当天零点起算。月紫白星歌诀:子午卯酉八白起,寅申巳亥二黑求,辰戌丑未五黄中。)
*
* @return 值月九星
*/
public NineStar getMonthNineStar() {
return getMonthNineStar(2);
} }
/** /**
* 获取值月九星(月紫白星歌诀:子午卯酉八白起,寅申巳亥二黑求,辰戌丑未五黄中。) * 获取值月九星(月紫白星歌诀:子午卯酉八白起,寅申巳亥二黑求,辰戌丑未五黄中。)
* *
* @param sect 流派:2为新的一月以节交接当天零点起算;3为新的一月以节交接准确时刻起算
* @return 值月九星 * @return 值月九星
*/ */
public NineStar getMonthNineStar() { public NineStar getMonthNineStar(int sect) {
int n = 12; int yearZhiIndex;
String yearZhi = getYearZhi(); int monthZhiIndex;
if ("子午卯酉".contains(yearZhi)) { switch (sect) {
n = 18; case 1:
} else if ("辰戌丑未".contains(yearZhi)) { yearZhiIndex = this.yearZhiIndex;
n = 15; monthZhiIndex = this.monthZhiIndex;
break;
case 3:
yearZhiIndex = this.yearZhiIndexExact;
monthZhiIndex = this.monthZhiIndexExact;
break;
default:
yearZhiIndex = this.yearZhiIndexByLiChun;
monthZhiIndex = this.monthZhiIndex;
} }
int m = month; return getMonthNineStar(yearZhiIndex, monthZhiIndex);
if(m < 0){
m = -m;
}
int offset = (n - m) % 9;
if(0 == offset){
offset = 9;
}
return NineStar.fromIndex(offset - 1);
} }
/** /**

View File

@@ -132,6 +132,22 @@ public class LunarMonth {
return LunarUtil.POSITION_DESC.get(getPositionTaiSui()); return LunarUtil.POSITION_DESC.get(getPositionTaiSui());
} }
/**
* 获取月九星
*
* @return 九星
*/
public NineStar getNineStar() {
int index = LunarYear.fromYear(year).getZhiIndex() % 3;
int m = Math.abs(month);
int monthZhiIndex = (13 + m) % 12;
int n = 27 - (index * 3);
if (monthZhiIndex < LunarUtil.BASE_MONTH_ZHI_INDEX) {
n -= 3;
}
int offset = (n - monthZhiIndex) % 9;
return NineStar.fromIndex(offset);
}
@Override @Override
public String toString() { public String toString() {

View File

@@ -214,6 +214,24 @@ public class LunarYear {
return jieQiJulianDays; return jieQiJulianDays;
} }
/**
* 获取天干序号,从0开始
*
* @return 序号
*/
public int getGanIndex() {
return ganIndex;
}
/**
* 获取地支序号,从0开始
*
* @return 序号
*/
public int getZhiIndex() {
return zhiIndex;
}
/** /**
* 获取天干 * 获取天干
* *
@@ -436,15 +454,10 @@ public class LunarYear {
* @return 九星 * @return 九星
*/ */
public NineStar getNineStar() { public NineStar getNineStar() {
int index = LunarUtil.getJiaZiIndex(this.getGanZhi()) + 1; int index = LunarUtil.getJiaZiIndex(getGanZhi()) + 1;
int n = 65; int yuan = ((this.year + 2696) / 60) % 3;
if ("中元".equals(getYuan())) { int offset = (62 + yuan * 3 - index) % 9;
n = 68; if(0 == offset){
} else if ("下元".equals(getYuan())) {
n = 62;
}
int offset = (n - index) % 9;
if (0 == offset) {
offset = 9; offset = 9;
} }
return NineStar.fromIndex(offset - 1); return NineStar.fromIndex(offset - 1);
@@ -586,4 +599,5 @@ public class LunarYear {
public String toFullString() { public String toFullString() {
return year + "年"; return year + "年";
} }
} }

View File

@@ -159,13 +159,13 @@ public class LunarTest {
@Test @Test
public void test23() { public void test23() {
Lunar lunar = Lunar.fromYmd(2022, 1, 1); Lunar lunar = Lunar.fromYmd(2022, 1, 1);
Assert.assertEquals("五黄土玉衡", lunar.getYearNineStar().toString()); Assert.assertEquals("六白金开阳", lunar.getYearNineStar().toString());
} }
@Test @Test
public void test24() { public void test24() {
Lunar lunar = Lunar.fromYmd(2033, 1, 1); Lunar lunar = Lunar.fromYmd(2033, 1, 1);
Assert.assertEquals("三碧木天玑", lunar.getYearNineStar().toString()); Assert.assertEquals("四绿木天权", lunar.getYearNineStar().toString());
} }
@Test @Test