v1.3.12 八字转阳历结果按时间先后排序,转换速度大幅提升。
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user