/// <summary> /// 通过农历年月日时初始化 /// </summary> /// <param name="lunarYear">年(农历)</param> /// <param name="lunarMonth">月(农历),1到12,闰月为负,即闰2月=-2</param> /// <param name="lunarDay">日(农历),1到31</param> /// <param name="hour">小时(阳历)</param> /// <param name="minute">分钟(阳历)</param> /// <param name="second">秒钟(阳历)</param> public Lunar(int lunarYear, int lunarMonth, int lunarDay, int hour, int minute, int second) { this.year = lunarYear; this.month = lunarMonth; this.day = lunarDay; this.hour = hour; this.minute = minute; this.second = second; dayOffset = LunarUtil.computeAddDays(year, month, day); this.solar = toSolar(); compute(); }
/// <summary> /// 通过阳历日期初始化 /// </summary> /// <param name="date">阳历日期</param> public Lunar(DateTime date) { solar = new Solar(date); int y = solar.getYear(); int m = solar.getMonth(); int d = solar.getDay(); int startYear, startMonth, startDay; int lunarYear, lunarMonth, lunarDay; if (y < 2000) { startYear = SolarUtil.BASE_YEAR; startMonth = SolarUtil.BASE_MONTH; startDay = SolarUtil.BASE_DAY; lunarYear = LunarUtil.BASE_YEAR; lunarMonth = LunarUtil.BASE_MONTH; lunarDay = LunarUtil.BASE_DAY; } else { startYear = SolarUtil.BASE_YEAR + 99; startMonth = 1; startDay = 1; lunarYear = LunarUtil.BASE_YEAR + 99; lunarMonth = 11; lunarDay = 25; } int diff = 0; for (int i = startYear; i < y; i++) { diff += 365; if (SolarUtil.isLeapYear(i)) { diff += 1; } } for (int i = startMonth; i < m; i++) { diff += SolarUtil.getDaysOfMonth(y, i); } diff += d - startDay; lunarDay += diff; int lastDate = LunarUtil.getDaysOfMonth(lunarYear, lunarMonth); while (lunarDay > lastDate) { lunarDay -= lastDate; lunarMonth = LunarUtil.nextMonth(lunarYear, lunarMonth); if (lunarMonth == 1) { lunarYear++; } lastDate = LunarUtil.getDaysOfMonth(lunarYear, lunarMonth); } year = lunarYear; month = lunarMonth; day = lunarDay; hour = solar.getHour(); minute = solar.getMinute(); second = solar.getSecond(); dayOffset = LunarUtil.computeAddDays(year, month, day); compute(); }