1
0
mirror of synced 2026-10-09 08:16:09 +08:00

Compare commits

...

2 Commits

Author SHA1 Message Date
6tail
0a5ea27b20 v1.2.18 修复SolarWeek获取当月第几周的错误。 2022-01-10 20:45:45 +08:00
6tail
3e256ddbd3 v1.2.17 修复九星错误;修复太岁方位错误。 2022-01-07 20:29:04 +08:00
10 changed files with 411 additions and 80 deletions

View File

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

View File

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

View File

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

View File

@@ -833,16 +833,14 @@ public class Lunar {
* @return 节令
*/
public String getJie() {
String jie = "";
for(int i=0,j=JIE_QI_IN_USE.length;i<j;i+=2){
for(int i=0, j=JIE_QI_IN_USE.length; i<j; i+=2){
String key = JIE_QI_IN_USE[i];
Solar d = jieQi.get(key);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
jie = key;
break;
return convertJieQi(key);
}
}
return convertJieQi(jie);
return "";
}
/**
@@ -851,16 +849,14 @@ public class Lunar {
* @return 气令
*/
public String getQi() {
String qi = "";
for(int i=1,j=JIE_QI_IN_USE.length;i<j;i+=2){
for(int i=1, j=JIE_QI_IN_USE.length; i<j; i+=2){
String key = JIE_QI_IN_USE[i];
Solar d = jieQi.get(key);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
qi = key;
break;
return convertJieQi(key);
}
}
return convertJieQi(qi);
return "";
}
/**
@@ -1207,36 +1203,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 gz = getDayInGanZhi();
if ("甲子,乙丑,丙寅,丁卯,戊辰,已巳".contains(gz)) {
p = "震";
} else if ("丙子,丁丑,戊寅,已卯,庚辰,辛巳".contains(gz)) {
p = "离";
} else if ("戊子,已丑,庚寅,辛卯,壬辰,癸巳".contains(gz)) {
p = "中";
} else if ("庚子,辛丑,壬寅,癸卯,甲辰,乙巳".contains(gz)) {
p = "兑";
} else if ("壬子,癸丑,甲寅,乙卯,丙辰,丁巳".contains(gz)) {
p = "坎";
} else{
p = LunarYear.fromYear(this.getYear()).getPositionTaiSui();
int m = monthZhiIndex - LunarUtil.BASE_MONTH_ZHI_INDEX;
if (m < 0) {
m += 12;
}
switch(m) {
case 0:
case 4:
case 8:
p = "艮";
break;
case 2:
case 6:
case 10:
p = "坤";
break;
case 3:
case 7:
case 11:
p = "巽";
break;
default:
p = LunarUtil.POSITION_GAN[monthGanIndex];
}
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() {
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 +2015,92 @@ public class Lunar {
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 值年九星
*/
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 值月九星
*/
public NineStar getMonthNineStar() {
int n = 12;
String yearZhi = getYearZhi();
if ("子午卯酉".contains(yearZhi)) {
n = 18;
} else if ("辰戌丑未".contains(yearZhi)) {
n = 15;
public NineStar getMonthNineStar(int sect) {
int yearZhiIndex;
int monthZhiIndex;
switch (sect) {
case 1:
yearZhiIndex = this.yearZhiIndex;
monthZhiIndex = this.monthZhiIndex;
break;
case 3:
yearZhiIndex = this.yearZhiIndexExact;
monthZhiIndex = this.monthZhiIndexExact;
break;
default:
yearZhiIndex = this.yearZhiIndexByLiChun;
monthZhiIndex = this.monthZhiIndex;
}
int m = month;
if(m < 0){
m = -m;
}
int offset = (n - m) % 9;
if(0 == offset){
offset = 9;
}
return NineStar.fromIndex(offset - 1);
return getMonthNineStar(yearZhiIndex, monthZhiIndex);
}
/**
@@ -2170,15 +2385,13 @@ public class Lunar {
* @return 节气名称
*/
public String getJieQi() {
String name = "";
for (Map.Entry<String, Solar> jq : jieQi.entrySet()) {
Solar d = jq.getValue();
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
name = jq.getKey();
break;
return convertJieQi(jq.getKey());
}
}
return convertJieQi(name);
return "";
}
/**
@@ -2187,8 +2400,13 @@ public class Lunar {
* @return 节气对象
*/
public JieQi getCurrentJieQi() {
String name = getJieQi();
return name.length() > 0 ? new JieQi(name, solar) : null;
for (Map.Entry<String, Solar> jq : jieQi.entrySet()) {
Solar d = jq.getValue();
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
return new JieQi(convertJieQi(jq.getKey()), d);
}
}
return null;
}
/**
@@ -2197,8 +2415,14 @@ public class Lunar {
* @return 节气对象
*/
public JieQi getCurrentJie() {
String name = getJie();
return name.length() > 0 ? new JieQi(name, solar) : null;
for(int i=0, j=JIE_QI_IN_USE.length; i<j; i+=2){
String key = JIE_QI_IN_USE[i];
Solar d = jieQi.get(key);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
return new JieQi(convertJieQi(key), d);
}
}
return null;
}
/**
@@ -2207,8 +2431,14 @@ public class Lunar {
* @return 节气对象
*/
public JieQi getCurrentQi() {
String name = getQi();
return name.length() > 0 ? new JieQi(name, solar) : null;
for(int i=1, j=JIE_QI_IN_USE.length; i<j; i+=2){
String key = JIE_QI_IN_USE[i];
Solar d = jieQi.get(key);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
return new JieQi(convertJieQi(key), d);
}
}
return null;
}
public String toFullString() {
@@ -2745,10 +2975,8 @@ public class Lunar {
public String getHou() {
JieQi jieQi = getPrevJieQi(true);
String name = jieQi.getName();
Calendar currentCalendar = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay());
Solar startSolar = jieQi.getSolar();
Calendar startCalendar = ExactDate.fromYmd(startSolar.getYear(), startSolar.getMonth(), startSolar.getDay());
int days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
int days = ExactDate.getDaysBetween(startSolar.getYear(), startSolar.getMonth(), startSolar.getDay(), solar.getYear(), solar.getMonth(), solar.getDay());
return String.format("%s %s", name, LunarUtil.HOU[(days / 5) % LunarUtil.HOU.length]);
}

View File

@@ -132,6 +132,22 @@ public class LunarMonth {
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
public String toString() {

View File

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

View File

@@ -159,10 +159,11 @@ public class SolarWeek {
public int getIndex() {
Calendar c = ExactDate.fromYmd(year, month, 1);
int firstDayWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (firstDayWeek == 0) {
firstDayWeek = 7;
int offset = firstDayWeek - start;
if(offset < 0) {
offset += 7;
}
return (int) Math.ceil((day + firstDayWeek - start) / 7D);
return (int) Math.ceil((day + offset) / 7D);
}
/**

View File

@@ -62,10 +62,8 @@ public class Yun {
int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substring(11, 16));
// 时辰差
int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
Calendar endCalendar = ExactDate.fromYmd(end.getYear(), end.getMonth(), end.getDay());
Calendar startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay());
// 天数差
int dayDiff = (int) ((endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / (1000 * 3600 * 24));
int dayDiff = ExactDate.getDaysBetween(start.getYear(), start.getMonth(), start.getDay(), end.getYear(), end.getMonth(), end.getDay());
if (hourDiff < 0) {
hourDiff += 12;
dayDiff--;

View File

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

View File

@@ -0,0 +1,74 @@
package test;
import com.nlf.calendar.SolarWeek;
import org.junit.Assert;
import org.junit.Test;
/**
* 阳历周测试
*
* @author 6tail
*/
public class SolarWeekTest {
@Test
public void test() {
SolarWeek week = SolarWeek.fromYmd(2022, 5, 1, 0);
Assert.assertEquals(1, week.getIndex());
}
@Test
public void test1() {
SolarWeek week = SolarWeek.fromYmd(2022, 5, 7, 0);
Assert.assertEquals(1, week.getIndex());
}
@Test
public void test2() {
SolarWeek week = SolarWeek.fromYmd(2022, 5, 8, 0);
Assert.assertEquals(2, week.getIndex());
}
@Test
public void test3() {
SolarWeek week = SolarWeek.fromYmd(2022, 5, 1, 1);
Assert.assertEquals(1, week.getIndex());
}
@Test
public void test4() {
SolarWeek week = SolarWeek.fromYmd(2022, 5, 2, 1);
Assert.assertEquals(2, week.getIndex());
}
@Test
public void test5() {
SolarWeek week = SolarWeek.fromYmd(2022, 5, 8, 1);
Assert.assertEquals(2, week.getIndex());
}
@Test
public void test6() {
SolarWeek week = SolarWeek.fromYmd(2021, 11, 1, 0);
Assert.assertEquals(1, week.getIndex());
}
@Test
public void test7() {
SolarWeek week = SolarWeek.fromYmd(2021, 11, 1, 1);
Assert.assertEquals(1, week.getIndex());
}
@Test
public void test8() {
SolarWeek week = SolarWeek.fromYmd(2021, 5, 2, 2);
Assert.assertEquals(1, week.getIndex());
}
@Test
public void test9() {
SolarWeek week = SolarWeek.fromYmd(2021, 5, 4, 2);
Assert.assertEquals(2, week.getIndex());
}
}