/// <summary> /// 起运计算 /// </summary> private void computeStart() { JieQi prev = lunar.getPrevJie(); JieQi next = lunar.getNextJie(); Solar current = lunar.getSolar(); Solar start = forward ? current : prev.getSolar(); Solar end = forward ? next.getSolar() : current; int hourDiff = LunarUtil.getTimeZhiIndex(end.toYmdHms().Substring(11, 5)) - LunarUtil.getTimeZhiIndex(start.toYmdHms().Substring(11, 5)); DateTime endCalendar = new DateTime(end.getYear(), end.getMonth(), end.getDay(), 0, 0, 0, 0); DateTime startCalendar = new DateTime(start.getYear(), start.getMonth(), start.getDay(), 0, 0, 0, 0); int dayDiff = endCalendar.Subtract(startCalendar).Days; if (hourDiff < 0) { hourDiff += 12; dayDiff--; } int monthDiff = hourDiff * 10 / 30; int month = dayDiff * 4 + monthDiff; int day = hourDiff * 10 - monthDiff * 30; int year = month / 12; month = month - year * 12; this.startYear = year; this.startMonth = month; this.startDay = day; }
/// <summary> /// 获取起运的阳历日期 /// </summary> /// <returns>阳历日期</returns> public Solar getStartSolar() { Solar birth = lunar.getSolar(); DateTime c = new DateTime(birth.getYear(), birth.getMonth(), birth.getDay(), 0, 0, 0); c = c.AddYears(startYear); c = c.AddMonths(startMonth); c = c.AddDays(startDay); return(Solar.fromDate(c)); }
/// <summary> /// 周推移 /// </summary> /// <param name="weeks">推移的周数,负数为倒推</param> /// <param name="separateMonth">是否按月单独计算</param> /// <returns>推移后的阳历周</returns> public SolarWeek next(int weeks, bool separateMonth) { if (0 == weeks) { return(new SolarWeek(year, month, day, start)); } if (separateMonth) { int n = weeks; DateTime c = new DateTime(year, month, day); SolarWeek week = new SolarWeek(c, start); int m = this.month; bool plus = n > 0; while (0 != n) { c = c.AddDays(plus ? 7 : -7); week = new SolarWeek(c, start); int weekMonth = week.getMonth(); if (m != weekMonth) { int index = week.getIndex(); if (plus) { if (1 == index) { Solar firstDay = week.getFirstDay(); week = new SolarWeek(firstDay.getYear(), firstDay.getMonth(), firstDay.getDay(), start); weekMonth = week.getMonth(); } else { c = new DateTime(week.getYear(), week.getMonth(), 1); week = new SolarWeek(c, start); } } else { int size = SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start); if (size == index) { Solar firstDay = week.getFirstDay(); Solar lastDay = firstDay.next(6); week = new SolarWeek(lastDay.getYear(), lastDay.getMonth(), lastDay.getDay(), start); weekMonth = week.getMonth(); } else { c = new DateTime(week.getYear(), week.getMonth(), SolarUtil.getDaysOfMonth(week.getYear(), week.getMonth())); week = new SolarWeek(c, start); } } m = weekMonth; } n -= plus ? 1 : -1; } return(week); } else { DateTime c = new DateTime(year, month, day); c = c.AddDays(weeks * 7); return(new SolarWeek(c, start)); } }