• lunar-1.5.jar


    公历农历转换包

    https://mvnrepository.com/artifact/com.github.heqiao2010/lunar



        com.github.heqiao2010
        lunar
        1.5


    https://repo1.maven.org/maven2/com/github/heqiao2010/lunar/1.5/lunar-1.5.jar

    1. /**
    2. * TestLanar.java
    3. *
    4. *
    5. *
    6. * @author ZengWenFeng
    7. * @email 117791303@QQ.com
    8. * @mobile 13805029595
    9. * @date 2023.10.25
    10. */
    11. package zwf;
    12. import java.util.Calendar;
    13. import com.github.heqiao2010.lunar.LunarCalendar;
    14. /**
    15. * 测试公历农历转换包
    16. *
    17. *
    18. <!-- https://mvnrepository.com/artifact/com.github.heqiao2010/lunar -->
    19. <dependency>
    20. <groupId>com.github.heqiao2010</groupId>
    21. <artifactId>lunar</artifactId>
    22. <version>1.5</version>
    23. </dependency>
    24. https://repo1.maven.org/maven2/com/github/heqiao2010/lunar/1.5/lunar-1.5.jar
    25. *
    26. *
    27. * @author ZengWenFeng
    28. * @email 117791303@QQ.com
    29. * @mobile 13805029595
    30. * @date 2023.10.25
    31. */
    32. public class SolarLunarTest
    33. {
    34. /**
    35. * 构造函数
    36. *
    37. * @author ZengWenFeng
    38. * @email 117791303@QQ.com
    39. * @mobile 13805029595
    40. * @date 2023.10.25
    41. */
    42. public SolarLunarTest()
    43. {
    44. }
    45. /**
    46. * 测试方法
    47. *
    48. * @author ZengWenFeng
    49. * @email 117791303@QQ.com
    50. * @mobile 13805029595
    51. * @date 2023.10.25
    52. */
    53. public static void main(String[] args)
    54. {
    55. Calendar calendar = Calendar.getInstance();
    56. int year = calendar.get(Calendar.YEAR);
    57. int month = calendar.get(Calendar.MONTH);
    58. int date = calendar.get(Calendar.DATE);
    59. int hour = calendar.get(Calendar.HOUR_OF_DAY);
    60. int minute = calendar.get(Calendar.MINUTE);
    61. int second = calendar.get(Calendar.SECOND);
    62. //
    63. System.out.println(year + "年" + month + "月" + date + "日" + hour + "时" + minute + "分" + second + "秒");
    64. // 公历转农历
    65. LunarCalendar lunar = LunarCalendar.solar2Lunar(calendar);
    66. System.out.println(lunar.getFullLunarName());
    67. System.out.println("----------------");
    68. // 农历转公历
    69. Calendar solar = LunarCalendar.lunar2Solar(2023, 9, 10, false);
    70. int year2 = solar.get(Calendar.YEAR);
    71. int month2 = solar.get(Calendar.MONTH);
    72. int date2 = solar.get(Calendar.DATE);
    73. int hour2 = solar.get(Calendar.HOUR_OF_DAY);
    74. int minute2 = solar.get(Calendar.MINUTE);
    75. int second2 = solar.get(Calendar.SECOND);
    76. //
    77. System.out.println(year2 + "年" + month2 + "月" + date2 + "日" + hour2 + "时" + minute2 + "分" + second2 + "秒");
    78. }
    79. }

    源码不多:

    1. package com.github.heqiao2010.lunar;
    2. import java.util.*;
    3. /**
    4. * 中国农历
    5. * Created by joel on 2019/3/31.
    6. *
    7. * @author joel
    8. */
    9. public class LunarCalendar extends GregorianCalendar {
    10. /**
    11. * serialVersionUID
    12. */
    13. private static final long serialVersionUID = 7241031233810655166L;
    14. // ------------------------ 农历相关成员变量 --------------------------------
    15. // 农历年,和公历可能不一样
    16. private int lunarYear;
    17. // 农历月(范围1-12和公历不一样)
    18. private int lunarMonth;
    19. // 农历日期
    20. private int dayOfLunarMonth;
    21. // 是否为闰月日期
    22. private boolean isLeapMonth = false;
    23. // 农历这年闰月,如果不闰月,默认为0
    24. private int leapMonth = 0;
    25. // ------------------------ 构造方法 --------------------------------
    26. public LunarCalendar() {
    27. super();
    28. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    29. }
    30. public LunarCalendar(TimeZone zone) {
    31. super(zone);
    32. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    33. }
    34. public LunarCalendar(Locale aLocale) {
    35. super(aLocale);
    36. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    37. }
    38. public LunarCalendar(TimeZone zone, Locale aLocale) {
    39. super(zone, aLocale);
    40. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    41. }
    42. /**
    43. * 通过公历年、月、日构造
    44. * @param year 公历年
    45. * @param month 公历月
    46. * @param dayOfMonth 公历日
    47. */
    48. public LunarCalendar(int year, int month, int dayOfMonth) {
    49. super(year, month, dayOfMonth);
    50. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    51. }
    52. public LunarCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute) {
    53. super(year, month, dayOfMonth, hourOfDay, minute);
    54. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    55. }
    56. public LunarCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second) {
    57. super(year, month, dayOfMonth, hourOfDay, minute, second);
    58. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    59. }
    60. /**
    61. * 通过农历年、月、日构造
    62. *
    63. * @param lunarYear 农历年
    64. * @param lunarMonth 农历月份,范围1-12
    65. * @param dayOfLunarMonth 农历日
    66. * @param isLeapMonth 是否闰月
    67. */
    68. public LunarCalendar(int lunarYear, int lunarMonth, int dayOfLunarMonth, boolean isLeapMonth) {
    69. computeByLunarDate(lunarYear, lunarMonth, dayOfLunarMonth, isLeapMonth);
    70. }
    71. /**
    72. * 通过公历构造
    73. *
    74. * @param calendar  公历日期
    75. */
    76. public LunarCalendar(Calendar calendar) {
    77. super(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
    78. calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
    79. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    80. }
    81. // ------------------------ 静态方法 --------------------------------
    82. /**
    83. * 公历转农历
    84. *
    85. * @param solar  公历日期
    86. * @return 农历日期
    87. */
    88. public static LunarCalendar solar2Lunar(Calendar solar) {
    89. return new LunarCalendar(solar);
    90. }
    91. /**
    92. * 农历转公历
    93. *
    94. * @param lunarYear  农历年
    95. * @param lunarMonth  农历月,从1开始
    96. * @param LunarDate  农历日
    97. * @param isLeapMonth  是否润月
    98. * @return 公历日期
    99. */
    100. public static Calendar lunar2Solar(int lunarYear, int lunarMonth, int LunarDate, boolean isLeapMonth) {
    101. LunarCalendar ret = new LunarCalendar();
    102. ret.computeByLunarDate(lunarYear, lunarMonth, LunarDate, isLeapMonth);
    103. return ret;
    104. }
    105. // ------------------------ 成员方法 --------------------------------
    106. /**
    107. * 公历上的操作,加减均是公历上的“一年”,“一个月”
    108. * @param field {@link java.util.Calendar} YEAR/MONTH/DATE
    109. * @param amount 数量,可正可负
    110. */
    111. @Override
    112. public void add(int field, int amount) {
    113. super.add(field, amount);
    114. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    115. }
    116. /**
    117. * 农历上的年月日,加减均是农历上的“一年”,“一个月”
    118. * @param field {@link java.util.Calendar} YEAR/MONTH/DATE
    119. * @param amount 数量,可正可负
    120. */
    121. public void addByLunar(int field, int amount) {
    122. switch (field) {
    123. case Calendar.DATE:
    124. super.add(Calendar.DATE, amount);
    125. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    126. break;
    127. case Calendar.MONTH:
    128. addLunarMonths(amount);
    129. break;
    130. case Calendar.YEAR:
    131. // 增加一个农历年,保持月/日不变,清空闰月状态
    132. checkComputeLunarDate(lunarYear + amount, lunarMonth, dayOfLunarMonth, false);
    133. break;
    134. default:
    135. throw new IllegalArgumentException(String.format("unsupported field: %d", field));
    136. }
    137. }
    138. /**
    139. * 计算月份加减
    140. *
    141. * @param amount 数量
    142. */
    143. public void addLunarMonths(int amount) {
    144. int y = lunarYear;
    145. int m = -1;
    146. int index = LunarCodes.monthIndex(y, lunarMonth, isLeapMonth);
    147. boolean isLeapMonth = false;
    148. int sum = index + amount;
    149. if (amount > 0) {
    150. for (int _y = lunarYear; _y < LunarData.MAX_YEAR; _y++) {
    151. final short[] a = LunarCodes.monthCodes(_y);
    152. int lunarMonths = a.length - 1;
    153. sum -= lunarMonths;
    154. if (sum > 0) {
    155. y++;
    156. }
    157. if (sum <= 0) {
    158. if (sum == 0) m = lunarMonths;
    159. else m = lunarMonths + sum;
    160. isLeapMonth = a[0] > 0 && a[0] + 1 == m;
    161. if (isLeapMonth || a[0] > 0 && a[0] < m) {
    162. m--;
    163. }
    164. break;
    165. }
    166. }
    167. if (sum > 0) {
    168. throw new IllegalArgumentException(String.format("add of month out of range: %d", amount));
    169. }
    170. } else if (amount < 0) {
    171. if (sum > 0) {
    172. m = sum;
    173. } else if (sum == 0) {
    174. Map.Entry<Integer, Boolean> en = LunarCodes.month(--y, -1);
    175. m = en.getKey();
    176. isLeapMonth = en.getValue();
    177. } else {
    178. for (int i = lunarYear - 1; i > LunarData.MINI_YEAR; i--) {
    179. int lunarMonths = LunarCodes.monthCodes(i).length - 1;
    180. sum += lunarMonths;
    181. y--;
    182. if (sum >= 0) {
    183. Map.Entry<Integer, Boolean> en;
    184. if (sum == 0) {
    185. en = LunarCodes.month(--y, -1);
    186. } else {
    187. en = LunarCodes.month(y, sum + 1);
    188. }
    189. m = en.getKey();
    190. isLeapMonth = en.getValue();
    191. break;
    192. }
    193. }
    194. }
    195. if (sum < 0) {
    196. throw new IllegalArgumentException(String.format("add of month out of range: %d", amount));
    197. }
    198. }
    199. checkComputeLunarDate(y, m, dayOfLunarMonth, isLeapMonth);
    200. }
    201. /**
    202. * 校验 day of month 是否是合法的,如果越限则从30号减到29号
    203. *
    204. * @param y lunar year
    205. * @param m lunar month
    206. * @param d lunar day of month
    207. * @param isLeap 闰月
    208. */
    209. private void checkComputeLunarDate(int y, int m, int d, boolean isLeap) {
    210. int days = d;
    211. if (d > 29 && d > LunarUtils.lengthOfMonth(y, m, isLeap)) {
    212. days--;
    213. }
    214. computeByLunarDate(y, m, days, isLeap);
    215. }
    216. @Override
    217. public void set(int field, int value) {
    218. super.set(field, value);
    219. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    220. }
    221. @Override
    222. public void roll(int field, int amount) {
    223. super.roll(field, amount);
    224. computeBySolarDate(get(Calendar.YEAR), get(Calendar.MONTH), get(Calendar.DATE));
    225. }
    226. @Override
    227. public String toString() {
    228. if (this.lunarYear < LunarData.MINI_YEAR || this.lunarYear > LunarData.MAX_YEAR || this.lunarMonth < 1
    229. || this.lunarMonth > 12 || this.dayOfLunarMonth < 1
    230. || this.dayOfLunarMonth > 30) {
    231. return String.format("Wrong lunar date: %d %d %d", lunarYear, lunarMonth, dayOfLunarMonth);
    232. }
    233. return String.format("%s年%s%s月%s", LunarData.getYearName(this.lunarYear), this.isLeapMonth() ? "闰" : "",
    234. LunarData.getMonthName(this.lunarMonth), LunarData.getDayName(this.dayOfLunarMonth));
    235. }
    236. @Override
    237. public boolean equals(Object o) {
    238. if (this == o) return true;
    239. if (!(o instanceof LunarCalendar)) return false;
    240. if (!super.equals(o)) return false;
    241. LunarCalendar that = (LunarCalendar) o;
    242. return lunarYear == that.lunarYear &&
    243. lunarMonth == that.lunarMonth &&
    244. dayOfLunarMonth == that.dayOfLunarMonth &&
    245. isLeapMonth == that.isLeapMonth &&
    246. leapMonth == that.leapMonth;
    247. }
    248. @Override
    249. public int hashCode() {
    250. int result = super.hashCode();
    251. result = 31 * result + lunarYear;
    252. result = 31 * result + lunarMonth;
    253. result = 31 * result + dayOfLunarMonth;
    254. result = 31 * result + leapMonth;
    255. result = 31 * result + (isLeapMonth ? 1 : 0);
    256. return result;
    257. }
    258. @Override
    259. public Object clone() {
    260. LunarCalendar other = (LunarCalendar) super.clone();
    261. other.lunarYear = getLunarYear();
    262. other.lunarMonth = getLunarMonth();
    263. other.dayOfLunarMonth = getDayOfLunarMonth();
    264. other.leapMonth = getLeapMonth();
    265. other.isLeapMonth = isLeapMonth();
    266. return other;
    267. }
    268. /**
    269. * 返回农历日期,不包含年份
    270. *
    271. * @param showLeap  是否显示闰月的闰字
    272. * @return 农历日期
    273. */
    274. public String getLunar(boolean showLeap) {
    275. if (this.lunarMonth < 1 || this.lunarMonth > 12 || this.dayOfLunarMonth < 1
    276. || this.dayOfLunarMonth > 30) {
    277. throw new IllegalArgumentException(String.format("Wrong lunar dayOfLunarMonth: %d %d", lunarMonth, dayOfLunarMonth));
    278. }
    279. if (showLeap) {
    280. return String.format("%s%s月%s", this.isLeapMonth() ? "闰" : "", LunarData.getMonthName(this.lunarMonth),
    281. LunarData.getDayName(this.dayOfLunarMonth));
    282. } else {
    283. return String.format("%s月%s", LunarData.getMonthName(this.lunarMonth),
    284. LunarData.getDayName(this.dayOfLunarMonth));
    285. }
    286. }
    287. /**
    288. * 返回中国农历的全名
    289. *
    290. * @return String
    291. */
    292. public String getFullLunarName() {
    293. return String.format("%s %s %s", this, LunarData.getTraditionalYearName(this.lunarYear),
    294. LunarData.getAnimalYearName(this.lunarYear));
    295. }
    296. /**
    297. * 创建LunarInfo中某一年的一列公历日历编码
    298. * 公历日历编码:年份+月份+天,用于查询某个公历日期在某个LunarInfo列的哪一个区间
    299. *
    300. * @param solarYear 年份
    301. * @return 公历日历编码
    302. */
    303. private int[] builderSolarCodes(int solarYear) {
    304. if (solarYear < LunarData.MINI_YEAR || solarYear > LunarData.MAX_YEAR) {
    305. throw new IllegalArgumentException("Illegal solar year: " + solarYear);
    306. }
    307. int lunarIndex = solarYear - LunarData.MINI_YEAR;
    308. int[] solarCodes = new int[LunarData.LUNAR_INFO[lunarIndex].length];
    309. for (int i = 0; i < solarCodes.length; i++) {
    310. if (0 == i) { // 第一个数表示闰月,不用更改
    311. solarCodes[i] = LunarData.LUNAR_INFO[lunarIndex][i];
    312. } else if (1 == i) {
    313. if (LunarData.LUNAR_INFO[lunarIndex][1] > 999) {
    314. // 这年农历一月一日对应的公历实际是上一年的
    315. solarCodes[i] = (solarYear - 1) * 10000 + LunarData.LUNAR_INFO[lunarIndex][i];
    316. } else {
    317. solarCodes[i] = solarYear * 10000 + LunarData.LUNAR_INFO[lunarIndex][i];
    318. }
    319. } else {
    320. solarCodes[i] = solarYear * 10000 + LunarData.LUNAR_INFO[lunarIndex][i];
    321. }
    322. }
    323. return solarCodes;
    324. }
    325. /**
    326. * 通过给定的农历日期,计算公历日期
    327. *
    328. * @param lunarYear  农历年
    329. * @param lunarMonth  农历月,从1开始
    330. * @param lunarDate  农历日期
    331. * @param isLeapMonth  是否为闰月
    332. */
    333. private void computeByLunarDate(final int lunarYear, final int lunarMonth, final int lunarDate,
    334. final boolean isLeapMonth) {
    335. if (lunarYear < LunarData.MINI_YEAR || lunarYear > LunarData.MAX_YEAR) {
    336. throw new IllegalArgumentException(String.format("LunarYear must in (%d, %d)", LunarData.MINI_YEAR,
    337. LunarData.MAX_YEAR));
    338. }
    339. this.lunarYear = lunarYear;
    340. this.lunarMonth = lunarMonth;
    341. this.dayOfLunarMonth = lunarDate;
    342. this.isLeapMonth = isLeapMonth;
    343. short code = LunarCodes.lunarMonthCode(lunarYear, lunarMonth, isLeapMonth);
    344. // 对设置的day of month 进行检查
    345. if (lunarDate == 30) {
    346. long length = LunarCodes.lengthOfMonth(lunarYear, lunarMonth, code);
    347. if (length != 30) {
    348. throw new IllegalArgumentException(String.format("农历%d年%d月, 闰月=%s,月天数为%d < %d", lunarYear,
    349. lunarMonth, isLeapMonth, length, lunarDate));
    350. }
    351. }
    352. super.set(Calendar.YEAR, lunarYear + LunarCodes.codeYear(code));
    353. super.set(Calendar.MONTH, LunarCodes.codeMonth(code) - 1);
    354. super.set(Calendar.DATE, LunarCodes.codeDay(code));
    355. super.add(Calendar.DATE, lunarDate - 1);
    356. }
    357. /**
    358. * 通过给定公历日期,计算农历日期各个域值
    359. *
    360. * 这个方法可能会被调用多次,后续看能否再做优化
    361. *
    362. *
    363. * @param solarYear 公历年
    364. * @param solarMonth 公历月,0-11
    365. * @param solarDate 公历日
    366. */
    367. private void computeBySolarDate(final int solarYear, final int solarMonth, final int solarDate) {
    368. if (solarYear < LunarData.MINI_YEAR
    369. || (solarYear == LunarData.MINI_YEAR && solarMonth < LunarData.MINI_MONTH)
    370. || (solarYear == LunarData.MINI_YEAR && solarMonth == LunarData.MINI_MONTH && solarDate < LunarData.MINI_DATE)
    371. || solarYear > LunarData.MAX_YEAR
    372. || (solarYear == LunarData.MAX_YEAR && solarMonth > LunarData.MAX_MONTH)
    373. || (solarYear == LunarData.MAX_YEAR && solarMonth == LunarData.MAX_MONTH && solarDate > LunarData.MAX_DATE)
    374. ) {
    375. // 有些中间过程日期会超出可计算范围
    376. // throw new IllegalArgumentException("Illegal solar year: " + solarYear);
    377. return;
    378. }
    379. int solarCode = solarYear * 10000 + 100 * (1 + solarMonth) + solarDate; // 公历码
    380. leapMonth = LunarData.LUNAR_INFO[solarYear - LunarData.MINI_YEAR][0];
    381. int[] solarCodes = builderSolarCodes(solarYear);
    382. int newMonth = LunarUtils.binSearch(solarCodes, solarCode);
    383. if (-1 == newMonth) {
    384. throw new IllegalArgumentException("No lunarInfo found by solarCode: " + solarCode);
    385. }
    386. int xDate = Long.valueOf(LunarCodes.solarDateCodesDiff(solarCode, solarCodes[newMonth], Calendar.DATE)).intValue();
    387. if (0 == newMonth) {// 在上一年
    388. int preYear = solarYear - 1;
    389. leapMonth = LunarData.LUNAR_INFO[preYear - LunarData.MINI_YEAR][0];
    390. short[] preSolarCodes = LunarData.LUNAR_INFO[preYear - LunarData.MINI_YEAR];
    391. // 取上年农历121号公历日期码
    392. int nearSolarCode = preSolarCodes[preSolarCodes.length - 1]; // 上一年121
    393. // 下一年公历1月表示为了13月,这里做翻译,并计算出日期码
    394. nearSolarCode = (nearSolarCode / 100 == 13 ? preYear + 1 : preYear) * 10000
    395. + (nearSolarCode / 100 == 13 ? nearSolarCode - 1200 : nearSolarCode);
    396. if (nearSolarCode > solarCode) {// 此公历日期在上一年农历121号,之前,即在上年农历11月内
    397. newMonth = 11;
    398. // 取农历11月的公历码
    399. nearSolarCode = preYear * 10000 + preSolarCodes[preSolarCodes.length - 2];
    400. } else {// 此公历日期在上一年农历12月内
    401. newMonth = 12;
    402. }
    403. xDate = Long.valueOf(LunarCodes.solarDateCodesDiff(solarCode, nearSolarCode, Calendar.DATE)).intValue();
    404. if (xDate < 0) {
    405. throw new IllegalArgumentException("Wrong solarCode: " + solarCode);
    406. }
    407. this.dayOfLunarMonth = 1 + xDate;
    408. this.lunarYear = preYear;
    409. this.lunarMonth = newMonth;
    410. this.isLeapMonth = 0 != leapMonth && (leapMonth == newMonth);
    411. } else if (solarCodes.length == newMonth + 1 && xDate >= 30) {// 在下一年(公历12月只有30天)
    412. newMonth = 1; // 农历肯定是1
    413. // 取下一年的公历日期码
    414. short[] nextSolarCodes = LunarData.LUNAR_INFO[solarYear + 1 - LunarData.MINI_YEAR];
    415. // 取下一年农历11号公历日期码
    416. int nearSolarCode = solarYear * 10000 + nextSolarCodes[1]; // 下一年农历11号公历日期码
    417. xDate = Long.valueOf(LunarCodes.solarDateCodesDiff(solarCode, nearSolarCode, Calendar.DATE)).intValue();
    418. if (xDate < 0) {
    419. throw new IllegalArgumentException("Wrong solarCode: " + solarCode);
    420. }
    421. this.dayOfLunarMonth = 1 + xDate;
    422. this.lunarYear = solarYear + 1; // 农历年到了下一年
    423. this.lunarMonth = newMonth;
    424. this.isLeapMonth = false; // 农历1月不可能为闰月
    425. } else {
    426. if (xDate < 0) {
    427. throw new IllegalArgumentException("Wrong solarCode: " + solarCode);
    428. }
    429. this.dayOfLunarMonth = 1 + xDate;
    430. this.lunarYear = solarYear;
    431. this.isLeapMonth = 0 != leapMonth && (leapMonth + 1 == newMonth);
    432. if (0 != leapMonth && leapMonth < newMonth) {
    433. this.lunarMonth = newMonth - 1;
    434. } else {
    435. this.lunarMonth = newMonth;
    436. }
    437. }
    438. }
    439. // ------------------------ getter and setter --------------------------------
    440. public int getLunarYear() {
    441. return lunarYear;
    442. }
    443. public int getLunarMonth() {
    444. return lunarMonth;
    445. }
    446. public int getDayOfLunarMonth() {
    447. return dayOfLunarMonth;
    448. }
    449. public int getLeapMonth() {
    450. return leapMonth;
    451. }
    452. public boolean isLeapMonth() {
    453. return isLeapMonth;
    454. }
    455. }

    1. package com.github.heqiao2010.lunar;
    2. import java.util.AbstractMap;
    3. import java.util.Calendar;
    4. import java.util.GregorianCalendar;
    5. import java.util.Map;
    6. import static com.github.heqiao2010.lunar.LunarData.LUNAR_INFO;
    7. /**
    8. * 农历/公历对照代码工具方法集合
    9. */
    10. public class LunarCodes {
    11. /**
    12. * 农历年的代码数组
    13. * @param year 农历年
    14. * @return 代码数组
    15. */
    16. public static short[] monthCodes(int year) {
    17. return LUNAR_INFO[year - LunarData.MINI_YEAR];
    18. }
    19. /**
    20. * 从代码中获取年份,大于12时表示下一年
    21. * @param code 代码
    22. * @return 0/1 今年或者下一年
    23. */
    24. public static int codeYear(int code) {
    25. return code / 100 > 12 ? 1 : 0;
    26. }
    27. /**
    28. * 从代码中获取农历月初一在公历中的月份
    29. * @param code 代码
    30. * @return 公历月份
    31. */
    32. public static int codeMonth(int code) {
    33. int m = code / 100;
    34. if (m > 12) m -= 12;
    35. return m;
    36. }
    37. /**
    38. * 从代码中获取农历日在公历中的日 (day of month)
    39. * @param code 代码
    40. * @return 公历日
    41. */
    42. public static int codeDay(int code) {
    43. return code % 100;
    44. }
    45. /**
    46. * 求一个农历月的天数
    47. * @param lunarYear 农历年
    48. * @param month 农历月
    49. * @param code 农历月有日期,Mdd 表示
    50. * @return 月的天数
    51. */
    52. public static long lengthOfMonth(int lunarYear, int month, short code) {
    53. short md2;
    54. short[] starts = monthCodes(lunarYear);
    55. int y2 = lunarYear;
    56. if (month + 1 < starts.length && starts[month] == code) {
    57. md2 = starts[month + 1];
    58. } else if (month + 2 < starts.length && starts[month + 1] == code) {
    59. md2 = starts[month + 2];
    60. } else if (lunarYear - LunarData.MINI_YEAR + 1 < LUNAR_INFO.length) {
    61. md2 = monthCodes(lunarYear + 1)[1];
    62. y2 ++;
    63. } else {
    64. throw new IllegalArgumentException("lunar date out of range");
    65. }
    66. int y1 = lunarYear + codeYear(code);
    67. int m1 = codeMonth(code);
    68. int d1 = codeDay(code);
    69. y2 += codeYear(md2);
    70. int m2 = codeMonth(md2);
    71. int d2 = codeDay(md2);
    72. Calendar c1 = Calendar.getInstance();
    73. c1.set(y1, m1 - 1, d1);
    74. Calendar c2 = Calendar.getInstance();
    75. c2.set(y2, m2 - 1, d2);
    76. return LunarUtils.solarDiff(c2, c1, Calendar.DATE);
    77. }
    78. /**
    79. * 根据农历年和 LUNAR_INFO 中的下标来确定月份和闰月
    80. *
    81. * @param year 农历年
    82. * @param index LUNAR_INFO 月份数组中的下标
    83. * @return 月, 闰月
    84. */
    85. public static Map.Entry<Integer, Boolean> month(int year, int index) {
    86. short[] a = monthCodes(year);
    87. int i = index;
    88. if (index == -1) {
    89. i = a.length - 1;
    90. }
    91. boolean isLeap = a[0] > 0 && a[0] + 1 == i;
    92. int month = isLeap || a[0] > 0 && a[0] < i ? i - 1 : i;
    93. return new AbstractMap.SimpleImmutableEntry(month, isLeap);
    94. }
    95. /**
    96. * 计算月份 Mdd 代码在数组中的位置
    97. *
    98. * @param year 农历年
    99. * @param month 农历月
    100. * @param isLeapMonth 闰月
    101. * @return 月所在的下标
    102. */
    103. public static int monthIndex(int year, int month, boolean isLeapMonth) {
    104. short[] a = monthCodes(year);
    105. if (a[0] > 0 && a[0] < month || a[0] == month && isLeapMonth) {
    106. return month + 1;
    107. }
    108. return month;
    109. }
    110. /**
    111. * 判断两个整数所代表公历日期的差值
    112. * 一年按365天计算,一个月按30天计算
    113. *
    114. * @param solarCode1  农历日期代码
    115. * @param solarCode2  农历日期代码
    116. * @param field  差值单位
    117. * @return 差值
    118. */
    119. public static long solarDateCodesDiff(int solarCode1, int solarCode2, int field) {
    120. GregorianCalendar c1 = new GregorianCalendar(solarCode1 / 10000, solarCode1 % 10000 / 100 - 1,
    121. solarCode1 % 10000 % 100);
    122. GregorianCalendar c2 = new GregorianCalendar(solarCode2 / 10000, solarCode2 % 10000 / 100 - 1,
    123. solarCode2 % 10000 % 100);
    124. return LunarUtils.solarDiff(c1, c2, field);
    125. }
    126. /**
    127. * 农历月的代码
    128. * @param lunarYear 农历月
    129. * @param lunarMonth 农历月
    130. * @param isLeapMonth 闰月
    131. * @return 代码
    132. */
    133. public static short lunarMonthCode(int lunarYear, int lunarMonth, boolean isLeapMonth) {
    134. short[] codes = monthCodes(lunarYear);
    135. int index = lunarMonth;
    136. if (codes[0] > 0 && codes[0] < lunarMonth || codes[0] == lunarMonth && isLeapMonth) {
    137. index++;
    138. }
    139. return codes[index];
    140. }
    141. }

    1. package com.github.heqiao2010.lunar;
    2. public class LunarData {
    3. /**
    4. * 支持的最小日期1850-02-12
    5. */
    6. public final static int MINI_YEAR = 1850;
    7. public final static int MINI_MONTH = 1;
    8. public final static int MINI_DATE = 12;
    9. /**
    10. * 支持的最大日期2150-12-31
    11. */
    12. public final static int MAX_YEAR = 2150;
    13. public final static int MAX_MONTH = 11;
    14. public final static int MAX_DATE = 31;
    15. /**
    16. * 10天干
    17. * '甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'
    18. */
    19. static final char[] LunarGan = {'\u7532', '\u4e59', '\u4e19', '\u4e01', '\u620a', '\u5df1', '\u5e9a',
    20. '\u8f9b', '\u58ec', '\u7678'};
    21. /**
    22. * 12地支
    23. * '子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'
    24. */
    25. static final char[] LunarZhi = {'\u5b50', '\u4e11', '\u5bc5', '\u536f', '\u8fb0', '\u5df3', '\u5348',
    26. '\u672a', '\u7533', '\u9149', '\u620c', '\u4ea5'};
    27. /**
    28. * 12生肖
    29. * '鼠', '牛', '虎', '兔', '龍', '蛇', '馬', '羊', '猴', '雞', '犬', '豬'
    30. */
    31. static final char[] LunarAnimalName = {'\u9f20', '\u725b', '\u864e', '\u5154', '\u9f8d', '\u86c7',
    32. '\u99ac', '\u7f8a', '\u7334', '\u96de', '\u72ac', '\u8c6c'};
    33. /**
    34. * 农历年份名
    35. * '〇', '一', '二', '三', '四', '五', '六', '七', '八', '九'
    36. */
    37. static final char[] LunarYearName = {'\u3007', '\u4e00', '\u4e8c', '\u4e09', '\u56db', '\u4e94',
    38. '\u516d', '\u4e03', '\u516b', '\u4e5d'};
    39. /**
    40. * 农历月份名
    41. * '正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊'
    42. */
    43. static final char[] LunarMonthName = {'\u6b63', '\u4e8c', '\u4e09', '\u56db', '\u4e94', '\u516d',
    44. '\u4e03', '\u516b', '\u4e5d', '\u5341', '\u51ac', '\u814a'};
    45. /**
    46. * 农历日期名
    47. * "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九",
    48. * "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二",
    49. * "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"
    50. */
    51. static final String[] LunarDayName = {"\u521d\u4e00", "\u521d\u4e8c", "\u521d\u4e09", "\u521d\u56db",
    52. "\u521d\u4e94", "\u521d\u516d", "\u521d\u4e03", "\u521d\u516b", "\u521d\u4e5d", "\u521d\u5341",
    53. "\u5341\u4e00", "\u5341\u4e8c", "\u5341\u4e09", "\u5341\u56db", "\u5341\u4e94", "\u5341\u516d",
    54. "\u5341\u4e03", "\u5341\u516b", "\u5341\u4e5d", "\u4e8c\u5341", "\u5eff\u4e00", "\u5eff\u4e8c",
    55. "\u5eff\u4e09", "\u5eff\u56db", "\u5eff\u4e94", "\u5eff\u516d", "\u5eff\u4e03", "\u5eff\u516b",
    56. "\u5eff\u4e5d", "\u4e09\u5341"};
    57. /**
    58. * 农历信息.
    59. * 每个数组的第一个数表示该年闰月月份,为0表示不闰月
    60. * 数组中其他数表示该月初一对应的公历日期
    61. */
    62. final static short[][] LUNAR_INFO = {
    63. {0, 212, 314, 412, 512, 610, 709, 808, 906, 1005, 1104, 1204, 1302}, // 1850
    64. {8, 201, 303, 402, 501, 531, 629, 728, 827, 925, 1024, 1123, 1222, 1321}, // 1851
    65. {0, 220, 321, 331, 419, 519, 618, 717, 815, 914, 1013, 1112, 1211, 1309}, // 1852
    66. {0, 208, 310, 408, 508, 607, 706, 805, 903, 1003, 1101, 1201, 1230}, // 1853
    67. {7, 129, 227, 329, 427, 527, 625, 725, 824, 922, 1022, 1120, 1220, 1318}, // 1854
    68. {0, 217, 318, 416, 516, 614, 714, 813, 911, 1011, 1110, 1209, 1308}, // 1855
    69. {0, 206, 307, 405, 504, 603, 702, 801, 830, 929, 1029, 1128, 1227}, // 1856
    70. {5, 126, 224, 326, 424, 523, 622, 721, 820, 918, 1018, 1116, 1216, 1315}, // 1857
    71. {0, 214, 315, 414, 513, 611, 711, 809, 907, 1007, 1106, 1205, 1304}, // 1858
    72. {0, 203, 305, 403, 503, 601, 630, 730, 828, 926, 1026, 1124, 1224}, // 1859
    73. {3, 123, 222, 322, 421, 521, 619, 718, 817, 915, 1014, 1113, 1212, 1311}, // 1860
    74. {0, 210, 311, 410, 510, 608, 708, 806, 905, 1004, 1103, 1202, 1231}, // 1861
    75. {8, 130, 301, 330, 429, 528, 627, 727, 825, 924, 1023, 1122, 1221, 1319}, // 1862
    76. {0, 218, 319, 418, 518, 616, 716, 814, 913, 1013, 1111, 1211, 1309}, // 1863
    77. {0, 208, 308, 406, 506, 604, 704, 802, 901, 1001, 1030, 1129, 1229, 1327}, // 1864
    78. {6, 127, 226, 327, 425, 525, 623, 723, 821, 920, 1020, 1118, 1218, 1317}, // 1865
    79. {0, 215, 317, 415, 514, 613, 712, 810, 909, 1009, 1107, 1207, 1306}, // 1866
    80. {0, 205, 306, 405, 504, 602, 702, 731, 829, 928, 1027, 1126, 1226}, // 1867
    81. {4, 125, 223, 324, 423, 522, 620, 720, 818, 916, 1016, 1114, 1214, 1313}, // 1868
    82. {0, 211, 313, 412, 512, 610, 709, 808, 906, 1005, 1104, 1203, 1302}, // 1869
    83. {10, 131, 302, 401, 501, 530, 629, 728, 827, 925, 1024, 1123, 1222, 1321}, // 1870
    84. {0, 219, 321, 420, 519, 618, 718, 816, 915, 1014, 1113, 1212, 1310}, // 1871
    85. {0, 209, 309, 408, 507, 606, 706, 804, 903, 1002, 1101, 1201, 1230}, // 1872
    86. {6, 129, 227, 328, 427, 526, 625, 724, 823, 922, 1021, 1120, 1220, 1318}, // 1873
    87. {0, 217, 318, 416, 516, 614, 714, 812, 911, 1010, 1109, 1209, 1308}, // 1874
    88. {0, 206, 308, 406, 505, 604, 703, 801, 831, 929, 1029, 1128, 1228}, // 1875
    89. {5, 126, 225, 326, 424, 523, 622, 721, 819, 918, 1017, 1116, 1216, 1314}, // 1876
    90. {0, 213, 315, 414, 513, 611, 711, 809, 907, 1007, 1105, 1205, 1303}, // 1877
    91. {0, 202, 304, 403, 502, 601, 630, 730, 828, 926, 1026, 1124, 1224}, // 1878
    92. {3, 122, 221, 323, 421, 521, 620, 719, 818, 916, 1015, 1114, 1213, 1312}, // 1879
    93. {0, 210, 311, 409, 509, 608, 707, 806, 905, 1004, 1103, 1202, 1231}, // 1880
    94. {7, 130, 228, 330, 428, 528, 626, 726, 825, 923, 1023, 1122, 1221, 1320}, // 1881
    95. {0, 218, 319, 418, 517, 616, 715, 814, 912, 1012, 1111, 1210, 1309}, // 1882
    96. {0, 208, 309, 407, 507, 605, 704, 803, 901, 1001, 1031, 1130, 1229}, // 1883
    97. {5, 128, 227, 327, 425, 525, 623, 722, 821, 919, 1019, 1118, 1217, 1316}, // 1884
    98. {0, 215, 317, 415, 514, 613, 712, 810, 909, 1008, 1107, 1206, 1305}, // 1885
    99. {0, 204, 306, 404, 504, 602, 702, 731, 829, 928, 1027, 1126, 1225}, // 1886
    100. {4, 124, 223, 325, 423, 523, 621, 721, 819, 917, 1017, 1115, 1215, 1313}, // 1887
    101. {0, 212, 313, 411, 511, 610, 709, 808, 906, 1005, 1104, 1203, 1302}, // 1888
    102. {12, 131, 302, 331, 430, 530, 628, 728, 826, 925, 1024, 1123, 1222, 1321}, // 1889
    103. {0, 219, 321, 419, 519, 617, 717, 816, 914, 1014, 1112, 1212, 1310}, // 1890
    104. {0, 209, 310, 409, 508, 607, 706, 805, 903, 1003, 1102, 1201, 1231}, // 1891
    105. {6, 130, 228, 328, 427, 526, 624, 724, 822, 921, 1021, 1119, 1219, 1318}, // 1892
    106. {0, 217, 318, 416, 516, 614, 713, 812, 910, 1010, 1108, 1208, 1307}, // 1893
    107. {0, 206, 307, 406, 505, 604, 703, 801, 831, 929, 1029, 1127, 1227}, // 1894
    108. {5, 126, 225, 326, 425, 524, 623, 722, 820, 919, 1018, 1117, 1216, 1315}, // 1895
    109. {0, 214, 314, 413, 513, 611, 711, 809, 907, 1007, 1105, 1205, 1303}, // 1896
    110. {0, 202, 303, 402, 502, 531, 630, 730, 828, 926, 1026, 1124, 1224}, // 1897
    111. {3, 122, 221, 322, 421, 520, 619, 719, 817, 916, 1015, 1114, 1213, 1312}, // 1898
    112. {0, 210, 312, 410, 510, 608, 708, 806, 905, 1005, 1103, 1203, 1301}, // 1899
    113. {8, 131, 301, 331, 429, 528, 627, 726, 825, 924, 1023, 1122, 1222, 1320}, // 1900
    114. {0, 219, 320, 419, 518, 616, 716, 814, 913, 1012, 1111, 1211, 1310}, // 1901
    115. {0, 208, 310, 408, 508, 606, 705, 804, 902, 1002, 1031, 1130, 1230}, // 1902
    116. {5, 129, 227, 329, 427, 527, 625, 724, 823, 921, 1020, 1119, 1219, 1317}, // 1903
    117. {0, 216, 317, 416, 515, 614, 713, 811, 910, 1009, 1107, 1207, 1306}, // 1904
    118. {0, 204, 306, 405, 504, 603, 703, 801, 830, 929, 1028, 1127, 1226}, // 1905
    119. {4, 125, 223, 325, 424, 523, 622, 721, 820, 918, 1018, 1116, 1216, 1314}, // 1906
    120. {0, 213, 314, 413, 512, 611, 710, 809, 908, 1007, 1106, 1205, 1304}, // 1907
    121. {0, 202, 303, 401, 430, 530, 629, 728, 827, 925, 1025, 1124, 1223}, // 1908
    122. {2, 122, 220, 322, 420, 519, 618, 717, 816, 914, 1014, 1113, 1213, 1311}, // 1909
    123. {0, 210, 311, 410, 509, 607, 707, 805, 904, 1003, 1102, 1202, 1301}, // 1910
    124. {6, 130, 301, 330, 429, 528, 626, 726, 824, 922, 1022, 1121, 1220, 1319}, // 1911
    125. {0, 218, 319, 417, 517, 615, 714, 813, 911, 1010, 1109, 1209, 1307}, // 1912
    126. {0, 206, 308, 407, 506, 605, 704, 802, 901, 930, 1029, 1128, 1227}, // 1913
    127. {5, 126, 225, 327, 425, 525, 623, 723, 821, 920, 1019, 1117, 1217, 1315}, // 1914
    128. {0, 214, 316, 414, 514, 613, 712, 811, 909, 1009, 1107, 1207, 1305}, // 1915
    129. {0, 203, 304, 403, 502, 601, 630, 730, 829, 927, 1027, 1125, 1225}, // 1916
    130. {2, 123, 222, 323, 421, 521, 619, 719, 818, 916, 1016, 1115, 1214, 1313}, // 1917
    131. {0, 211, 313, 411, 510, 609, 708, 807, 905, 1005, 1104, 1203, 1302}, // 1918
    132. {7, 201, 302, 401, 430, 529, 628, 727, 825, 924, 1024, 1122, 1222, 1321}, // 1919
    133. {0, 220, 320, 419, 518, 616, 716, 814, 912, 1012, 1110, 1210, 1309}, // 1920
    134. {0, 208, 310, 408, 508, 606, 705, 804, 902, 1001, 1031, 1129, 1229}, // 1921
    135. {5, 128, 227, 328, 427, 527, 625, 724, 823, 921, 1020, 1119, 1218, 1317}, // 1922
    136. {0, 216, 317, 416, 516, 614, 714, 812, 911, 1010, 1108, 1208, 1306}, // 1923
    137. {0, 205, 305, 404, 504, 602, 702, 801, 830, 929, 1028, 1127, 1226}, // 1924
    138. {4, 124, 223, 324, 423, 522, 621, 721, 819, 918, 1018, 1116, 1216, 1314}, // 1925
    139. {0, 213, 314, 412, 512, 610, 710, 808, 907, 1007, 1105, 1205, 1304}, // 1926
    140. {0, 202, 304, 402, 501, 531, 629, 729, 827, 926, 1025, 1124, 1224}, // 1927
    141. {2, 123, 221, 322, 420, 519, 618, 717, 815, 914, 1013, 1112, 1212, 1311}, // 1928
    142. {0, 210, 311, 410, 509, 607, 707, 805, 903, 1003, 1101, 1201, 1231}, // 1929
    143. {6, 130, 228, 330, 429, 528, 626, 726, 824, 922, 1022, 1120, 1220, 1319}, // 1930
    144. {0, 217, 319, 418, 517, 616, 715, 814, 912, 1011, 1110, 1209, 1308}, // 1931
    145. {0, 206, 307, 406, 506, 604, 704, 802, 901, 930, 1029, 1128, 1227}, // 1932
    146. {5, 126, 224, 326, 425, 524, 623, 723, 821, 920, 1019, 1118, 1217, 1315}, // 1933
    147. {0, 214, 315, 414, 513, 612, 712, 810, 909, 1008, 1107, 1207, 1305}, // 1934
    148. {0, 204, 305, 403, 503, 601, 701, 730, 829, 928, 1027, 1126, 1226}, // 1935
    149. {3, 124, 223, 323, 421, 521, 619, 718, 817, 916, 1015, 1114, 1214, 1313}, // 1936
    150. {0, 211, 313, 411, 510, 609, 708, 806, 905, 1004, 1103, 1203, 1302}, // 1937
    151. {7, 131, 302, 401, 430, 529, 628, 727, 825, 924, 1023, 1122, 1222, 1320}, // 1938
    152. {0, 219, 321, 420, 519, 617, 717, 815, 913, 1013, 1111, 1211, 1309}, // 1939
    153. {0, 208, 309, 408, 507, 606, 705, 804, 902, 1001, 1031, 1129, 1229}, // 1940
    154. {6, 127, 226, 328, 426, 526, 625, 724, 823, 921, 1020, 1119, 1218, 1317}, // 1941
    155. {0, 215, 317, 415, 515, 614, 713, 812, 910, 1010, 1108, 1208, 1306}, // 1942
    156. {0, 205, 306, 405, 504, 603, 702, 801, 831, 929, 1029, 1127, 1227}, // 1943
    157. {4, 125, 224, 324, 423, 522, 621, 720, 819, 917, 1017, 1116, 1215, 1314}, // 1944
    158. {0, 213, 314, 412, 512, 610, 709, 808, 906, 1006, 1105, 1205, 1303}, // 1945
    159. {0, 202, 304, 402, 501, 531, 629, 728, 827, 925, 1025, 1124, 1223}, // 1946
    160. {2, 122, 221, 323, 421, 520, 619, 718, 816, 915, 1014, 1113, 1212, 1311}, // 1947
    161. {0, 210, 311, 409, 509, 607, 707, 805, 903, 1003, 1101, 1201, 1230}, // 1948
    162. {7, 129, 228, 329, 428, 528, 626, 726, 824, 922, 1022, 1120, 1220, 1318}, // 1949
    163. {0, 217, 318, 417, 517, 615, 715, 814, 912, 1011, 1110, 1209, 1308}, // 1950
    164. {0, 206, 308, 406, 506, 605, 704, 803, 901, 1001, 1030, 1129, 1228}, // 1951
    165. {5, 127, 225, 326, 424, 524, 622, 722, 820, 919, 1019, 1117, 1217, 1315}, // 1952
    166. {0, 214, 315, 414, 513, 611, 711, 810, 908, 1008, 1107, 1206, 1305}, // 1953
    167. {0, 203, 305, 403, 503, 601, 630, 730, 828, 927, 1027, 1125, 1225}, // 1954
    168. {3, 124, 222, 324, 422, 522, 620, 719, 818, 916, 1016, 1114, 1214, 1313}, // 1955
    169. {0, 212, 312, 411, 510, 609, 708, 806, 905, 1004, 1103, 1202, 1301}, // 1956
    170. {8, 131, 302, 331, 430, 529, 628, 727, 825, 924, 1023, 1122, 1221, 1320}, // 1957
    171. {0, 218, 320, 419, 519, 617, 717, 815, 913, 1013, 1111, 1211, 1309}, // 1958
    172. {0, 208, 309, 408, 508, 606, 706, 804, 903, 1002, 1101, 1130, 1230}, // 1959
    173. {6, 128, 227, 327, 426, 525, 624, 724, 822, 921, 1020, 1119, 1218, 1317}, // 1960
    174. {0, 215, 317, 415, 515, 613, 713, 811, 910, 1010, 1108, 1208, 1306}, // 1961
    175. {0, 205, 306, 405, 504, 602, 702, 731, 830, 929, 1028, 1127, 1227}, // 1962
    176. {4, 125, 224, 325, 424, 523, 621, 721, 819, 918, 1017, 1116, 1216, 1315}, // 1963
    177. {0, 213, 314, 412, 512, 610, 709, 808, 906, 1006, 1104, 1204, 1303}, // 1964
    178. {0, 202, 303, 402, 501, 531, 629, 728, 827, 925, 1024, 1123, 1223}, // 1965
    179. {3, 121, 220, 322, 421, 520, 619, 718, 816, 915, 1014, 1112, 1212, 1311}, // 1966
    180. {0, 209, 311, 410, 509, 608, 708, 806, 904, 1004, 1102, 1202, 1231}, // 1967
    181. {7, 130, 228, 329, 427, 527, 626, 725, 824, 922, 1022, 1120, 1220, 1318}, // 1968
    182. {0, 217, 318, 417, 516, 615, 714, 813, 912, 1011, 1110, 1209, 1308}, // 1969
    183. {0, 206, 308, 406, 505, 604, 703, 802, 901, 930, 1030, 1129, 1228}, // 1970
    184. {5, 127, 225, 327, 425, 524, 623, 722, 821, 919, 1019, 1118, 1218, 1316}, // 1971
    185. {0, 215, 315, 414, 513, 611, 711, 809, 908, 1007, 1106, 1206, 1304}, // 1972
    186. {0, 203, 305, 403, 503, 601, 630, 730, 828, 926, 1026, 1125, 1224}, // 1973
    187. {4, 123, 222, 324, 422, 522, 620, 719, 818, 916, 1015, 1114, 1214, 1312}, // 1974
    188. {0, 211, 313, 412, 511, 610, 709, 807, 906, 1005, 1103, 1203, 1301}, // 1975
    189. {8, 131, 301, 331, 429, 529, 627, 727, 825, 924, 1023, 1121, 1221, 1319}, // 1976
    190. {0, 218, 320, 418, 518, 617, 716, 815, 913, 1013, 1111, 1211, 1309}, // 1977
    191. {0, 207, 309, 407, 507, 606, 705, 804, 903, 1002, 1101, 1130, 1230}, // 1978
    192. {6, 128, 227, 328, 426, 526, 624, 724, 823, 921, 1021, 1120, 1219, 1318}, // 1979
    193. {0, 216, 317, 415, 514, 613, 712, 811, 909, 1009, 1108, 1207, 1306}, // 1980
    194. {0, 205, 306, 405, 504, 602, 702, 731, 829, 928, 1028, 1126, 1226}, // 1981
    195. {4, 125, 224, 325, 424, 523, 621, 721, 819, 917, 1017, 1115, 1215, 1314}, // 1982
    196. {0, 213, 315, 413, 513, 611, 710, 809, 907, 1006, 1105, 1204, 1303}, // 1983
    197. {10, 202, 303, 401, 501, 531, 629, 728, 827, 925, 1024, 1123, 1222, 1321}, // 1984
    198. {0, 220, 321, 420, 520, 618, 718, 816, 915, 1014, 1112, 1212, 1310}, // 1985
    199. {0, 209, 310, 409, 509, 607, 707, 806, 904, 1004, 1102, 1202, 1231}, // 1986
    200. {6, 129, 228, 329, 428, 527, 626, 726, 824, 923, 1023, 1121, 1221, 1319}, // 1987
    201. {0, 217, 318, 416, 516, 614, 714, 812, 911, 1011, 1109, 1209, 1308}, // 1988
    202. {0, 206, 308, 406, 505, 604, 703, 802, 831, 930, 1029, 1128, 1228}, // 1989
    203. {5, 127, 225, 327, 425, 524, 623, 722, 820, 919, 1018, 1117, 1217, 1316}, // 1990
    204. {0, 215, 316, 415, 514, 612, 712, 810, 908, 1008, 1106, 1206, 1305}, // 1991
    205. {0, 204, 304, 403, 503, 601, 630, 730, 828, 926, 1026, 1124, 1224}, // 1992
    206. {3, 123, 221, 323, 422, 521, 620, 719, 818, 916, 1015, 1114, 1213, 1312}, // 1993
    207. {0, 210, 312, 411, 511, 609, 709, 807, 906, 1005, 1103, 1203, 1301}, // 1994
    208. {8, 131, 301, 331, 430, 529, 628, 727, 826, 925, 1024, 1122, 1222, 1320}, // 1995
    209. {0, 219, 319, 418, 517, 616, 716, 814, 913, 1012, 1111, 1211, 1309}, // 1996
    210. {0, 207, 309, 407, 507, 605, 705, 803, 902, 1002, 1031, 1130, 1230}, // 1997
    211. {5, 128, 227, 328, 426, 526, 624, 723, 822, 921, 1020, 1119, 1219, 1317}, // 1998
    212. {0, 216, 318, 416, 515, 614, 713, 811, 910, 1009, 1108, 1208, 1307}, // 1999
    213. {0, 205, 306, 405, 504, 602, 702, 731, 829, 928, 1027, 1126, 1226}, // 2000
    214. {4, 124, 223, 325, 423, 523, 621, 721, 819, 917, 1017, 1115, 1215, 1313}, // 2001
    215. {0, 212, 314, 413, 512, 611, 710, 809, 907, 1006, 1105, 1204, 1303}, // 2002
    216. {0, 201, 303, 402, 501, 531, 630, 729, 828, 926, 1025, 1124, 1223}, // 2003
    217. {2, 122, 220, 321, 419, 519, 618, 717, 816, 914, 1014, 1112, 1212, 1310}, // 2004
    218. {0, 209, 310, 409, 508, 607, 706, 805, 904, 1003, 1102, 1201, 1231}, // 2005
    219. {7, 129, 228, 329, 428, 527, 626, 725, 824, 922, 1022, 1121, 1220, 1319}, // 2006
    220. {0, 218, 319, 417, 517, 615, 714, 813, 911, 1011, 1110, 1210, 1308}, // 2007
    221. {0, 207, 308, 406, 505, 604, 703, 801, 831, 929, 1029, 1128, 1227}, // 2008
    222. {5, 126, 225, 327, 425, 524, 623, 722, 820, 919, 1018, 1117, 1216, 1315}, // 2009
    223. {0, 214, 316, 414, 514, 612, 712, 810, 908, 1008, 1106, 1206, 1304}, // 2010
    224. {0, 203, 305, 403, 503, 602, 701, 731, 829, 927, 1027, 1125, 1225}, // 2011
    225. {4, 123, 222, 322, 421, 521, 619, 719, 817, 916, 1015, 1114, 1213, 1312}, // 2012
    226. {0, 210, 312, 410, 510, 608, 708, 807, 905, 1005, 1103, 1203, 1301}, // 2013
    227. {9, 131, 301, 331, 429, 529, 627, 727, 825, 924, 1024, 1122, 1222, 1320}, // 2014
    228. {0, 219, 320, 419, 518, 616, 716, 814, 913, 1013, 1112, 1211, 1310}, // 2015
    229. {0, 208, 309, 407, 507, 605, 704, 803, 901, 1001, 1031, 1129, 1229}, // 2016
    230. {6, 128, 226, 328, 426, 526, 624, 723, 822, 920, 1020, 1118, 1218, 1317}, // 2017
    231. {0, 216, 317, 416, 515, 614, 713, 811, 910, 1009, 1108, 1207, 1306}, // 2018
    232. {0, 205, 307, 405, 505, 603, 703, 801, 830, 929, 1028, 1126, 1226}, // 2019
    233. {4, 125, 223, 324, 423, 523, 621, 721, 819, 917, 1017, 1115, 1215, 1313}, // 2020
    234. {0, 212, 313, 412, 512, 610, 710, 808, 907, 1006, 1105, 1204, 1303}, // 2021
    235. {0, 201, 303, 401, 501, 530, 629, 729, 827, 926, 1025, 1124, 1223}, // 2022
    236. {2, 122, 220, 322, 420, 519, 618, 718, 816, 915, 1015, 1113, 1213, 1311}, // 2023
    237. {0, 210, 310, 409, 508, 606, 706, 804, 903, 1003, 1101, 1201, 1231}, // 2024
    238. {6, 129, 228, 329, 428, 527, 625, 725, 823, 922, 1021, 1120, 1220, 1319}, // 2025
    239. {0, 217, 319, 417, 517, 615, 714, 813, 911, 1010, 1109, 1209, 1308}, // 2026
    240. {0, 206, 308, 407, 506, 605, 704, 802, 901, 930, 1029, 1128, 1228}, // 2027
    241. {5, 126, 225, 326, 425, 524, 623, 722, 820, 919, 1018, 1116, 1216, 1315}, // 2028
    242. {0, 213, 315, 414, 513, 612, 711, 810, 908, 1008, 1106, 1205, 1304}, // 2029
    243. {0, 203, 304, 403, 502, 601, 701, 730, 829, 927, 1027, 1125, 1225}, // 2030
    244. {3, 123, 221, 323, 422, 521, 620, 719, 818, 917, 1016, 1115, 1214, 1313}, // 2031
    245. {0, 211, 312, 410, 509, 608, 707, 806, 905, 1004, 1103, 1203, 1301}, // 2032
    246. {11, 131, 301, 331, 429, 528, 627, 726, 825, 923, 1023, 1122, 1222, 1320}, // 2033
    247. {0, 219, 320, 419, 518, 616, 716, 814, 913, 1012, 1111, 1211, 1309}, // 2034
    248. {0, 208, 310, 408, 508, 606, 705, 804, 902, 1001, 1031, 1130, 1229}, // 2035
    249. {6, 128, 227, 328, 426, 526, 624, 723, 822, 920, 1019, 1118, 1217, 1316}, // 2036
    250. {0, 215, 317, 416, 515, 614, 713, 811, 910, 1009, 1107, 1207, 1305}, // 2037
    251. {0, 204, 306, 405, 504, 603, 702, 801, 830, 929, 1028, 1126, 1226}, // 2038
    252. {5, 124, 223, 325, 423, 523, 622, 721, 820, 918, 1018, 1116, 1216, 1314}, // 2039
    253. {0, 212, 313, 411, 511, 610, 709, 808, 906, 1006, 1105, 1204, 1303}, // 2040
    254. {0, 201, 302, 401, 430, 530, 628, 728, 827, 925, 1025, 1124, 1223}, // 2041
    255. {2, 122, 220, 322, 420, 519, 618, 717, 816, 914, 1014, 1113, 1212, 1311}, // 2042
    256. {0, 210, 311, 410, 509, 607, 707, 805, 903, 1003, 1102, 1201, 1231}, // 2043
    257. {7, 130, 229, 329, 428, 527, 625, 725, 823, 921, 1021, 1119, 1219, 1318}, // 2044
    258. {0, 217, 319, 417, 517, 615, 714, 813, 911, 1010, 1109, 1208, 1307}, // 2045
    259. {0, 206, 308, 406, 506, 604, 704, 802, 901, 930, 1029, 1128, 1227}, // 2046
    260. {5, 126, 225, 326, 425, 525, 623, 723, 821, 920, 1019, 1117, 1217, 1315}, // 2047
    261. {0, 214, 314, 413, 513, 611, 711, 810, 908, 1008, 1106, 1205, 1304}, // 2048
    262. {0, 202, 304, 402, 502, 531, 630, 730, 828, 927, 1027, 1125, 1225}, // 2049
    263. {3, 123, 221, 323, 421, 521, 619, 719, 817, 916, 1016, 1114, 1214, 1313}, // 2050
    264. {0, 211, 313, 411, 510, 609, 708, 806, 905, 1005, 1103, 1203, 1302}, // 2051
    265. {8, 201, 301, 331, 429, 528, 627, 726, 824, 923, 1022, 1121, 1221, 1320}, // 2052
    266. {0, 219, 320, 419, 518, 616, 716, 814, 912, 1012, 1110, 1210, 1309}, // 2053
    267. {0, 208, 309, 408, 508, 606, 705, 804, 902, 1001, 1031, 1129, 1229}, // 2054
    268. {6, 128, 226, 328, 427, 526, 625, 724, 823, 921, 1020, 1119, 1218, 1317}, // 2055
    269. {0, 215, 316, 415, 515, 613, 713, 811, 910, 1009, 1107, 1207, 1305}, // 2056
    270. {0, 204, 305, 404, 504, 602, 702, 731, 830, 928, 1028, 1126, 1226}, // 2057
    271. {4, 124, 223, 324, 423, 522, 621, 720, 819, 918, 1017, 1116, 1216, 1314}, // 2058
    272. {0, 212, 314, 412, 512, 610, 710, 808, 907, 1006, 1105, 1205, 1304}, // 2059
    273. {0, 202, 303, 401, 430, 530, 628, 727, 826, 924, 1024, 1123, 1223}, // 2060
    274. {3, 121, 220, 322, 420, 519, 618, 717, 815, 914, 1013, 1112, 1212, 1311}, // 2061
    275. {0, 209, 311, 410, 509, 607, 707, 805, 903, 1003, 1101, 1201, 1231}, // 2062
    276. {7, 129, 228, 330, 428, 528, 626, 726, 824, 922, 1022, 1120, 1220, 1318}, // 2063
    277. {0, 217, 318, 417, 516, 615, 714, 813, 911, 1010, 1109, 1208, 1307}, // 2064
    278. {0, 205, 307, 406, 505, 604, 704, 802, 901, 930, 1029, 1128, 1227}, // 2065
    279. {5, 126, 224, 326, 424, 524, 623, 722, 821, 919, 1019, 1117, 1217, 1315}, // 2066
    280. {0, 214, 315, 414, 513, 612, 711, 810, 909, 1008, 1107, 1206, 1305}, // 2067
    281. {0, 203, 304, 402, 502, 531, 629, 729, 828, 926, 1026, 1125, 1224}, // 2068
    282. {4, 123, 221, 323, 421, 521, 619, 718, 817, 915, 1015, 1114, 1214, 1312}, // 2069
    283. {0, 211, 312, 411, 510, 609, 708, 806, 905, 1004, 1103, 1203, 1301}, // 2070
    284. {8, 131, 302, 331, 430, 529, 628, 727, 825, 924, 1023, 1122, 1221, 1320}, // 2071
    285. {0, 219, 320, 418, 518, 616, 716, 814, 912, 1012, 1110, 1210, 1308}, // 2072
    286. {0, 207, 309, 407, 507, 606, 705, 804, 902, 1001, 1031, 1129, 1229}, // 2073
    287. {6, 127, 226, 327, 426, 526, 624, 724, 822, 921, 1020, 1119, 1218, 1317}, // 2074
    288. {0, 215, 317, 415, 515, 613, 713, 812, 910, 1010, 1108, 1208, 1306}, // 2075
    289. {0, 205, 305, 404, 503, 602, 701, 731, 829, 928, 1028, 1126, 1226}, // 2076
    290. {4, 124, 223, 324, 423, 522, 620, 720, 818, 917, 1017, 1116, 1215, 1314}, // 2077
    291. {0, 212, 314, 412, 512, 610, 709, 808, 906, 1006, 1105, 1204, 1303}, // 2078
    292. {0, 202, 303, 402, 501, 531, 629, 728, 827, 925, 1025, 1123, 1223}, // 2079
    293. {3, 122, 221, 321, 420, 519, 618, 717, 815, 914, 1013, 1111, 1211, 1310}, // 2080
    294. {0, 209, 310, 409, 509, 607, 707, 805, 903, 1003, 1101, 1130, 1230}, // 2081
    295. {7, 129, 227, 329, 428, 528, 626, 725, 824, 922, 1022, 1120, 1219, 1318}, // 2082
    296. {0, 217, 318, 417, 517, 615, 715, 813, 912, 1011, 1110, 1209, 1308}, // 2083
    297. {0, 206, 307, 405, 505, 603, 703, 802, 831, 930, 1029, 1128, 1227}, // 2084
    298. {5, 126, 224, 326, 424, 523, 622, 722, 820, 919, 1019, 1117, 1217, 1315}, // 2085
    299. {0, 214, 315, 414, 513, 611, 711, 809, 908, 1008, 1106, 1206, 1305}, // 2086
    300. {0, 203, 305, 403, 503, 601, 630, 730, 828, 927, 1026, 1125, 1225}, // 2087
    301. {4, 124, 222, 323, 421, 521, 619, 718, 817, 915, 1014, 1113, 1213, 1312}, // 2088
    302. {0, 210, 312, 411, 510, 609, 708, 806, 904, 1004, 1102, 1202, 1301}, // 2089
    303. {8, 130, 301, 331, 430, 529, 628, 727, 825, 924, 1023, 1121, 1221, 1320}, // 2090
    304. {0, 218, 320, 419, 518, 617, 716, 815, 913, 1013, 1111, 1210, 1309}, // 2091
    305. {0, 207, 308, 407, 506, 605, 705, 803, 902, 1001, 1031, 1129, 1229}, // 2092
    306. {6, 127, 225, 327, 426, 525, 624, 723, 822, 921, 1020, 1119, 1218, 1317}, // 2093
    307. {0, 215, 316, 415, 514, 613, 712, 811, 910, 1009, 1108, 1208, 1306}, // 2094
    308. {0, 205, 306, 405, 504, 602, 702, 731, 830, 928, 1028, 1127, 1227}, // 2095
    309. {4, 125, 224, 324, 423, 522, 620, 720, 818, 916, 1016, 1115, 1215, 1313}, // 2096
    310. {0, 212, 314, 412, 512, 610, 709, 807, 906, 1005, 1104, 1204, 1302}, // 2097
    311. {0, 201, 303, 402, 501, 531, 629, 728, 826, 925, 1024, 1123, 1222}, // 2098
    312. {2, 121, 220, 322, 420, 520, 619, 718, 816, 915, 1014, 1112, 1212, 1310}, // 2099
    313. {0, 209, 311, 410, 509, 608, 707, 806, 904, 1004, 1102, 1201, 1231}, // 2010
    314. {7, 129, 228, 330, 428, 528, 626, 726, 825, 923, 1023, 1121, 1220, 1319}, //2101
    315. {0, 217, 319, 417, 517, 616, 715, 814, 912, 1012, 1111, 1210, 1309}, //2102
    316. {0, 207, 308, 407, 506, 605, 704, 803, 901, 1001, 1031, 1130, 1229}, //2103
    317. {5, 128, 226, 327, 425, 524, 623, 722, 821, 919, 1019, 1118, 1217, 1316}, //2104
    318. {0, 215, 316, 415, 514, 612, 712, 810, 908, 1008, 1107, 1206, 1305}, //2105
    319. {0, 204, 306, 404, 504, 602, 701, 731, 829, 927, 1027, 1125, 1225}, //2106
    320. {4, 124, 223, 325, 423, 523, 621, 720, 819, 917, 1016, 1115, 1214, 1313}, //2107
    321. {0, 212, 313, 411, 511, 609, 709, 807, 906, 1005, 1103, 1203, 1301}, //2108
    322. {9, 131, 302, 331, 430, 530, 628, 728, 826, 925, 1024, 1122, 1222, 1320}, //2109
    323. {0, 219, 320, 419, 519, 617, 717, 816, 914, 1014, 1112, 1211, 1310}, //2110
    324. {0, 208, 310, 408, 508, 606, 706, 805, 903, 1003, 1101, 1201, 1231}, //2111
    325. {6, 129, 227, 328, 426, 526, 624, 724, 822, 921, 1021, 1119, 1219, 1318}, //2112
    326. {0, 216, 318, 416, 515, 614, 713, 811, 910, 1010, 1108, 1208, 1307}, //2113
    327. {0, 206, 307, 406, 505, 603, 703, 801, 830, 929, 1028, 1127, 1227}, //2114
    328. {4, 126, 225, 326, 425, 524, 622, 722, 820, 918, 1018, 1116, 1216, 1315}, //2115
    329. {0, 214, 314, 413, 512, 611, 710, 809, 907, 1006, 1105, 1204, 1303}, //2116
    330. {0, 202, 303, 402, 502, 531, 630, 729, 828, 926, 1025, 1124, 1223}, //2117
    331. {3, 122, 220, 322, 421, 520, 619, 719, 817, 916, 1015, 1113, 1213, 1311}, //2118
    332. {0, 210, 311, 410, 509, 608, 708, 806, 905, 1004, 1103, 1202, 1301}, //2119
    333. {7, 130, 229, 329, 428, 527, 626, 725, 824, 923, 1022, 1121, 1220, 1319}, //2120
    334. {0, 217, 319, 417, 517, 615, 715, 813, 912, 1011, 1110, 1210, 1309}, //2121
    335. {0, 207, 308, 407, 506, 605, 704, 802, 901, 930, 1030, 1129, 1229}, //2122
    336. {5, 127, 226, 328, 426, 525, 624, 723, 821, 920, 1019, 1118, 1218, 1317}, //2123
    337. {0, 215, 316, 414, 514, 612, 712, 810, 908, 1008, 1106, 1206, 1305}, //2124
    338. {0, 203, 305, 404, 503, 602, 701, 731, 829, 927, 1027, 1125, 1225}, //2125
    339. {4, 123, 222, 324, 423, 522, 621, 720, 819, 917, 1016, 1115, 1214, 1313}, //2126
    340. {0, 211, 313, 412, 511, 610, 710, 808, 906, 1006, 1104, 1204, 1302}, //2127
    341. {11, 201, 301, 331, 429, 529, 628, 727, 826, 924, 1024, 1122, 1222, 1320}, //2128
    342. {0, 219, 320, 419, 518, 617, 716, 815, 914, 1013, 1112, 1211, 1310}, //2129
    343. {0, 208, 310, 408, 508, 606, 705, 804, 903, 1002, 1101, 1201, 1230}, //2130
    344. {6, 129, 227, 329, 427, 527, 625, 724, 823, 921, 1021, 1120, 1220, 1318}, //2131
    345. {0, 217, 317, 416, 515, 614, 713, 811, 910, 1009, 1108, 1208, 1306}, //2132
    346. {0, 205, 307, 405, 505, 603, 703, 801, 830, 928, 1028, 1127, 1226}, //2133
    347. {5, 125, 224, 326, 424, 524, 622, 722, 820, 918, 1018, 1116, 1216, 1314}, //2134
    348. {0, 213, 315, 413, 513, 612, 711, 809, 908, 1007, 1106, 1205, 1304}, //2135
    349. {0, 202, 303, 401, 501, 531, 629, 729, 827, 926, 1025, 1124, 1223}, //2136
    350. {2, 122, 220, 322, 420, 520, 618, 718, 817, 915, 1015, 1113, 1213, 1311}, //2137
    351. {0, 210, 311, 410, 509, 608, 707, 806, 904, 1004, 1103, 1202, 1301}, //2138
    352. {7, 130, 301, 330, 429, 528, 626, 726, 824, 923, 1023, 1122, 1221, 1320}, //2139
    353. {0, 218, 319, 417, 517, 615, 714, 813, 911, 1011, 1110, 1209, 1308}, //2140
    354. {0, 207, 308, 407, 506, 605, 704, 802, 901, 930, 1030, 1128, 1228}, //2141
    355. {5, 127, 226, 327, 426, 525, 624, 723, 821, 919, 1019, 1117, 1217, 1316}, //2142
    356. {0, 215, 316, 415, 515, 613, 713, 811, 909, 1008, 1107, 1206, 1305}, //2143
    357. {0, 204, 304, 403, 503, 602, 701, 730, 829, 927, 1027, 1125, 1224}, //2144
    358. {4, 123, 222, 323, 422, 522, 620, 720, 818, 917, 1016, 1115, 1214, 1313}, //2145
    359. {0, 211, 312, 411, 511, 609, 709, 807, 906, 1006, 1104, 1204, 1302}, //2146
    360. {0, 201, 302, 331, 430, 529, 628, 728, 826, 925, 1025, 1123, 1223}, //2147
    361. {1, 121, 220, 320, 419, 518, 616, 716, 814, 913, 1013, 1111, 1211, 1310}, //2148
    362. {0, 208, 310, 408, 508, 606, 705, 804, 902, 1002, 1031, 1130, 1230}, //2149
    363. {6, 129, 227, 329, 427, 527, 625, 724, 822, 921, 1020, 1119, 1219, 1318}, //2150
    364. };
    365. /**
    366. * 获取农历日的表示
    367. *
    368. * @param lunarDay  农历日数值表示
    369. * @return 农历日传统字符表示
    370. */
    371. public static String getDayName(int lunarDay) {
    372. return LunarDayName[lunarDay - 1];
    373. }
    374. /**
    375. * 获取农历月份
    376. *
    377. * @param lunarMonth  农历月数值表示
    378. * @return 农历月传统字符表示
    379. */
    380. public static char getMonthName(int lunarMonth) {
    381. return LunarMonthName[lunarMonth - 1];
    382. }
    383. /**
    384. * 获取农历年份
    385. *
    386. * @param lunarYear  农历年数值表示
    387. * @return 农历年传统字符表示
    388. */
    389. public static String getYearName(int lunarYear) {
    390. StringBuilder sb = new StringBuilder();
    391. sb.append(LunarYearName[lunarYear / 1000]);
    392. sb.append(LunarYearName[lunarYear % 1000 / 100]);
    393. sb.append(LunarYearName[lunarYear % 100 / 10]);
    394. sb.append(LunarYearName[lunarYear % 10]);
    395. return sb.toString();
    396. }
    397. /**
    398. * 返回传统天干地支年名称
    399. *
    400. * @param y 农历年
    401. * @return 传统农历年份的表示
    402. */
    403. public static String getTraditionalYearName(int y) {
    404. // 1804年是甲子年
    405. y = y - 1804;
    406. return ("" + LunarGan[y % 10] + LunarZhi[y % 12] + "年");
    407. }
    408. /**
    409. * 获取生肖名
    410. *
    411. * @param y  农历年
    412. * @return 生肖名
    413. */
    414. public static char getAnimalYearName(int y) {
    415. return LunarAnimalName[(y - 4) % 12];
    416. }
    417. }

    1. package com.github.heqiao2010.lunar;
    2. import java.util.Calendar;
    3. public class LunarUtils {
    4. /**
    5. * 计算两个农历日期之差
    6. *
    7. * @param lc1  农历1
    8. * @param lc2  农历2
    9. * @param field  计算的维度,比如按月,天等
    10. * @return 具体的差值
    11. */
    12. public static long luanrDiff(LunarCalendar lc1, LunarCalendar lc2, int field) {
    13. return solarDiff(lc1, lc2, field);
    14. }
    15. /**
    16. * 求两个公历日期之差,field可以为年月日,时分秒
    17. * 一年按365天计算,一个月按30天计算
    18. *
    19. * @param solar1  历日期
    20. * @param solar2  历日期
    21. * @param field 差值单位
    22. * @return 差值
    23. */
    24. public static long solarDiff(Calendar solar1, Calendar solar2, int field) {
    25. long t1 = solar1.getTimeInMillis();
    26. long t2 = solar2.getTimeInMillis();
    27. switch (field) {
    28. case Calendar.SECOND:
    29. return (long) Math.rint((t1 - t2) / 1000.0);
    30. case Calendar.MINUTE:
    31. return (long) Math.rint((t1 - t2) / 60000.0); // 60 * 1000
    32. case Calendar.HOUR:
    33. return (long) Math.rint((t1 - t2) / 3600000.0); //3600 * 1000
    34. case Calendar.DATE:
    35. return (long) Math.rint((t1 - t2) / 86400000.0); // 24 * 3600 * 1000
    36. case Calendar.MONTH:
    37. return (long) Math.rint((t1 - t2) / 2592000000.0); // 30 * 24 * 3600 * 1000
    38. case Calendar.YEAR:
    39. return (long) Math.rint((t1 - t2) / 31536000000.0); // 365 * 24 * 3600 * 1000
    40. default:
    41. return -1;
    42. }
    43. }
    44. /**
    45. * 农历月的天数
    46. *
    47. * @param lunarYear 农历年
    48. * @param month 农历月
    49. * @param isLeapMonth 闰月
    50. * @return 天数,29或者30
    51. */
    52. public static long lengthOfMonth(int lunarYear, int month, boolean isLeapMonth) {
    53. short[] codes = LunarCodes.monthCodes(lunarYear);
    54. int i = isLeapMonth ? month + 1 : month;
    55. if (codes[0] > 0 && month > codes[0]) i++;
    56. return LunarCodes.lengthOfMonth(lunarYear, month, codes[i]);
    57. }
    58. /**
    59. * 一个简单的二分查找,返回查找到的元素坐标,用于查找农历二维数组信息
    60. *
    61. * @param array  数组
    62. * @param n  待查询数字
    63. * @return 查到的坐标
    64. */
    65. public static int binSearch(int[] array, int n) {
    66. if (null == array || array.length == 0) {
    67. return -1;
    68. }
    69. int min = 0, max = array.length - 1;
    70. if (n <= array[min]) {
    71. return min;
    72. } else if (n >= array[max]) {
    73. return max;
    74. }
    75. while (max - min > 1) {
    76. int newIndex = (max + min) / 2; // 二分
    77. if (array[newIndex] > n) { // 取小区间
    78. max = newIndex;
    79. } else if (array[newIndex] < n) {// 取大区间
    80. min = newIndex;
    81. } else { // 相等,直接返回下标
    82. return newIndex;
    83. }
    84. }
    85. if (array[max] == n) {
    86. return max;
    87. } else if (array[min] == n) {
    88. return min;
    89. } else {
    90. return min; // 返回 较小的一个
    91. }
    92. }
    93. }

  • 相关阅读:
    【计算机毕业设计】Java ssm 高校运动会管理系统(开题+源码+论文)
    Metabase学习教程:提问-5
    js之页面列表加载常用方法总结
    virtio device type : Block Device
    vscode上搭建go开发环境
    apache shiro安全框架反序列化漏洞
    开源:Taurus.DistributedLock 分布式锁框架,支持 .Net 和 .Net Core 双系列版本
    【浅谈设计模式】装饰器模式篇
    题目0156-计算网络信号
    python编写小游戏之三入最最简陋简单贪食蛇编写2
  • 原文地址:https://blog.csdn.net/spencer_tseng/article/details/134042438