v1.2.16 补充灶马头相关内容;修复九星错误;新增太岁方位;新增道历天赦日;修复八字转阳历的错误;优化代码。
This commit is contained in:
@@ -11,10 +11,6 @@ import java.util.*;
|
||||
* @author 6tail
|
||||
*/
|
||||
public class Lunar {
|
||||
/**
|
||||
* 1天对应的毫秒
|
||||
*/
|
||||
private static final long MS_PER_DAY = 86400000L;
|
||||
/**
|
||||
* 节气表,国标以冬至为首个气令
|
||||
*/
|
||||
@@ -198,18 +194,14 @@ public class Lunar {
|
||||
@SuppressWarnings("MagicConstant")
|
||||
public Lunar(Date date) {
|
||||
solar = new Solar(date);
|
||||
Calendar c = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay());
|
||||
long solarTime = c.getTimeInMillis();
|
||||
int y = solar.getYear();
|
||||
LunarYear ly = LunarYear.fromYear(y);
|
||||
int currentYear = solar.getYear();
|
||||
int currentMonth = solar.getMonth();
|
||||
int currentDay = solar.getDay();
|
||||
LunarYear ly = LunarYear.fromYear(currentYear);
|
||||
for (LunarMonth m : ly.getMonths()) {
|
||||
// 初一
|
||||
Calendar firstDay = Solar.fromJulianDay(m.getFirstJulianDay()).getCalendar();
|
||||
firstDay.set(Calendar.HOUR_OF_DAY, 0);
|
||||
firstDay.set(Calendar.MINUTE, 0);
|
||||
firstDay.set(Calendar.SECOND, 0);
|
||||
firstDay.set(Calendar.MILLISECOND, 0);
|
||||
int days = (int) ((solarTime - firstDay.getTimeInMillis()) / MS_PER_DAY);
|
||||
Solar firstDay = Solar.fromJulianDay(m.getFirstJulianDay());
|
||||
int days = ExactDate.getDaysBetween(firstDay.getYear(), firstDay.getMonth(), firstDay.getDay(), currentYear, currentMonth, currentDay);
|
||||
if (days < m.getDayCount()) {
|
||||
year = m.getYear();
|
||||
month = m.getMonth();
|
||||
@@ -1214,6 +1206,39 @@ public class Lunar {
|
||||
return LunarUtil.POSITION_DESC.get(getDayPositionCai());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日太岁方位
|
||||
*
|
||||
* @return 日太岁方位,如艮
|
||||
*/
|
||||
public String getDayPositionTaiSui() {
|
||||
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();
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日太岁方位描述
|
||||
*
|
||||
* @return 日太岁方位描述,如东北
|
||||
*/
|
||||
public String getDayPositionTaiSuiDesc() {
|
||||
return LunarUtil.POSITION_DESC.get(getDayPositionTaiSui());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时辰喜神方位
|
||||
*
|
||||
@@ -1836,11 +1861,7 @@ public class Lunar {
|
||||
* @return 值年九星
|
||||
*/
|
||||
public NineStar getYearNineStar() {
|
||||
int index = -(year - 1900) % 9;
|
||||
if (index < 0) {
|
||||
index += 9;
|
||||
}
|
||||
return new NineStar(index);
|
||||
return LunarYear.fromYear(year).getNineStar();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1849,23 +1870,22 @@ public class Lunar {
|
||||
* @return 值月九星
|
||||
*/
|
||||
public NineStar getMonthNineStar() {
|
||||
int start = 2;
|
||||
int n = 12;
|
||||
String yearZhi = getYearZhi();
|
||||
if ("子午卯酉".contains(yearZhi)) {
|
||||
start = 8;
|
||||
n = 18;
|
||||
} else if ("辰戌丑未".contains(yearZhi)) {
|
||||
start = 5;
|
||||
n = 15;
|
||||
}
|
||||
// 寅月起,所以需要-2
|
||||
int monthIndex = monthZhiIndex - 2;
|
||||
if (monthIndex < 0) {
|
||||
monthIndex += 12;
|
||||
int m = month;
|
||||
if(m < 0){
|
||||
m = -m;
|
||||
}
|
||||
int index = start - monthIndex - 1;
|
||||
while (index < 0) {
|
||||
index += 9;
|
||||
int offset = (n - m) % 9;
|
||||
if(0 == offset){
|
||||
offset = 9;
|
||||
}
|
||||
return new NineStar(index);
|
||||
return NineStar.fromIndex(offset - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1874,40 +1894,45 @@ public class Lunar {
|
||||
* @return 值日九星
|
||||
*/
|
||||
public NineStar getDayNineStar() {
|
||||
//顺逆
|
||||
String solarYmd = solar.toYmd();
|
||||
String dongZhi = jieQi.get("冬至").toYmd();
|
||||
String yuShui = jieQi.get("雨水").toYmd();
|
||||
String guYu = jieQi.get("谷雨").toYmd();
|
||||
String xiaZhi = jieQi.get("夏至").toYmd();
|
||||
String chuShu = jieQi.get("处暑").toYmd();
|
||||
String shuangJiang = jieQi.get("霜降").toYmd();
|
||||
|
||||
int start = 6;
|
||||
boolean asc = false;
|
||||
if (solarYmd.compareTo(dongZhi) >= 0 && solarYmd.compareTo(yuShui) < 0) {
|
||||
asc = true;
|
||||
start = 1;
|
||||
} else if (solarYmd.compareTo(yuShui) >= 0 && solarYmd.compareTo(guYu) < 0) {
|
||||
asc = true;
|
||||
start = 7;
|
||||
} else if (solarYmd.compareTo(guYu) >= 0 && solarYmd.compareTo(xiaZhi) < 0) {
|
||||
asc = true;
|
||||
start = 4;
|
||||
} else if (solarYmd.compareTo(xiaZhi) >= 0 && solarYmd.compareTo(chuShu) < 0) {
|
||||
start = 9;
|
||||
} else if (solarYmd.compareTo(chuShu) >= 0 && solarYmd.compareTo(shuangJiang) < 0) {
|
||||
start = 3;
|
||||
Solar dongZhi = jieQi.get("冬至");
|
||||
Solar dongZhi2 = jieQi.get("DONG_ZHI");
|
||||
Solar xiaZhi = jieQi.get("夏至");
|
||||
int dongZhiIndex = LunarUtil.getJiaZiIndex(dongZhi.getLunar().getDayInGanZhi());
|
||||
int dongZhiIndex2 = LunarUtil.getJiaZiIndex(dongZhi2.getLunar().getDayInGanZhi());
|
||||
int xiaZhiIndex = LunarUtil.getJiaZiIndex(xiaZhi.getLunar().getDayInGanZhi());
|
||||
Solar solarShunBai;
|
||||
Solar solarShunBai2;
|
||||
Solar solarNiZi;
|
||||
if (dongZhiIndex>29) {
|
||||
solarShunBai = dongZhi.next(60 - dongZhiIndex);
|
||||
} else {
|
||||
solarShunBai = dongZhi.next(-dongZhiIndex);
|
||||
}
|
||||
int ganZhiIndex = LunarUtil.getJiaZiIndex(getDayInGanZhi()) % 9;
|
||||
int index = asc ? start + ganZhiIndex - 1 : start - ganZhiIndex - 1;
|
||||
if (index > 8) {
|
||||
index -= 9;
|
||||
String solarShunBaiYmd = solarShunBai.toYmd();
|
||||
if (dongZhiIndex2>29) {
|
||||
solarShunBai2 = dongZhi2.next(60 - dongZhiIndex2);
|
||||
} else {
|
||||
solarShunBai2 = dongZhi2.next(-dongZhiIndex2);
|
||||
}
|
||||
if (index < 0) {
|
||||
index += 9;
|
||||
String solarShunBaiYmd2 = solarShunBai2.toYmd();
|
||||
if (xiaZhiIndex>29) {
|
||||
solarNiZi = xiaZhi.next(60 - xiaZhiIndex);
|
||||
} else {
|
||||
solarNiZi = xiaZhi.next(-xiaZhiIndex);
|
||||
}
|
||||
return new NineStar(index);
|
||||
String solarNiZiYmd = solarNiZi.toYmd();
|
||||
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){
|
||||
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;
|
||||
} else if (solarYmd.compareTo(solarShunBaiYmd) < 0) {
|
||||
offset = (8 + ExactDate.getDaysBetween(this.getSolar().getCalendar(), solarShunBai.getCalendar())) % 9;
|
||||
}
|
||||
return NineStar.fromIndex(offset);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1916,27 +1941,22 @@ public class Lunar {
|
||||
* @return 值时九星
|
||||
*/
|
||||
public NineStar getTimeNineStar() {
|
||||
//顺逆
|
||||
String solarYmd = solar.toYmd();
|
||||
boolean asc = false;
|
||||
if (solarYmd.compareTo(jieQi.get("冬至").toYmd()) >= 0 && solarYmd.compareTo(jieQi.get("夏至").toYmd()) < 0) {
|
||||
if ((solarYmd.compareTo(jieQi.get("冬至").toYmd()) >= 0 && solarYmd.compareTo(jieQi.get("夏至").toYmd()) < 0)) {
|
||||
asc = true;
|
||||
} else if (solarYmd.compareTo(jieQi.get("DONG_ZHI").toYmd()) >= 0) {
|
||||
asc = true;
|
||||
}
|
||||
int start = asc ? 7 : 3;
|
||||
int start = asc ? 6 : 2;
|
||||
String dayZhi = getDayZhi();
|
||||
if ("子午卯酉".contains(dayZhi)) {
|
||||
start = asc ? 1 : 9;
|
||||
start = asc ? 0 : 8;
|
||||
} else if ("辰戌丑未".contains(dayZhi)) {
|
||||
start = asc ? 4 : 6;
|
||||
start = asc ? 3 : 5;
|
||||
}
|
||||
int index = asc ? start + timeZhiIndex - 1 : start - timeZhiIndex - 1;
|
||||
if (index > 8) {
|
||||
index -= 9;
|
||||
}
|
||||
if (index < 0) {
|
||||
index += 9;
|
||||
}
|
||||
return new NineStar(index);
|
||||
int index = asc ? start + timeZhiIndex : start + 9 - timeZhiIndex;
|
||||
return new NineStar(index % 9);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2615,7 +2635,7 @@ public class Lunar {
|
||||
return null;
|
||||
}
|
||||
|
||||
int days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
|
||||
int days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
|
||||
return new ShuJiu(LunarUtil.NUMBER[days / 9 + 1] + "九", days % 9 + 1);
|
||||
}
|
||||
|
||||
@@ -2644,7 +2664,7 @@ public class Lunar {
|
||||
return null;
|
||||
}
|
||||
|
||||
int days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
|
||||
int days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
|
||||
if (days < 10) {
|
||||
return new Fu("初伏", days + 1);
|
||||
}
|
||||
@@ -2652,7 +2672,7 @@ public class Lunar {
|
||||
// 第4个庚日,中伏第1天
|
||||
startCalendar.add(Calendar.DATE, 10);
|
||||
|
||||
days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
|
||||
days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
|
||||
if (days < 10) {
|
||||
return new Fu("中伏", days + 1);
|
||||
}
|
||||
@@ -2662,7 +2682,7 @@ public class Lunar {
|
||||
|
||||
Calendar liQiuCalendar = ExactDate.fromYmd(liQiu.getYear(), liQiu.getMonth(), liQiu.getDay());
|
||||
|
||||
days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
|
||||
days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
|
||||
// 末伏
|
||||
if (liQiuCalendar.compareTo(startCalendar) <= 0) {
|
||||
if (days < 10) {
|
||||
@@ -2675,7 +2695,7 @@ public class Lunar {
|
||||
}
|
||||
// 末伏第1天
|
||||
startCalendar.add(Calendar.DATE, 10);
|
||||
days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
|
||||
days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
|
||||
if (days < 10) {
|
||||
return new Fu("末伏", days + 1);
|
||||
}
|
||||
@@ -2713,7 +2733,7 @@ public class Lunar {
|
||||
Solar startSolar = jieQi.getSolar();
|
||||
Calendar startCalendar = ExactDate.fromYmd(startSolar.getYear(), startSolar.getMonth(), startSolar.getDay());
|
||||
|
||||
int days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
|
||||
int days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
|
||||
return LunarUtil.WU_HOU[(offset * 3 + days / 5) % LunarUtil.WU_HOU.length];
|
||||
}
|
||||
|
||||
@@ -2728,7 +2748,7 @@ public class Lunar {
|
||||
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 = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
|
||||
int days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
|
||||
return String.format("%s %s", name, LunarUtil.HOU[(days / 5) % LunarUtil.HOU.length]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user