1
0
mirror of synced 2026-10-17 20:16:11 +08:00

v1.3.12 八字转阳历结果按时间先后排序,转换速度大幅提升。

This commit is contained in:
6tail
2024-02-06 17:40:46 +08:00
parent e15a916e55
commit 1c3ff0cbc5
8 changed files with 82 additions and 97 deletions

View File

@@ -499,22 +499,8 @@ public class EightChar {
* @return 命宫
*/
public String getMingGong() {
int monthZhiIndex = 0;
int timeZhiIndex = 0;
String monthZhi = getMonthZhi();
String timeZhi = getTimeZhi();
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
if (monthZhi.equals(MONTH_ZHI[i])) {
monthZhiIndex = i;
break;
}
}
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
if (timeZhi.equals(MONTH_ZHI[i])) {
timeZhiIndex = i;
break;
}
}
int monthZhiIndex = LunarUtil.find(getMonthZhi(), MONTH_ZHI, 0);
int timeZhiIndex = LunarUtil.find(getTimeZhi(), MONTH_ZHI, 0);
int offset = monthZhiIndex + timeZhiIndex;
offset = (offset >= 14 ? 26 : 14) - offset;
int ganIndex = (lunar.getYearGanIndexExact() + 1) * 2 + offset;
@@ -539,22 +525,8 @@ public class EightChar {
* @return 身宫
*/
public String getShenGong() {
int monthZhiIndex = 0;
int timeZhiIndex = 0;
String monthZhi = getMonthZhi();
String timeZhi = getTimeZhi();
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
if (monthZhi.equals(MONTH_ZHI[i])) {
monthZhiIndex = i;
break;
}
}
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
if (timeZhi.equals(LunarUtil.ZHI[i])) {
timeZhiIndex = i;
break;
}
}
int monthZhiIndex = LunarUtil.find(getMonthZhi(), MONTH_ZHI, 0);
int timeZhiIndex = LunarUtil.find(getTimeZhi(), LunarUtil.ZHI, 0);
int offset = monthZhiIndex + timeZhiIndex;
while (offset > 12) {
offset -= 12;

View File

@@ -296,49 +296,67 @@ public class Solar {
public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String dayGanZhi, String timeGanZhi, int sect, int baseYear) {
sect = (1 == sect) ? 1 : 2;
List<Solar> l = new ArrayList<Solar>();
List<Integer> years = new ArrayList<Integer>();
Solar today = fromDate(new Date());
int offsetYear = (today.getYear() - 4) % 60 - LunarUtil.getJiaZiIndex(yearGanZhi);
if(offsetYear < 0){
offsetYear += 60;
// 月地支距寅月的偏移值
int m = LunarUtil.find(monthGanZhi.substring(1), LunarUtil.ZHI, -1) - 2;
if (m < 0) {
m += 12;
}
int startYear = today.getYear() - offsetYear - 1;
int minYear = baseYear - 2;
while (startYear >= minYear) {
years.add(startYear);
startYear -= 60;
// 月天干要一致
if (((LunarUtil.find(yearGanZhi.substring(0, 1), LunarUtil.GAN, -1) + 1) * 2 + m) % 10 != LunarUtil.find(monthGanZhi.substring(0,1), LunarUtil.GAN, -1)) {
return l;
}
List<Integer> hours = new ArrayList<Integer>(2);
String timeZhi = timeGanZhi.substring(1);
for(int i = 1, j = LunarUtil.ZHI.length; i < j; i++){
if(LunarUtil.ZHI[i].equals(timeZhi)){
hours.add((i - 1) * 2);
break;
}
// 1年的立春是辛酉,序号57
int y = LunarUtil.getJiaZiIndex(yearGanZhi) - 57;
if (y < 0) {
y += 60;
}
if ("子".equals(timeZhi)) {
hours.add(23);
}
for (int hour: hours) {
for (Integer y : years) {
int maxYear = y + 3;
int year = y;
int month = 11;
if (year < baseYear) {
year = baseYear;
month = 1;
}
Solar solar = fromYmdHms(year, month, 1, hour, 0, 0);
while (solar.getYear() <= maxYear) {
Lunar lunar = solar.getLunar();
y++;
// 节令偏移值
m *= 2;
// 时辰地支转时刻,子时按零点算
int h = LunarUtil.find(timeGanZhi.substring(1), LunarUtil.ZHI, -1) * 2;
int startYear = baseYear - 1;
// 结束年
Calendar c = Calendar.getInstance(TIME_ZONE);
c.setTime(new Date());
c.set(Calendar.MILLISECOND, 0);
int endYear = c.get(Calendar.YEAR);
while (y <= endYear) {
if (y >= startYear) {
// 立春为寅月的开始
List<Solar> jieQiList = new ArrayList<Solar>(Lunar.fromYmd(y, 1, 1).getJieQiTable().values());
// 节令推移,年干支和月干支就都匹配上了
Solar solarTime = jieQiList.get(4 + m);
if (solarTime.getYear() >= baseYear) {
int mi = 0;
int s = 0;
// 日干支和节令干支的偏移值
Lunar lunar = solarTime.getLunar();
String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
int d = LunarUtil.getJiaZiIndex(dayGanZhi) - LunarUtil.getJiaZiIndex(dgz);
if (d < 0) {
d += 60;
}
if (d > 0) {
// 从节令推移天数
solarTime = solarTime.next(d);
} else if (h == solarTime.getHour()) {
// 如果正好是节令当天,且小时和节令的小时数相等的极端情况,把分钟和秒钟带上
mi = solarTime.getMinute();
s = solarTime.getSecond();
}
// 验证一下
Solar solar = Solar.fromYmdHms(solarTime.getYear(), solarTime.getMonth(), solarTime.getDay(), h, mi, s);
lunar = solar.getLunar();
dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && dgz.equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) {
l.add(solar);
break;
}
solar = solar.next(1);
}
}
y += 60;
}
return l;
}

View File

@@ -809,9 +809,13 @@ public class LunarUtil{
* @return 甲子序号
*/
public static int getJiaZiIndex(String ganZhi){
for(int i=0,j=LunarUtil.JIA_ZI.length;i<j;i++){
if(LunarUtil.JIA_ZI[i].equals(ganZhi)){
return i;
return find(ganZhi, JIA_ZI, 0);
}
public static int find(String name, String[] names, int offset) {
for(int i=0,j=names.length;i<j;i++){
if(names[i].equals(name)){
return i + offset;
}
}
return -1;
@@ -1020,22 +1024,8 @@ public class LunarUtil{
* @return 旬下标,0-5
*/
protected static int getXunIndex(String ganZhi){
String gan = ganZhi.substring(0,1);
String zhi = ganZhi.substring(1);
int ganIndex = 0;
int zhiIndex = 0;
for(int i=0,j=GAN.length;i<j;i++){
if(GAN[i].equals(gan)){
ganIndex = i;
break;
}
}
for(int i=0,j=ZHI.length;i<j;i++){
if(ZHI[i].equals(zhi)){
zhiIndex = i;
break;
}
}
int ganIndex = find(ganZhi.substring(0, 1), GAN, 0);
int zhiIndex = find(ganZhi.substring(1), ZHI, 0);
int diff = ganIndex - zhiIndex;
if(diff<0){
diff += 12;