solarToLunar method

List<int> solarToLunar(
  1. int year,
  2. int month,
  3. int day
)

公历转农历 Solar To Lunar

@param year 公历年 @param month 公历月 @param day 公历日 @return 0农历年 1农历月 2农历日 3是否闰月 0 false : 1 true

Implementation

static List<int> solarToLunar(int year, int month, int day) {
  List<int> lunarInt = new List(4);
  int index = year - SOLAR[0];
  int data = (year << 9) | (month << 5) | (day);
  int solar11;
  if (SOLAR[index] > data) {
    index--;
  }
  solar11 = SOLAR[index];
  int y = getBitInt(solar11, 12, 9);
  int m = getBitInt(solar11, 4, 5);
  int d = getBitInt(solar11, 5, 0);
  int offset = solarToInt(year, month, day) - solarToInt(y, m, d);

  int days = LUNAR_MONTH_DAYS[index];
  int leap = getBitInt(days, 4, 13);

  int lunarY = index + SOLAR[0];
  int lunarM = 1;
  int lunarD;
  offset += 1;

  for (int i = 0; i < 13; i++) {
    int dm = getBitInt(days, 1, 12 - i) == 1 ? 30 : 29;
    if (offset > dm) {
      lunarM++;
      offset -= dm;
    } else {
      break;
    }
  }

  lunarD = offset;
  lunarInt[0] = lunarY;
  lunarInt[1] = lunarM;
  lunarInt[3] = 0;

  if (leap != 0 && lunarM > leap) {
    lunarInt[1] = lunarM - 1;
    if (lunarM == leap + 1) {
      lunarInt[3] = 1;
    }
  }
  lunarInt[2] = lunarD;
  return lunarInt;
}