1
0
mirror of synced 2025-12-07 22:17:55 +08:00

v1.2.19 新增阴历年、阴历月的推移;修复三候错误;修复道历节日错别字。

This commit is contained in:
6tail
2022-02-16 20:11:13 +08:00
parent 0a5ea27b20
commit 1a15531fb4
9 changed files with 249 additions and 163 deletions

View File

@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.2.18</version>
<version>1.2.19</version>
</dependency>
```
@@ -54,33 +54,3 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
## 文档
请移步至 [http://6tail.cn/calendar/api.html](http://6tail.cn/calendar/api.html "http://6tail.cn/calendar/api.html")
## 更新日志
v1.2.15 南京大XX纪念日更改为国家公祭日新增初候、二候、三候新增三元九运新增道历戊日。
v1.2.14 佛历新增27宿修复宜忌重复的问题修复获取气时缺冬至的问题。
v1.2.13 新增道历Tao。
v1.2.12 修复星宿方位错误。
v1.2.11 修正胎神数据;增加福神流派。
v1.2.9 新增2022年法定假日。
v1.2.8 新增佛历Foto更改Lunar中的getOtherFestivals方法为传统节日。
v1.2.7 修复儒略日转阳历秒数为60的错误。
v1.2.6 新增治水、分饼、耕田、得金、日禄新增时辰LunarTime新增获取当天的所有时辰。
v1.2.5 修复公元0至4年转阴历错误的问题。
v1.2.4 修复闰冬月、闰腊月的问题;修复日历不准的问题;修复物候错误;大运、小运、流年支持自定义轮数。
v1.2.3 修复除夕错误;代码优化。
v1.2.2 修正2016年国庆节数据删除5月23日世界读书日修复v1.2.0和v1.2.1的重大bug。
v1.2.0 支持0001到9999年。

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.18</version>
<version>1.2.19</version>
</dependency>
```

View File

@@ -7,7 +7,7 @@
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<packaging>jar</packaging>
<version>1.2.18</version>
<version>1.2.19</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

@@ -285,11 +285,11 @@ public class Lunar {
}
}
yearGanIndexByLiChun = (g<0?g+10:g)%10;
yearZhiIndexByLiChun = (z<0?z+12:z)%12;
yearGanIndexByLiChun = (g < 0 ? g + 10 : g) % 10;
yearZhiIndexByLiChun = (z < 0 ? z + 12 : z) % 12;
yearGanIndexExact = (gExact<0?gExact+10:gExact)%10;
yearZhiIndexExact = (zExact<0?zExact+12:zExact)%12;
yearGanIndexExact = (gExact < 0 ? gExact + 10 : gExact) % 10;
yearZhiIndexExact = (zExact < 0 ? zExact + 12 : zExact) % 12;
}
/**
@@ -304,7 +304,7 @@ public class Lunar {
//序号:大雪以前-3大雪到小寒之间-2小寒到立春之间-1立春之后0
int index = -3;
for (int i=0;i<size;i+=2) {
for (int i = 0; i < size; i += 2) {
end = jieQi.get(JIE_QI_IN_USE[i]);
String symd = null == start ? ymd : start.toYmd();
if (ymd.compareTo(symd) >= 0 && ymd.compareTo(end.toYmd()) < 0) {
@@ -315,13 +315,13 @@ public class Lunar {
}
//干偏移值(以立春当天起算)
int offset = (((yearGanIndexByLiChun+(index<0?1:0)) % 5 + 1) * 2) % 10;
monthGanIndex = ((index<0?index+10:index) + offset) % 10;
monthZhiIndex = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
int offset = (((yearGanIndexByLiChun + (index < 0 ? 1 : 0)) % 5 + 1) * 2) % 10;
monthGanIndex = ((index < 0 ? index + 10 : index) + offset) % 10;
monthZhiIndex = ((index < 0 ? index + 12 : index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
start = null;
index = -3;
for (int i=0;i<size;i+=2) {
for (int i = 0; i < size; i += 2) {
end = jieQi.get(JIE_QI_IN_USE[i]);
String stime = null == start ? time : start.toYmdHms();
if (time.compareTo(stime) >= 0 && time.compareTo(end.toYmdHms()) < 0) {
@@ -332,9 +332,9 @@ public class Lunar {
}
//干偏移值(以立春交接时刻起算)
offset = (((yearGanIndexExact+(index<0?1:0)) % 5 + 1) * 2) % 10;
monthGanIndexExact = ((index<0?index+10:index) + offset) % 10;
monthZhiIndexExact = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
offset = (((yearGanIndexExact + (index < 0 ? 1 : 0)) % 5 + 1) * 2) % 10;
monthGanIndexExact = ((index < 0 ? index + 10 : index) + offset) % 10;
monthZhiIndexExact = ((index < 0 ? index + 12 : index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
}
/**
@@ -807,17 +807,17 @@ public class Lunar {
return LunarUtil.SEASON[Math.abs(month)];
}
protected String convertJieQi(String name){
protected String convertJieQi(String name) {
String jq = name;
if("DONG_ZHI".equals(jq)){
if ("DONG_ZHI".equals(jq)) {
jq = "冬至";
}else if("DA_HAN".equals(jq)){
} else if ("DA_HAN".equals(jq)) {
jq = "大寒";
}else if("XIAO_HAN".equals(jq)){
} else if ("XIAO_HAN".equals(jq)) {
jq = "小寒";
}else if("LI_CHUN".equals(jq)){
} else if ("LI_CHUN".equals(jq)) {
jq = "立春";
}else if("DA_XUE".equals(jq)){
} else if ("DA_XUE".equals(jq)) {
jq = "大雪";
} else if ("YU_SHUI".equals(jq)) {
jq = "雨水";
@@ -833,7 +833,7 @@ public class Lunar {
* @return 节令
*/
public String getJie() {
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()) {
@@ -849,7 +849,7 @@ public class Lunar {
* @return 气令
*/
public String getQi() {
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()) {
@@ -968,7 +968,7 @@ public class Lunar {
if (null != fs) {
l.addAll(fs);
}
if(solar.toYmd().equals(jieQi.get("清明").next(-1).toYmd())) {
if (solar.toYmd().equals(jieQi.get("清明").next(-1).toYmd())) {
l.add("寒食节");
}
return l;
@@ -995,7 +995,7 @@ public class Lunar {
/**
* 获取日喜神方位
*
* @return 喜神方位,如艮
* @return 方位,如艮
* @deprecated 使用getDayPositionXi
*/
public String getPositionXi() {
@@ -1005,7 +1005,7 @@ public class Lunar {
/**
* 获取喜神方位描述
*
* @return 喜神方位描述,如东北
* @return 方位描述,如东北
* @deprecated 使用getDayPositionXiDesc
*/
public String getPositionXiDesc() {
@@ -1025,7 +1025,7 @@ public class Lunar {
/**
* 获取阳贵神方位描述
*
* @return 阳贵神方位描述,如东北
* @return 方位描述,如东北
* @deprecated 使用getDayPositionYangGuiDesc
*/
public String getPositionYangGuiDesc() {
@@ -1035,7 +1035,7 @@ public class Lunar {
/**
* 获取阴贵神方位
*
* @return 阴贵神方位,如艮
* @return 方位,如艮
* @deprecated 使用getDayPositionYinGui
*/
public String getPositionYinGui() {
@@ -1055,7 +1055,7 @@ public class Lunar {
/**
* 获取福神方位
*
* @return 福神方位,如艮
* @return 方位,如艮
* @deprecated 使用getDayPositionFu
*/
public String getPositionFu() {
@@ -1065,7 +1065,7 @@ public class Lunar {
/**
* 获取福神方位描述
*
* @return 福神方位描述,如东北
* @return 方位描述,如东北
* @deprecated 使用getDayPositionFuDesc
*/
public String getPositionFuDesc() {
@@ -1075,7 +1075,7 @@ public class Lunar {
/**
* 获取财神方位
*
* @return 财神方位,如艮
* @return 方位,如艮
* @deprecated 使用getDayPositionCai
*/
public String getPositionCai() {
@@ -1085,7 +1085,7 @@ public class Lunar {
/**
* 获取财神方位描述
*
* @return 财神方位描述,如东北
* @return 方位描述,如东北
* @deprecated 使用getDayPositionCaiDesc
*/
public String getPositionCaiDesc() {
@@ -1095,7 +1095,7 @@ public class Lunar {
/**
* 获取日喜神方位
*
* @return 喜神方位,如艮
* @return 方位,如艮
*/
public String getDayPositionXi() {
return LunarUtil.POSITION_XI[dayGanIndex + 1];
@@ -1104,7 +1104,7 @@ public class Lunar {
/**
* 获取日喜神方位描述
*
* @return 喜神方位描述,如东北
* @return 方位描述,如东北
*/
public String getDayPositionXiDesc() {
return LunarUtil.POSITION_DESC.get(getDayPositionXi());
@@ -1113,7 +1113,7 @@ public class Lunar {
/**
* 获取日阳贵神方位
*
* @return 阳贵神方位,如艮
* @return 方位,如艮
*/
public String getDayPositionYangGui() {
return LunarUtil.POSITION_YANG_GUI[dayGanIndex + 1];
@@ -1122,7 +1122,7 @@ public class Lunar {
/**
* 获取日阳贵神方位描述
*
* @return 阳贵神方位描述,如东北
* @return 方位描述,如东北
*/
public String getDayPositionYangGuiDesc() {
return LunarUtil.POSITION_DESC.get(getDayPositionYangGui());
@@ -1140,7 +1140,7 @@ public class Lunar {
/**
* 获取日阴贵神方位描述
*
* @return 阴贵神方位描述,如东北
* @return 方位描述,如东北
*/
public String getDayPositionYinGuiDesc() {
return LunarUtil.POSITION_DESC.get(getDayPositionYinGui());
@@ -1159,16 +1159,16 @@ public class Lunar {
* 获取日福神方位
*
* @param sect 流派1或2
* @return 福神方位,如艮
* @return 方位,如艮
*/
public String getDayPositionFu(int sect) {
return (1==sect?LunarUtil.POSITION_FU:LunarUtil.POSITION_FU_2)[dayGanIndex + 1];
return (1 == sect ? LunarUtil.POSITION_FU : LunarUtil.POSITION_FU_2)[dayGanIndex + 1];
}
/**
* 获取日福神方位描述默认流派2
*
* @return 福神方位描述,如东北
* @return 方位描述,如东北
*/
public String getDayPositionFuDesc() {
return getDayPositionFuDesc(2);
@@ -1178,7 +1178,7 @@ public class Lunar {
* 获取日福神方位描述
*
* @param sect 流派1或2
* @return 福神方位描述,如东北
* @return 方位描述,如东北
*/
public String getDayPositionFuDesc(int sect) {
return LunarUtil.POSITION_DESC.get(getDayPositionFu(sect));
@@ -1187,7 +1187,7 @@ public class Lunar {
/**
* 获取日财神方位
*
* @return 财神方位,如艮
* @return 方位,如艮
*/
public String getDayPositionCai() {
return LunarUtil.POSITION_CAI[dayGanIndex + 1];
@@ -1196,7 +1196,7 @@ public class Lunar {
/**
* 获取日财神方位描述
*
* @return 财神方位描述,如东北
* @return 方位描述,如东北
*/
public String getDayPositionCaiDesc() {
return LunarUtil.POSITION_DESC.get(getDayPositionCai());
@@ -1205,7 +1205,7 @@ public class Lunar {
/**
* 获取年太岁方位默认流派2新年以立春零点起算
*
* @return 太岁方位,如艮
* @return 方位,如艮
*/
public String getYearPositionTaiSui() {
return getYearPositionTaiSui(2);
@@ -1215,7 +1215,7 @@ public class Lunar {
* 获取年太岁方位
*
* @param sect 流派2为新年以立春零点起算1为新年以正月初一起算3为新年以立春节气交接的时刻起算
* @return 太岁方位,如艮
* @return 方位,如艮
*/
public String getYearPositionTaiSui(int sect) {
int yearZhiIndex;
@@ -1235,7 +1235,7 @@ public class Lunar {
/**
* 获取年太岁方位描述默认流派2新年以立春零点起算
*
* @return 太岁方位描述,如东北
* @return 方位描述,如东北
*/
public String getYearPositionTaiSuiDesc() {
return getYearPositionTaiSuiDesc(2);
@@ -1245,32 +1245,27 @@ public class Lunar {
* 获取年太岁方位描述
*
* @param sect 流派2为新年以立春零点起算1为新年以正月初一起算3为新年以立春节气交接的时刻起算
* @return 太岁方位描述,如东北
* @return 方位描述,如东北
*/
public String getYearPositionTaiSuiDesc(int sect) {
return LunarUtil.POSITION_DESC.get(getYearPositionTaiSui(sect));
}
protected String getMonthPositionTaiSui(int monthZhiIndex, int monthGanIndex){
protected String getMonthPositionTaiSui(int monthZhiIndex, int monthGanIndex) {
String p;
int m = monthZhiIndex - LunarUtil.BASE_MONTH_ZHI_INDEX;
if (m < 0) {
m += 12;
}
switch(m) {
m = m % 4;
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:
@@ -1328,7 +1323,7 @@ public class Lunar {
return LunarUtil.POSITION_DESC.get(getMonthPositionTaiSui(sect));
}
protected String getDayPositionTaiSui(String dayInGanZhi, int yearZhiIndex){
protected String getDayPositionTaiSui(String dayInGanZhi, int yearZhiIndex) {
String p;
if ("甲子,乙丑,丙寅,丁卯,戊辰,已巳".contains(dayInGanZhi)) {
p = "";
@@ -1340,7 +1335,7 @@ public class Lunar {
p = "";
} else if ("壬子,癸丑,甲寅,乙卯,丙辰,丁巳".contains(dayInGanZhi)) {
p = "";
} else{
} else {
p = LunarUtil.POSITION_TAI_SUI_YEAR[yearZhiIndex];
}
return p;
@@ -1469,7 +1464,7 @@ public class Lunar {
* @return 福神方位,如艮
*/
public String getTimePositionFu(int sect) {
return (1==sect?LunarUtil.POSITION_FU:LunarUtil.POSITION_FU_2)[timeGanIndex + 1];
return (1 == sect ? LunarUtil.POSITION_FU : LunarUtil.POSITION_FU_2)[timeGanIndex + 1];
}
/**
@@ -2023,7 +2018,7 @@ public class Lunar {
}
int yuan = ((this.year + yearOffset + 2696) / 60) % 3;
int offset = (62 + yuan * 3 - index) % 9;
if(0 == offset){
if (0 == offset) {
offset = 9;
}
return NineStar.fromIndex(offset - 1);
@@ -2032,7 +2027,7 @@ public class Lunar {
/**
* 获取值年九星默认流派2新年以立春零点起算。流年紫白星起例歌诀年上吉星论甲子逐年星逆中宫起上中下作三元汇一上四中七下兑。
*
* @return 值年九星
* @return 九星
*/
public NineStar getYearNineStar() {
return getYearNineStar(2);
@@ -2042,7 +2037,7 @@ public class Lunar {
* 获取值年九星(流年紫白星起例歌诀:年上吉星论甲子,逐年星逆中宫起;上中下作三元汇,一上四中七下兑。)
*
* @param sect 流派2为新年以立春零点起算1为新年以正月初一起算3为新年以立春节气交接的时刻起算
* @return 值年九星
* @return 九星
*/
public NineStar getYearNineStar(int sect) {
String yearInGanZhi;
@@ -2061,7 +2056,7 @@ public class Lunar {
protected NineStar getMonthNineStar(int yearZhiIndex, int monthZhiIndex) {
int index = yearZhiIndex % 3;
int n = 27 - (index * 3);
int n = 27 - index * 3;
if (monthZhiIndex < LunarUtil.BASE_MONTH_ZHI_INDEX) {
n -= 3;
}
@@ -2072,7 +2067,7 @@ public class Lunar {
/**
* 获取值月九星流派2新的一月以节交接当天零点起算。月紫白星歌诀子午卯酉八白起寅申巳亥二黑求辰戌丑未五黄中。
*
* @return 值月九星
* @return 九星
*/
public NineStar getMonthNineStar() {
return getMonthNineStar(2);
@@ -2082,7 +2077,7 @@ public class Lunar {
* 获取值月九星(月紫白星歌诀:子午卯酉八白起,寅申巳亥二黑求,辰戌丑未五黄中。)
*
* @param sect 流派2为新的一月以节交接当天零点起算3为新的一月以节交接准确时刻起算
* @return 值月九星
* @return 九星
*/
public NineStar getMonthNineStar(int sect) {
int yearZhiIndex;
@@ -2106,7 +2101,7 @@ public class Lunar {
/**
* 获取值日九星(日家紫白星歌诀:日家白法不难求,二十四气六宫周;冬至雨水及谷雨,阳顺一七四中游;夏至处暑霜降后,九三六星逆行求。)
*
* @return 值日九星
* @return 九星
*/
public NineStar getDayNineStar() {
String solarYmd = solar.toYmd();
@@ -2119,19 +2114,19 @@ public class Lunar {
Solar solarShunBai;
Solar solarShunBai2;
Solar solarNiZi;
if (dongZhiIndex>29) {
if (dongZhiIndex > 29) {
solarShunBai = dongZhi.next(60 - dongZhiIndex);
} else {
solarShunBai = dongZhi.next(-dongZhiIndex);
}
String solarShunBaiYmd = solarShunBai.toYmd();
if (dongZhiIndex2>29) {
if (dongZhiIndex2 > 29) {
solarShunBai2 = dongZhi2.next(60 - dongZhiIndex2);
} else {
solarShunBai2 = dongZhi2.next(-dongZhiIndex2);
}
String solarShunBaiYmd2 = solarShunBai2.toYmd();
if (xiaZhiIndex>29) {
if (xiaZhiIndex > 29) {
solarNiZi = xiaZhi.next(60 - xiaZhiIndex);
} else {
solarNiZi = xiaZhi.next(-xiaZhiIndex);
@@ -2140,7 +2135,7 @@ public class Lunar {
int offset = 0;
if (solarYmd.compareTo(solarShunBaiYmd) >= 0 && solarYmd.compareTo(solarNiZiYmd) < 0) {
offset = ExactDate.getDaysBetween(solarShunBai.getCalendar(), this.getSolar().getCalendar()) % 9;
} else if (solarYmd.compareTo(solarNiZiYmd) >= 0 && solarYmd.compareTo(solarShunBaiYmd2) < 0){
} else if (solarYmd.compareTo(solarNiZiYmd) >= 0 && solarYmd.compareTo(solarShunBaiYmd2) < 0) {
offset = 8 - (ExactDate.getDaysBetween(solarNiZi.getCalendar(), this.getSolar().getCalendar()) % 9);
} else if (solarYmd.compareTo(solarShunBaiYmd2) >= 0) {
offset = ExactDate.getDaysBetween(solarShunBai2.getCalendar(), this.getSolar().getCalendar()) % 9;
@@ -2153,7 +2148,7 @@ public class Lunar {
/**
* 获取值时九星(时家紫白星歌诀:三元时白最为佳,冬至阳生顺莫差,孟日七宫仲一白,季日四绿发萌芽,每把时辰起甲子,本时星耀照光华,时星移入中宫去,顺飞八方逐细查。夏至阴生逆回首,孟归三碧季加六,仲在九宫时起甲,依然掌中逆轮跨。)
*
* @return 值时九星
* @return 九星
*/
public NineStar getTimeNineStar() {
String solarYmd = solar.toYmd();
@@ -2199,10 +2194,10 @@ public class Lunar {
* @return 节气
*/
public JieQi getNextJie(boolean wholeDay) {
int l = JIE_QI_IN_USE.length/2;
int l = JIE_QI_IN_USE.length / 2;
String[] conditions = new String[l];
for(int i=0;i<l;i++){
conditions[i] = JIE_QI_IN_USE[i*2];
for (int i = 0; i < l; i++) {
conditions[i] = JIE_QI_IN_USE[i * 2];
}
return getNearJieQi(true, conditions, wholeDay);
}
@@ -2223,10 +2218,10 @@ public class Lunar {
* @return 节气
*/
public JieQi getPrevJie(boolean wholeDay) {
int l = JIE_QI_IN_USE.length/2;
int l = JIE_QI_IN_USE.length / 2;
String[] conditions = new String[l];
for(int i=0;i<l;i++){
conditions[i] = JIE_QI_IN_USE[i*2];
for (int i = 0; i < l; i++) {
conditions[i] = JIE_QI_IN_USE[i * 2];
}
return getNearJieQi(false, conditions, wholeDay);
}
@@ -2247,10 +2242,10 @@ public class Lunar {
* @return 节气
*/
public JieQi getNextQi(boolean wholeDay) {
int l = JIE_QI_IN_USE.length/2;
int l = JIE_QI_IN_USE.length / 2;
String[] conditions = new String[l];
for(int i=0;i<l;i++){
conditions[i] = JIE_QI_IN_USE[i*2+1];
for (int i = 0; i < l; i++) {
conditions[i] = JIE_QI_IN_USE[i * 2 + 1];
}
return getNearJieQi(true, conditions, wholeDay);
}
@@ -2271,10 +2266,10 @@ public class Lunar {
* @return 节气
*/
public JieQi getPrevQi(boolean wholeDay) {
int l = JIE_QI_IN_USE.length/2;
int l = JIE_QI_IN_USE.length / 2;
String[] conditions = new String[l];
for(int i=0;i<l;i++){
conditions[i] = JIE_QI_IN_USE[i*2+1];
for (int i = 0; i < l; i++) {
conditions[i] = JIE_QI_IN_USE[i * 2 + 1];
}
return getNearJieQi(false, conditions, wholeDay);
}
@@ -2322,7 +2317,7 @@ public class Lunar {
*
* @param forward 是否顺推true为顺推false为逆推
* @param conditions 过滤条件,如果设置过滤条件,仅返回匹配该名称的
* @param wholeDay 是否按天计
* @param wholeDay 是否按天计
* @return 节气
*/
protected JieQi getNearJieQi(boolean forward, String[] conditions, boolean wholeDay) {
@@ -2415,7 +2410,7 @@ public class Lunar {
* @return 节气对象
*/
public JieQi getCurrentJie() {
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()) {
@@ -2431,7 +2426,7 @@ public class Lunar {
* @return 节气对象
*/
public JieQi getCurrentQi() {
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()) {
@@ -2958,12 +2953,8 @@ public class Lunar {
break;
}
}
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 LunarUtil.WU_HOU[(offset * 3 + days / 5) % LunarUtil.WU_HOU.length];
}
@@ -2974,14 +2965,19 @@ public class Lunar {
*/
public String getHou() {
JieQi jieQi = getPrevJieQi(true);
String name = jieQi.getName();
Solar startSolar = jieQi.getSolar();
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]);
int max = LunarUtil.HOU.length - 1;
int offset = days / 5;
if (offset > max) {
offset = max;
}
return String.format("%s %s", jieQi.getName(), LunarUtil.HOU[offset]);
}
/**
* 获取日禄
*
* @return 日禄
*/
public String getDayLu() {
@@ -3011,8 +3007,8 @@ public class Lunar {
public List<LunarTime> getTimes() {
List<LunarTime> l = new ArrayList<LunarTime>();
l.add(new LunarTime(year, month, day, 0, 0, 0));
for(int i = 0; i < 12; i++){
l.add(new LunarTime(year, month, day, (i+1)*2-1, 0, 0));
for (int i = 0; i < 12; i++) {
l.add(new LunarTime(year, month, day, (i + 1) * 2 - 1, 0, 0));
}
return l;
}

View File

@@ -2,6 +2,8 @@ package com.nlf.calendar;
import com.nlf.calendar.util.LunarUtil;
import java.util.List;
/**
* 农历月
*
@@ -31,9 +33,10 @@ public class LunarMonth {
/**
* 初始化
* @param lunarYear 农历年
* @param lunarMonth 农历月1-12闰月为负数如闰2月为-2
* @param dayCount 天数
*
* @param lunarYear 农历年
* @param lunarMonth 农历月1-12闰月为负数如闰2月为-2
* @param dayCount 天数
* @param firstJulianDay 初一的儒略日
*/
public LunarMonth(int lunarYear, int lunarMonth, int dayCount, double firstJulianDay) {
@@ -45,16 +48,18 @@ public class LunarMonth {
/**
* 通过农历年月初始化
* @param lunarYear 农历年
*
* @param lunarYear 农历年
* @param lunarMonth 农历月1-12闰月为负数如闰2月为-2
* @return 农历月
*/
public static LunarMonth fromYm(int lunarYear, int lunarMonth){
public static LunarMonth fromYm(int lunarYear, int lunarMonth) {
return LunarYear.fromYear(lunarYear).getMonth(lunarMonth);
}
/**
* 获取农历年
*
* @return 农历年
*/
public int getYear() {
@@ -63,6 +68,7 @@ public class LunarMonth {
/**
* 获取农历月
*
* @return 农历月1-12闰月为负数如闰2月为-2
*/
public int getMonth() {
@@ -71,6 +77,7 @@ public class LunarMonth {
/**
* 是否闰月
*
* @return true/false
*/
public boolean isLeap() {
@@ -79,6 +86,7 @@ public class LunarMonth {
/**
* 获取天数
*
* @return 天数
*/
public int getDayCount() {
@@ -87,6 +95,7 @@ public class LunarMonth {
/**
* 获取初一的儒略日
*
* @return 初一的儒略日
*/
public double getFirstJulianDay() {
@@ -96,27 +105,21 @@ public class LunarMonth {
/**
* 获取太岁方位
*
* @return 太岁方位,如艮
* @return 方位,如艮
*/
public String getPositionTaiSui() {
String p;
int m = Math.abs(month);
switch(m) {
int m = Math.abs(month) % 4;
switch (m) {
case 0:
p = "";
break;
case 1:
case 5:
case 9:
p = "";
break;
case 3:
case 7:
case 11:
p = "";
break;
case 4:
case 8:
case 12:
p = "";
break;
default:
p = LunarUtil.POSITION_GAN[Solar.fromJulianDay(this.getFirstJulianDay()).getLunar().getMonthGanIndex()];
}
@@ -126,7 +129,7 @@ public class LunarMonth {
/**
* 获取太岁方位描述
*
* @return 太岁方位描述,如东北
* @return 方位描述,如东北
*/
public String getPositionTaiSuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionTaiSui());
@@ -141,7 +144,7 @@ public class LunarMonth {
int index = LunarYear.fromYear(year).getZhiIndex() % 3;
int m = Math.abs(month);
int monthZhiIndex = (13 + m) % 12;
int n = 27 - (index * 3);
int n = 27 - index * 3;
if (monthZhiIndex < LunarUtil.BASE_MONTH_ZHI_INDEX) {
n -= 3;
}
@@ -153,4 +156,72 @@ public class LunarMonth {
public String toString() {
return year + "" + (isLeap() ? "" : "") + LunarUtil.MONTH[Math.abs(month)] + "月(" + dayCount + "天)";
}
/**
* 获取往后推几个月的阴历月,如果要往前推,则月数用负数
*
* @param n 月数
* @return 阴历月
*/
public LunarMonth next(int n) {
if (0 == n) {
return LunarMonth.fromYm(year, month);
} else if (n > 0) {
int rest = n;
int ny = year;
int iy = ny;
int im = month;
int index = 0;
List<LunarMonth> months = LunarYear.fromYear(ny).getMonths();
while (true) {
int size = months.size();
for (int i = 0; i < size; i++) {
LunarMonth m = months.get(i);
if (m.getYear() == iy && m.getMonth() == im) {
index = i;
break;
}
}
int more = size - index - 1;
if (rest < more) {
break;
}
rest -= more;
LunarMonth lastMonth = months.get(size - 1);
iy = lastMonth.getYear();
im = lastMonth.getMonth();
ny++;
months = LunarYear.fromYear(ny).getMonths();
}
return months.get(index + rest);
} else {
int rest = -n;
int ny = year;
int iy = ny;
int im = month;
int index = 0;
List<LunarMonth> months = LunarYear.fromYear(ny).getMonths();
while (true) {
int size = months.size();
for (int i = 0; i < size; i++) {
LunarMonth m = months.get(i);
if (m.getYear() == iy && m.getMonth() == im) {
index = i;
break;
}
}
if (rest <= index) {
break;
}
rest -= index;
LunarMonth firstMonth = months.get(0);
iy = firstMonth.getYear();
im = firstMonth.getMonth();
ny--;
months = LunarYear.fromYear(ny).getMonths();
}
return months.get(index - rest);
}
}
}

View File

@@ -457,7 +457,7 @@ public class LunarYear {
int index = LunarUtil.getJiaZiIndex(getGanZhi()) + 1;
int yuan = ((this.year + 2696) / 60) % 3;
int offset = (62 + yuan * 3 - index) % 9;
if(0 == offset){
if (0 == offset) {
offset = 9;
}
return NineStar.fromIndex(offset - 1);
@@ -466,7 +466,7 @@ public class LunarYear {
/**
* 获取喜神方位
*
* @return 喜神方位,如艮
* @return 方位,如艮
*/
public String getPositionXi() {
return LunarUtil.POSITION_XI[ganIndex + 1];
@@ -475,7 +475,7 @@ public class LunarYear {
/**
* 获取喜神方位描述
*
* @return 喜神方位描述,如东北
* @return 方位描述,如东北
*/
public String getPositionXiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionXi());
@@ -484,7 +484,7 @@ public class LunarYear {
/**
* 获取阳贵神方位
*
* @return 阳贵神方位,如艮
* @return 方位,如艮
*/
public String getPositionYangGui() {
return LunarUtil.POSITION_YANG_GUI[ganIndex + 1];
@@ -493,7 +493,7 @@ public class LunarYear {
/**
* 获取阳贵神方位描述
*
* @return 阳贵神方位描述,如东北
* @return 方位描述,如东北
*/
public String getPositionYangGuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionYangGui());
@@ -502,7 +502,7 @@ public class LunarYear {
/**
* 获取阴贵神方位
*
* @return 阴贵神方位,如艮
* @return 方位,如艮
*/
public String getPositionYinGui() {
return LunarUtil.POSITION_YIN_GUI[ganIndex + 1];
@@ -511,7 +511,7 @@ public class LunarYear {
/**
* 获取阴贵神方位描述
*
* @return 阴贵神方位描述,如东北
* @return 方位描述,如东北
*/
public String getPositionYinGuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionYinGui());
@@ -520,7 +520,7 @@ public class LunarYear {
/**
* 获取福神方位默认流派2
*
* @return 福神方位,如艮
* @return 方位,如艮
*/
public String getPositionFu() {
return getPositionFu(2);
@@ -530,7 +530,7 @@ public class LunarYear {
* 获取福神方位
*
* @param sect 流派1或2
* @return 福神方位,如艮
* @return 方位,如艮
*/
public String getPositionFu(int sect) {
return (1 == sect ? LunarUtil.POSITION_FU : LunarUtil.POSITION_FU_2)[ganIndex + 1];
@@ -539,7 +539,7 @@ public class LunarYear {
/**
* 获取福神方位描述默认流派2
*
* @return 福神方位描述,如东北
* @return 方位描述,如东北
*/
public String getPositionFuDesc() {
return getPositionFuDesc(2);
@@ -549,7 +549,7 @@ public class LunarYear {
* 获取福神方位描述
*
* @param sect 流派1或2
* @return 福神方位描述,如东北
* @return 方位描述,如东北
*/
public String getPositionFuDesc(int sect) {
return LunarUtil.POSITION_DESC.get(getPositionFu(sect));
@@ -567,7 +567,7 @@ public class LunarYear {
/**
* 获取财神方位描述
*
* @return 财神方位描述,如东北
* @return 方位描述,如东北
*/
public String getPositionCaiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionCai());
@@ -576,7 +576,7 @@ public class LunarYear {
/**
* 获取太岁方位
*
* @return 太岁方位,如艮
* @return 方位,如艮
*/
public String getPositionTaiSui() {
return LunarUtil.POSITION_TAI_SUI_YEAR[zhiIndex];
@@ -585,7 +585,7 @@ public class LunarYear {
/**
* 获取太岁方位描述
*
* @return 太岁方位描述,如东北
* @return 方位描述,如东北
*/
public String getPositionTaiSuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionTaiSui());
@@ -600,4 +600,13 @@ public class LunarYear {
return year + "";
}
/**
* 获取往后推几年的阴历年,如果要往前推,则年数用负数
*
* @param n 年数
* @return 阴历年
*/
public LunarYear next(int n) {
return LunarYear.fromYear(year + n);
}
}

View File

@@ -17,7 +17,7 @@ public class TaoUtil {
private static final long serialVersionUID = 1;
{
put("1-1", Collections.nCopies(1, new TaoFestival("天腊之辰", "天腊,此日五帝会于方九炁青天")));
put("1-1", Collections.nCopies(1, new TaoFestival("天腊之辰", "天腊,此日五帝会于方九炁青天")));
put("1-3", Arrays.asList(new TaoFestival("郝真人圣诞"), new TaoFestival("孙真人圣诞")));
put("1-5", Collections.nCopies(1, new TaoFestival("孙祖清静元君诞")));
put("1-7", Collections.nCopies(1, new TaoFestival("举迁赏会", "此日上元赐福,天官同地水二官考校罪福")));
@@ -52,7 +52,7 @@ public class TaoUtil {
put("4-20", Collections.nCopies(1, new TaoFestival("眼光圣母娘娘诞")));
put("4-28", Collections.nCopies(1, new TaoFestival("神农先帝诞")));
put("5-1", Collections.nCopies(1, new TaoFestival("南极长生大帝圣诞")));
put("5-5", Arrays.asList(new TaoFestival("地腊之辰", "地腊,此日五帝会南方三炁丹天"), new TaoFestival("南方雷祖圣诞"), new TaoFestival("地祗温元帅圣诞"), new TaoFestival("雷霆邓天君圣诞")));
put("5-5", Arrays.asList(new TaoFestival("地腊之辰", "地腊,此日五帝会南方三炁丹天"), new TaoFestival("南方雷祖圣诞"), new TaoFestival("地祗温元帅圣诞"), new TaoFestival("雷霆邓天君圣诞")));
put("5-11", Collections.nCopies(1, new TaoFestival("城隍爷圣诞")));
put("5-13", Arrays.asList(new TaoFestival("关圣帝君降神"), new TaoFestival("关平太子圣诞")));
put("5-18", Collections.nCopies(1, new TaoFestival("张天师圣诞")));
@@ -94,7 +94,7 @@ public class TaoUtil {
put("9-22", Collections.nCopies(1, new TaoFestival("增福财神诞")));
put("9-23", Collections.nCopies(1, new TaoFestival("萨翁真君圣诞")));
put("9-28", Collections.nCopies(1, new TaoFestival("五显灵官马元帅圣诞")));
put("10-1", Arrays.asList(new TaoFestival("民岁腊之辰", "民岁腊,此日五帝会北方五炁黑天"), new TaoFestival("东皇大帝圣诞")));
put("10-1", Arrays.asList(new TaoFestival("民岁腊之辰", "民岁腊,此日五帝会北方五炁黑天"), new TaoFestival("东皇大帝圣诞")));
put("10-3", Collections.nCopies(1, new TaoFestival("三茅应化真君圣诞")));
put("10-6", Collections.nCopies(1, new TaoFestival("天曹诸司五岳五帝圣诞")));
put("10-15", Arrays.asList(new TaoFestival("下元水官大帝圣诞"), new TaoFestival("建生大会", "此日下元解厄,水官同天地二官考校罪福")));
@@ -105,7 +105,7 @@ public class TaoUtil {
put("11-9", Collections.nCopies(1, new TaoFestival("湘子韩祖圣诞")));
put("11-11", Collections.nCopies(1, new TaoFestival("太乙救苦天尊圣诞")));
put("11-26", Collections.nCopies(1, new TaoFestival("北方五道圣诞")));
put("12-8", Collections.nCopies(1, new TaoFestival("王侯腊之辰", "王侯腊,此日五帝会上方玄都玉京")));
put("12-8", Collections.nCopies(1, new TaoFestival("王侯腊之辰", "王侯腊,此日五帝会上方玄都玉京")));
put("12-16", Arrays.asList(new TaoFestival("南岳大帝圣诞"), new TaoFestival("福德正神诞")));
put("12-20", Collections.nCopies(1, new TaoFestival("鲁班先师圣诞")));
put("12-21", Collections.nCopies(1, new TaoFestival("天猷上帝圣诞")));

View File

@@ -1,5 +1,6 @@
package sample;
import com.nlf.calendar.LunarMonth;
import com.nlf.calendar.Solar;
import com.nlf.calendar.SolarMonth;
import org.junit.Test;
@@ -10,11 +11,11 @@ import org.junit.Test;
public class MonthTest {
@Test
public void test(){
public void test() {
//下个月
SolarMonth month = new SolarMonth().next(1);
//遍历每一天
for(Solar d:month.getDays()){
for (Solar d : month.getDays()) {
//输出阳历信息
System.out.println(d.toFullString());
//输出阴历信息
@@ -23,4 +24,22 @@ public class MonthTest {
}
}
@Test
public void test1() {
LunarMonth month = LunarMonth.fromYm(2020, 1);
// 顺推
for (int i = 0; i < 60; i++) {
System.out.println(month.next(i).toString());
}
}
@Test
public void test2() {
LunarMonth month = LunarMonth.fromYm(2020, 1);
// 倒推
for (int i = 0; i < 60; i++) {
System.out.println(month.next(-i).toString());
}
}
}

View File

@@ -5,6 +5,11 @@ import com.nlf.calendar.Solar;
import org.junit.Assert;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* 农历测试
*
@@ -394,4 +399,20 @@ public class LunarTest {
Assert.assertEquals("[嫁娶, 祭祀, 祈福, 求嗣, 开光, 出行, 解除, 出火, 拆卸, 进人口, 入宅, 移徙, 安床, 栽种, 动土, 修造, 纳畜, 入殓, 安葬, 立碑, 除服, 成服]", lunar.getDayYi().toString());
}
@Test
public void test54() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
Date date = format.parse("1986-08-14");
Lunar lunar = new Lunar(date);
Assert.assertEquals("一九八六年七月初九", lunar.toString());
}
@Test
public void test55() {
Solar solar = new Solar(1986, 8, 14);
Lunar lunar = solar.getLunar();
Assert.assertEquals("一九八六年七月初九", lunar.toString());
}
}