当前位置:   article > 正文

中国农历1900-2100算法工具类_java 农历计算 2100

java 农历计算 2100
Github工程源码:<a target=_blank href="https://github.com/SalamanderJY/ChineseCalendar">https://github.com/SalamanderJY/ChineseCalendar</a>
  1. package com.date;
  2. import java.util.Calendar;
  3. import java.util.Date;
  4. import java.util.GregorianCalendar;
  5. /**
  6. * 农历日历。<br>
  7. * 将农历从1901年到2100年之间各年、月的大小以及历年节气保存,然后基于这些数据进行计算。<br>
  8. * <br>
  9. * 新增了几个用于农历的常量属性字段,可以使用get()方法获取日历对应的值;<br>
  10. * 农历年、月、日还可以使用set()/add()/roll()方法设置,其他农历属性自动计算;<br>
  11. * 另外,还提供了getChinese(int field)方法用于获得农历的中文文字(仅适用于农历属性和星期)。<br>
  12. * <ul>
  13. * <li>CHINESE_YEAR - 农历年</li>
  14. * <li>CHINESE_MONTH - 农历月</li>
  15. * <li>CHINESE_DATE - 农历日</li>
  16. * <li>CHINESE_SECTIONAL_TERM - 当月的节气</li>
  17. * <li>CHINESE_PRINCIPLE_TERM - 当月的中气</li>
  18. * <li>CHINESE_HEAVENLY_STEM - 农历年的天干</li>
  19. * <li>CHINESE_EARTHLY_BRANCH - 农历年的地支</li>
  20. * <li>CHINESE_ZODIAC - 农历年的属相</li>
  21. * <li>CHINESE_TERM_OR_DATE - 如果当天存在一个节气则指示节气,否则如果当天是初一则指示农历月,否则指示农历日</li>
  22. * </ul>
  23. * 注意:<br>
  24. * 由于Calendar类的设定,公历月份从0起始。所有方法都遵循了这一约定。<br>
  25. * 但所有的农历属性从1起始。即使是在Calendar提供的方法中,农历月也是从1起始的,并以负数表示闰月。<br>
  26. * clear()方法在某些情况下会导致农历和公历日期不对应或是不能达到预期的重置效果,应尽量避免使用。<br>
  27. * 使用getSimpleDateString()获得公历日期字符串时,公历月已经修正;<br>
  28. * 使用getSimpleChineseDateString()获得农历日期字符串时,农历闰月以*表示。<br>
  29. * <br>
  30. * <i>农历算法来源于<a
  31. * href="http://www.herongyang.com/year_gb/program.html">和荣笔记</a>。</i>
  32. *
  33. * @author Sala
  34. */
  35. public final class ChineseCalendar extends GregorianCalendar {
  36. private static final long serialVersionUID = 8L;
  37. /** 农历年 */
  38. public static final int CHINESE_YEAR = 801;
  39. /** 农历月 */
  40. public static final int CHINESE_MONTH = 802;
  41. /** 农历日 */
  42. public static final int CHINESE_DATE = 803;
  43. /** 当月的节气对应的公历日(前一个节气) */
  44. public static final int CHINESE_SECTIONAL_TERM = 804;
  45. /** 当月的中气对应的公历日(后一个节气) */
  46. public static final int CHINESE_PRINCIPLE_TERM = 805;
  47. /** 天干 */
  48. public static final int CHINESE_HEAVENLY_STEM = 806;
  49. /** 地支 */
  50. public static final int CHINESE_EARTHLY_BRANCH = 807;
  51. /** 农历年的属相(生肖) */
  52. public static final int CHINESE_ZODIAC = 808;
  53. /** 节气或者农历日 */
  54. public static final int CHINESE_TERM_OR_DATE = 888;
  55. private int chineseYear;
  56. private int chineseMonth; // 1起始,负数表示闰月
  57. private int chineseDate;
  58. private int sectionalTerm; // 当月节气的公历日
  59. private int principleTerm; // 当月中气的公历日
  60. private boolean areChineseFieldsComputed; // 农历日期是否已经经过计算确认
  61. private boolean areSolarTermsComputed; // 节气是否已经经过计算确认
  62. private boolean lastSetChinese; // 最后设置的是不是农历属性
  63. /** 使用当前时间构造一个实例。 */
  64. public ChineseCalendar() {
  65. super();
  66. }
  67. /** 使用指定时间构造一个实例。 */
  68. public ChineseCalendar(Date d) {
  69. super.setTime(d);
  70. }
  71. /** 使用指定时间构造一个实例。 */
  72. public ChineseCalendar(Calendar c) {
  73. this(c.getTime());
  74. }
  75. /** 使用指定公历日期构造一个实例。 */
  76. public ChineseCalendar(int y, int m, int d) {
  77. super(y, m, d);
  78. }
  79. /**
  80. * 使用指定日期构造一个实例。
  81. *
  82. * @param isChinese
  83. * 是否为农历日期
  84. * @param y
  85. * @param m
  86. * @param d
  87. */
  88. public ChineseCalendar(boolean isChinese, int y, int m, int d) {
  89. if (isChinese) {
  90. set(CHINESE_YEAR, y);
  91. set(CHINESE_MONTH, m);
  92. set(CHINESE_DATE, d);
  93. } else {
  94. set(y, m, d);
  95. }
  96. }
  97. public void set(int field, int value) {
  98. computeIfNeed(field);
  99. if (isChineseField(field)) {
  100. // 农历属性
  101. switch (field) {
  102. case CHINESE_YEAR:
  103. chineseYear = value;
  104. break;
  105. case CHINESE_MONTH:
  106. chineseMonth = value;
  107. break;
  108. case CHINESE_DATE:
  109. chineseDate = value;
  110. break;
  111. default:
  112. throw new IllegalArgumentException("不支持的field设置:" + field);
  113. }
  114. lastSetChinese = true;
  115. } else {
  116. // 非农历属性
  117. super.set(field, value);
  118. lastSetChinese = false;
  119. }
  120. areFieldsSet = false;
  121. areChineseFieldsComputed = false;
  122. areSolarTermsComputed = false;
  123. }
  124. public int get(int field) {
  125. computeIfNeed(field);
  126. if (!isChineseField(field)) {
  127. return super.get(field);
  128. }
  129. switch (field) {
  130. case CHINESE_YEAR:
  131. return chineseYear;
  132. case CHINESE_MONTH:
  133. return chineseMonth;
  134. case CHINESE_DATE:
  135. return chineseDate;
  136. case CHINESE_SECTIONAL_TERM:
  137. return sectionalTerm;
  138. case CHINESE_PRINCIPLE_TERM:
  139. return principleTerm;
  140. case CHINESE_HEAVENLY_STEM:
  141. return (chineseYear - 4) % 10 + 1;
  142. case CHINESE_EARTHLY_BRANCH:
  143. case CHINESE_ZODIAC:
  144. return (chineseYear - 4) % 12 + 1;
  145. case CHINESE_TERM_OR_DATE:
  146. int option;
  147. if (get(Calendar.DATE) == get(CHINESE_SECTIONAL_TERM)) {
  148. option = CHINESE_SECTIONAL_TERM;
  149. } else if (get(Calendar.DATE) == get(CHINESE_PRINCIPLE_TERM)) {
  150. option = CHINESE_PRINCIPLE_TERM;
  151. } else if (get(CHINESE_DATE) == 1) {
  152. option = CHINESE_MONTH;
  153. } else {
  154. option = CHINESE_DATE;
  155. }
  156. return option;
  157. default:
  158. throw new IllegalArgumentException("不支持的field获取:" + field);
  159. }
  160. }
  161. public void add(int field, int amount) {
  162. computeIfNeed(field);
  163. if (!isChineseField(field)) {
  164. super.add(field, amount);
  165. lastSetChinese = false;
  166. areChineseFieldsComputed = false;
  167. areSolarTermsComputed = false;
  168. return;
  169. }
  170. switch (field) {
  171. case CHINESE_YEAR:
  172. chineseYear += amount;
  173. break;
  174. case CHINESE_MONTH:
  175. for (int i = 0; i < amount; i++) {
  176. chineseMonth = nextChineseMonth(chineseYear, chineseMonth);
  177. if (chineseMonth == 1) {
  178. chineseYear++;
  179. }
  180. }
  181. break;
  182. case CHINESE_DATE:
  183. int maxDate = daysInChineseMonth(chineseYear, chineseMonth);
  184. for (int i = 0; i < amount; i++) {
  185. chineseDate++;
  186. if (chineseDate > maxDate) {
  187. chineseDate = 1;
  188. chineseMonth = nextChineseMonth(chineseYear, chineseMonth);
  189. if (chineseMonth == 1) {
  190. chineseYear++;
  191. }
  192. maxDate = daysInChineseMonth(chineseYear, chineseMonth);
  193. }
  194. }
  195. default:
  196. throw new IllegalArgumentException("不支持的field:" + field);
  197. }
  198. lastSetChinese = true;
  199. areFieldsSet = false;
  200. areChineseFieldsComputed = false;
  201. areSolarTermsComputed = false;
  202. }
  203. public void roll(int field, int amount) {
  204. computeIfNeed(field);
  205. if (!isChineseField(field)) {
  206. super.roll(field, amount);
  207. lastSetChinese = false;
  208. areChineseFieldsComputed = false;
  209. areSolarTermsComputed = false;
  210. return;
  211. }
  212. switch (field) {
  213. case CHINESE_YEAR:
  214. chineseYear += amount;
  215. break;
  216. case CHINESE_MONTH:
  217. for (int i = 0; i < amount; i++) {
  218. chineseMonth = nextChineseMonth(chineseYear, chineseMonth);
  219. }
  220. break;
  221. case CHINESE_DATE:
  222. int maxDate = daysInChineseMonth(chineseYear, chineseMonth);
  223. for (int i = 0; i < amount; i++) {
  224. chineseDate++;
  225. if (chineseDate > maxDate) {
  226. chineseDate = 1;
  227. }
  228. }
  229. default:
  230. throw new IllegalArgumentException("不支持的field:" + field);
  231. }
  232. lastSetChinese = true;
  233. areFieldsSet = false;
  234. areChineseFieldsComputed = false;
  235. areSolarTermsComputed = false;
  236. }
  237. /**
  238. * 获得属性的中文,可以使用的属性字段为DAY_OF_WEEK以及所有农历属性字段。
  239. *
  240. * @param field
  241. * @return
  242. */
  243. public String getChinese(int field) {
  244. computeIfNeed(field);
  245. switch (field) {
  246. case CHINESE_YEAR:
  247. return getChinese(CHINESE_HEAVENLY_STEM)
  248. + getChinese(CHINESE_EARTHLY_BRANCH) + "年";
  249. case CHINESE_MONTH:
  250. if (chineseMonth > 0)
  251. return chineseMonthNames[chineseMonth] + "月";
  252. else
  253. return "闰" + chineseMonthNames[-chineseMonth] + "月";
  254. case CHINESE_DATE:
  255. return chineseDateNames[chineseDate];
  256. case CHINESE_SECTIONAL_TERM:
  257. return sectionalTermNames[get(Calendar.MONTH)];
  258. case CHINESE_PRINCIPLE_TERM:
  259. return principleTermNames[get(Calendar.MONTH)];
  260. case CHINESE_HEAVENLY_STEM:
  261. return stemNames[get(field)];
  262. case CHINESE_EARTHLY_BRANCH:
  263. return branchNames[get(field)];
  264. case CHINESE_ZODIAC:
  265. return animalNames[get(field)];
  266. case Calendar.DAY_OF_WEEK:
  267. return chineseWeekNames[get(field)];
  268. case CHINESE_TERM_OR_DATE:
  269. return getChinese(get(CHINESE_TERM_OR_DATE));
  270. default:
  271. throw new IllegalArgumentException("不支持的field中文获取:" + field);
  272. }
  273. }
  274. public String getSimpleGregorianDateString() {
  275. return new StringBuffer().append(get(YEAR)).append("/")
  276. .append(get(MONTH) + 1).append("/").append(get(DATE))
  277. .toString();
  278. }
  279. public String getSimpleChineseDateString() {
  280. return new StringBuffer()
  281. .append(get(CHINESE_YEAR))
  282. .append("-")
  283. .append(get(CHINESE_MONTH) > 0 ? "" + get(CHINESE_MONTH) : "*"
  284. + (-get(CHINESE_MONTH))).append("-")
  285. .append(get(CHINESE_DATE)).toString();
  286. }
  287. public String getChineseDateString() {
  288. return new StringBuffer().append(getChinese(CHINESE_YEAR))
  289. .append(getChinese(CHINESE_MONTH))
  290. .append(getChinese(CHINESE_DATE)).toString();
  291. }
  292. public String toString() {
  293. StringBuffer buf = new StringBuffer();
  294. buf.append(getSimpleGregorianDateString()).append(" ")
  295. .append(getChinese(DAY_OF_WEEK)).append(" ")
  296. .append(getChineseDateString()).append(" ")
  297. .append(getChinese(CHINESE_ZODIAC)).append("年 ")
  298. .append(get(CHINESE_SECTIONAL_TERM)).append("日")
  299. .append(getChinese(CHINESE_SECTIONAL_TERM)).append(" ")
  300. .append(get(CHINESE_PRINCIPLE_TERM)).append("日")
  301. .append(getChinese(CHINESE_PRINCIPLE_TERM));
  302. return buf.toString();
  303. }
  304. /**
  305. * 判断是不是农历属性
  306. *
  307. * @param field
  308. * @return
  309. */
  310. private boolean isChineseField(int field) {
  311. switch (field) {
  312. case CHINESE_YEAR:
  313. case CHINESE_MONTH:
  314. case CHINESE_DATE:
  315. case CHINESE_SECTIONAL_TERM:
  316. case CHINESE_PRINCIPLE_TERM:
  317. case CHINESE_HEAVENLY_STEM:
  318. case CHINESE_EARTHLY_BRANCH:
  319. case CHINESE_ZODIAC:
  320. case CHINESE_TERM_OR_DATE:
  321. return true;
  322. default:
  323. return false;
  324. }
  325. }
  326. /**
  327. * 判断是不是与节气有关的属性
  328. *
  329. * @param field
  330. * @return
  331. */
  332. private boolean isChineseTermsField(int field) {
  333. switch (field) {
  334. case CHINESE_SECTIONAL_TERM:
  335. case CHINESE_PRINCIPLE_TERM:
  336. case CHINESE_TERM_OR_DATE:
  337. return true;
  338. default:
  339. return false;
  340. }
  341. }
  342. /**
  343. * 如果上一次设置的与这次将要设置或获取的属性不是同一类(农历/公历),<br>
  344. * 例如上一次设置的是农历而现在要设置或获取公历,<br>
  345. * 则需要先根据之前设置的农历日期计算出公历日期。
  346. *
  347. * @param field
  348. */
  349. private void computeIfNeed(int field) {
  350. if (isChineseField(field)) {
  351. if (!lastSetChinese && !areChineseFieldsComputed) {
  352. super.complete();
  353. computeChineseFields();
  354. areFieldsSet = true;
  355. areChineseFieldsComputed = true;
  356. areSolarTermsComputed = false;
  357. }
  358. if (isChineseTermsField(field) && !areSolarTermsComputed) {
  359. computeSolarTerms();
  360. areSolarTermsComputed = true;
  361. }
  362. } else {
  363. if (lastSetChinese && !areFieldsSet) {
  364. computeGregorianFields();
  365. super.complete();
  366. areFieldsSet = true;
  367. areChineseFieldsComputed = true;
  368. areSolarTermsComputed = false;
  369. }
  370. }
  371. }
  372. /**
  373. * 使用农历日期计算出公历日期
  374. */
  375. private void computeGregorianFields() {
  376. int y = chineseYear;
  377. int m = chineseMonth;
  378. int d = chineseDate;
  379. areChineseFieldsComputed = true;
  380. areFieldsSet = true;
  381. lastSetChinese = false;
  382. // 调整日期范围
  383. if (y < 1900)
  384. y = 1899;
  385. else if (y > 2100)
  386. y = 2101;
  387. if (m < -12)
  388. m = -12;
  389. else if (m > 12)
  390. m = 12;
  391. if (d < 1)
  392. d = 1;
  393. else if (d > 30)
  394. d = 30;
  395. int dateint = y * 10000 + Math.abs(m) * 100 + d;
  396. if (dateint < 19001111) { // 太小
  397. set(1901, Calendar.JANUARY, 1);
  398. super.complete();
  399. } else if (dateint > 21001201) { // 太大
  400. set(2100, Calendar.DECEMBER, 31);
  401. super.complete();
  402. } else {
  403. if (Math.abs(m) > 12) {
  404. m = 12;
  405. }
  406. int days = ChineseCalendar.daysInChineseMonth(y, m);
  407. if (days == 0) {
  408. m = -m;
  409. days = ChineseCalendar.daysInChineseMonth(y, m);
  410. }
  411. if (d > days) {
  412. d = days;
  413. }
  414. set(y, Math.abs(m) - 1, d);
  415. computeChineseFields();
  416. int amount = 0;
  417. while (chineseYear != y || chineseMonth != m) {
  418. amount += daysInChineseMonth(chineseYear, chineseMonth);
  419. chineseMonth = nextChineseMonth(chineseYear, chineseMonth);
  420. if (chineseMonth == 1) {
  421. chineseYear++;
  422. }
  423. }
  424. amount += d - chineseDate;
  425. super.add(Calendar.DATE, amount);
  426. }
  427. computeChineseFields();
  428. }
  429. /**
  430. * 使用公历日期计算出农历日期
  431. */
  432. private void computeChineseFields() {
  433. int gregorianYear = internalGet(Calendar.YEAR);
  434. int gregorianMonth = internalGet(Calendar.MONTH) + 1;
  435. int gregorianDate = internalGet(Calendar.DATE);
  436. if (gregorianYear < 1901 || gregorianYear > 2100) {
  437. return;
  438. }
  439. int startYear, startMonth, startDate;
  440. if (gregorianYear < 2000) {
  441. startYear = baseYear;
  442. startMonth = baseMonth;
  443. startDate = baseDate;
  444. chineseYear = baseChineseYear;
  445. chineseMonth = baseChineseMonth;
  446. chineseDate = baseChineseDate;
  447. } else {
  448. // 第二个对应日,用以提高计算效率
  449. // 公历 2000 年 1 月 1 日,对应农历 4697(1999) 年 11 月 25 日
  450. startYear = baseYear + 99;
  451. startMonth = 1;
  452. startDate = 1;
  453. chineseYear = baseChineseYear + 99;
  454. chineseMonth = 11;
  455. chineseDate = 25;
  456. }
  457. int daysDiff = 0;
  458. // 年
  459. for (int i = startYear; i < gregorianYear; i++) {
  460. if (isGregorianLeapYear(i)) {
  461. daysDiff += 366; // leap year
  462. } else {
  463. daysDiff += 365;
  464. }
  465. }
  466. // 月
  467. for (int i = startMonth; i < gregorianMonth; i++) {
  468. daysDiff += daysInGregorianMonth(gregorianYear, i - 1);
  469. }
  470. // 日
  471. daysDiff += gregorianDate - startDate;
  472. chineseDate += daysDiff;
  473. int lastDate = daysInChineseMonth(chineseYear, chineseMonth);
  474. while (chineseDate > lastDate) {
  475. chineseDate -= lastDate;
  476. chineseMonth = nextChineseMonth(chineseYear, chineseMonth);
  477. if (chineseMonth == 1) {
  478. chineseYear++;
  479. }
  480. lastDate = daysInChineseMonth(chineseYear, chineseMonth);
  481. }
  482. }
  483. /**
  484. * 计算节气
  485. */
  486. private void computeSolarTerms() {
  487. int gregorianYear = internalGet(Calendar.YEAR);
  488. int gregorianMonth = internalGet(Calendar.MONTH);
  489. if (gregorianYear < 1901 || gregorianYear > 2100) {
  490. return;
  491. }
  492. sectionalTerm = sectionalTerm(gregorianYear, gregorianMonth);
  493. principleTerm = principleTerm(gregorianYear, gregorianMonth);
  494. }
  495. /* 接下来是静态方法~ */
  496. /**
  497. * 是否为公历闰年
  498. *
  499. * @param year
  500. * @return
  501. */
  502. public static boolean isGregorianLeapYear(int year) {
  503. boolean isLeap = false;
  504. if (year % 4 == 0) {
  505. isLeap = true;
  506. }
  507. if (year % 100 == 0) {
  508. isLeap = false;
  509. }
  510. if (year % 400 == 0) {
  511. isLeap = true;
  512. }
  513. return isLeap;
  514. }
  515. /**
  516. * 计算公历年的当月天数,公历月从0起始!
  517. *
  518. * @param y
  519. * @param m
  520. * @return
  521. */
  522. public static int daysInGregorianMonth(int y, int m) {
  523. int d = daysInGregorianMonth[m];
  524. if (m == Calendar.FEBRUARY && isGregorianLeapYear(y)) {
  525. d++; // 公历闰年二月多一天
  526. }
  527. return d;
  528. }
  529. /**
  530. * 计算公历年当月的节气,公历月从0起始!
  531. *
  532. * @param y
  533. * @param m
  534. * @return
  535. */
  536. public static int sectionalTerm(int y, int m) {
  537. m++;
  538. if (y < 1901 || y > 2100) {
  539. return 0;
  540. }
  541. int index = 0;
  542. int ry = y - baseYear + 1;
  543. while (ry >= sectionalTermYear[m - 1][index]) {
  544. index++;
  545. }
  546. int term = sectionalTermMap[m - 1][4 * index + ry % 4];
  547. if ((ry == 121) && (m == 4)) {
  548. term = 5;
  549. }
  550. if ((ry == 132) && (m == 4)) {
  551. term = 5;
  552. }
  553. if ((ry == 194) && (m == 6)) {
  554. term = 6;
  555. }
  556. return term;
  557. }
  558. /**
  559. * 计算公历年当月的中气,公历月从0起始!
  560. *
  561. * @param y
  562. * @param m
  563. * @return
  564. */
  565. public static int principleTerm(int y, int m) {
  566. m++;
  567. if (y < 1901 || y > 2100) {
  568. return 0;
  569. }
  570. int index = 0;
  571. int ry = y - baseYear + 1;
  572. while (ry >= principleTermYear[m - 1][index]) {
  573. index++;
  574. }
  575. int term = principleTermMap[m - 1][4 * index + ry % 4];
  576. if ((ry == 171) && (m == 3)) {
  577. term = 21;
  578. }
  579. if ((ry == 181) && (m == 5)) {
  580. term = 21;
  581. }
  582. return term;
  583. }
  584. /**
  585. * 计算农历年的天数
  586. *
  587. * @param y
  588. * @param m
  589. * @return
  590. */
  591. public static int daysInChineseMonth(int y, int m) {
  592. // 注意:闰月 m < 0
  593. int index = y - baseChineseYear + baseIndex;
  594. int v = 0;
  595. int l = 0;
  596. int d = 30;
  597. if (1 <= m && m <= 8) {
  598. v = chineseMonths[2 * index];
  599. l = m - 1;
  600. if (((v >> l) & 0x01) == 1) {
  601. d = 29;
  602. }
  603. } else if (9 <= m && m <= 12) {
  604. v = chineseMonths[2 * index + 1];
  605. l = m - 9;
  606. if (((v >> l) & 0x01) == 1) {
  607. d = 29;
  608. }
  609. } else {
  610. v = chineseMonths[2 * index + 1];
  611. v = (v >> 4) & 0x0F;
  612. if (v != Math.abs(m)) {
  613. d = 0;
  614. } else {
  615. d = 29;
  616. for (int i = 0; i < bigLeapMonthYears.length; i++) {
  617. if (bigLeapMonthYears[i] == index) {
  618. d = 30;
  619. break;
  620. }
  621. }
  622. }
  623. }
  624. return d;
  625. }
  626. /**
  627. * 计算农历的下个月
  628. *
  629. * @param y
  630. * @param m
  631. * @return
  632. */
  633. public static int nextChineseMonth(int y, int m) {
  634. int n = Math.abs(m) + 1;
  635. if (m > 0) {
  636. int index = y - baseChineseYear + baseIndex;
  637. int v = chineseMonths[2 * index + 1];
  638. v = (v >> 4) & 0x0F;
  639. if (v == m) {
  640. n = -m;
  641. }
  642. }
  643. if (n == 13) {
  644. n = 1;
  645. }
  646. return n;
  647. }
  648. /* 日历第一天的日期 */
  649. private static final int baseYear = 1901;
  650. private static final int baseMonth = 1;
  651. private static final int baseDate = 1;
  652. private static final int baseIndex = 0;
  653. private static final int baseChineseYear = 1900;
  654. private static final int baseChineseMonth = 11;
  655. private static final int baseChineseDate = 11;
  656. /* 中文字符串 */
  657. private static final String[] chineseWeekNames = { "", "星期日", "星期一", "星期二",
  658. "星期三", "星期四", "星期五", "星期六" };
  659. private static final String[] chineseMonthNames = { "", "正", "二", "三", "四",
  660. "五", "六", "七", "八", "九", "十", "十一", "十二" };
  661. private static final String[] chineseDateNames = { "", "初一", "初二", "初三",
  662. "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四",
  663. "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五",
  664. "廿六", "廿七", "廿八", "廿九", "三十" };
  665. private static final String[] principleTermNames = { "大寒", "雨水", "春分",
  666. "谷雨", "夏满", "夏至", "大暑", "处暑", "秋分", "霜降", "小雪", "冬至" };
  667. private static final String[] sectionalTermNames = { "小寒", "立春", "惊蛰",
  668. "清明", "立夏", "芒种", "小暑", "立秋", "白露", "寒露", "立冬", "大雪" };
  669. private static final String[] stemNames = { "", "甲", "乙", "丙", "丁", "戊",
  670. "己", "庚", "辛", "壬", "癸" };
  671. private static final String[] branchNames = { "", "子", "丑", "寅", "卯", "辰",
  672. "巳", "午", "未", "申", "酉", "戌", "亥" };
  673. private static final String[] animalNames = { "", "鼠", "牛", "虎", "兔", "龙",
  674. "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
  675. /* 接下来是数据压缩表~ */
  676. private static final int[] bigLeapMonthYears = { 6, 14, 19, 25, 33, 36, 38,
  677. 41, 44, 52, 55, 79, 117, 136, 147, 150, 155, 158, 185, 193 };
  678. private static final char[][] sectionalTermMap = {
  679. { 7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 5, 5,
  680. 5, 5, 5, 4, 5, 5 },
  681. { 5, 4, 5, 5, 5, 4, 4, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 3,
  682. 3, 4, 4, 3, 3, 3 },
  683. { 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5,
  684. 5, 5, 4, 5, 5, 5, 5 },
  685. { 5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 4, 4, 5, 5, 4, 4,
  686. 4, 5, 4, 4, 4, 4, 5 },
  687. { 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5,
  688. 5, 5, 4, 5, 5, 5, 5 },
  689. { 6, 6, 7, 7, 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5,
  690. 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 5 },
  691. { 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6,
  692. 7, 7, 6, 6, 6, 7, 7 },
  693. { 8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7,
  694. 7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 7 },
  695. { 8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7,
  696. 7, 7, 6, 7, 7, 7, 7 },
  697. { 9, 9, 9, 9, 8, 9, 9, 9, 8, 8, 9, 9, 8, 8, 8, 9, 8, 8, 8, 8, 7, 8,
  698. 8, 8, 7, 7, 8, 8, 8 },
  699. { 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7,
  700. 7, 7, 6, 6, 7, 7, 7 },
  701. { 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6,
  702. 7, 7, 6, 6, 6, 7, 7 } };
  703. private static final char[][] sectionalTermYear = {
  704. { 13, 49, 85, 117, 149, 185, 201, 250, 250 },
  705. { 13, 45, 81, 117, 149, 185, 201, 250, 250 },
  706. { 13, 48, 84, 112, 148, 184, 200, 201, 250 },
  707. { 13, 45, 76, 108, 140, 172, 200, 201, 250 },
  708. { 13, 44, 72, 104, 132, 168, 200, 201, 250 },
  709. { 5, 33, 68, 96, 124, 152, 188, 200, 201 },
  710. { 29, 57, 85, 120, 148, 176, 200, 201, 250 },
  711. { 13, 48, 76, 104, 132, 168, 196, 200, 201 },
  712. { 25, 60, 88, 120, 148, 184, 200, 201, 250 },
  713. { 16, 44, 76, 108, 144, 172, 200, 201, 250 },
  714. { 28, 60, 92, 124, 160, 192, 200, 201, 250 },
  715. { 17, 53, 85, 124, 156, 188, 200, 201, 250 } };
  716. private static final char[][] principleTermMap = {
  717. { 21, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20,
  718. 20, 20, 20, 20, 20, 19, 20, 20, 20, 19, 19, 20 },
  719. { 20, 19, 19, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 18, 19, 19,
  720. 19, 18, 18, 19, 19, 18, 18, 18, 18, 18, 18, 18 },
  721. { 21, 21, 21, 22, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21,
  722. 20, 20, 20, 21, 20, 20, 20, 20, 19, 20, 20, 20, 20 },
  723. { 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 20, 20, 20, 20,
  724. 19, 20, 20, 20, 19, 19, 20, 20, 19, 19, 19, 20, 20 },
  725. { 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21,
  726. 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 21 },
  727. { 22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22,
  728. 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 21 },
  729. { 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23,
  730. 22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 23 },
  731. { 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23,
  732. 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 23 },
  733. { 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23,
  734. 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 23 },
  735. { 24, 24, 24, 24, 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24,
  736. 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 23 },
  737. { 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23,
  738. 22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 22 },
  739. { 22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 21, 22, 22, 22,
  740. 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21, 22 } };
  741. private static final char[][] principleTermYear = {
  742. { 13, 45, 81, 113, 149, 185, 201 },
  743. { 21, 57, 93, 125, 161, 193, 201 },
  744. { 21, 56, 88, 120, 152, 188, 200, 201 },
  745. { 21, 49, 81, 116, 144, 176, 200, 201 },
  746. { 17, 49, 77, 112, 140, 168, 200, 201 },
  747. { 28, 60, 88, 116, 148, 180, 200, 201 },
  748. { 25, 53, 84, 112, 144, 172, 200, 201 },
  749. { 29, 57, 89, 120, 148, 180, 200, 201 },
  750. { 17, 45, 73, 108, 140, 168, 200, 201 },
  751. { 28, 60, 92, 124, 160, 192, 200, 201 },
  752. { 16, 44, 80, 112, 148, 180, 200, 201 },
  753. { 17, 53, 88, 120, 156, 188, 200, 201 } };
  754. private static final char[] daysInGregorianMonth = { 31, 28, 31, 30, 31,
  755. 30, 31, 31, 30, 31, 30, 31 };
  756. private static final char[] chineseMonths = { 0x00, 0x04, 0xad, 0x08, 0x5a,
  757. 0x01, 0xd5, 0x54, 0xb4, 0x09, 0x64, 0x05, 0x59, 0x45, 0x95, 0x0a,
  758. 0xa6, 0x04, 0x55, 0x24, 0xad, 0x08, 0x5a, 0x62, 0xda, 0x04, 0xb4,
  759. 0x05, 0xb4, 0x55, 0x52, 0x0d, 0x94, 0x0a, 0x4a, 0x2a, 0x56, 0x02,
  760. 0x6d, 0x71, 0x6d, 0x01, 0xda, 0x02, 0xd2, 0x52, 0xa9, 0x05, 0x49,
  761. 0x0d, 0x2a, 0x45, 0x2b, 0x09, 0x56, 0x01, 0xb5, 0x20, 0x6d, 0x01,
  762. 0x59, 0x69, 0xd4, 0x0a, 0xa8, 0x05, 0xa9, 0x56, 0xa5, 0x04, 0x2b,
  763. 0x09, 0x9e, 0x38, 0xb6, 0x08, 0xec, 0x74, 0x6c, 0x05, 0xd4, 0x0a,
  764. 0xe4, 0x6a, 0x52, 0x05, 0x95, 0x0a, 0x5a, 0x42, 0x5b, 0x04, 0xb6,
  765. 0x04, 0xb4, 0x22, 0x6a, 0x05, 0x52, 0x75, 0xc9, 0x0a, 0x52, 0x05,
  766. 0x35, 0x55, 0x4d, 0x0a, 0x5a, 0x02, 0x5d, 0x31, 0xb5, 0x02, 0x6a,
  767. 0x8a, 0x68, 0x05, 0xa9, 0x0a, 0x8a, 0x6a, 0x2a, 0x05, 0x2d, 0x09,
  768. 0xaa, 0x48, 0x5a, 0x01, 0xb5, 0x09, 0xb0, 0x39, 0x64, 0x05, 0x25,
  769. 0x75, 0x95, 0x0a, 0x96, 0x04, 0x4d, 0x54, 0xad, 0x04, 0xda, 0x04,
  770. 0xd4, 0x44, 0xb4, 0x05, 0x54, 0x85, 0x52, 0x0d, 0x92, 0x0a, 0x56,
  771. 0x6a, 0x56, 0x02, 0x6d, 0x02, 0x6a, 0x41, 0xda, 0x02, 0xb2, 0xa1,
  772. 0xa9, 0x05, 0x49, 0x0d, 0x0a, 0x6d, 0x2a, 0x09, 0x56, 0x01, 0xad,
  773. 0x50, 0x6d, 0x01, 0xd9, 0x02, 0xd1, 0x3a, 0xa8, 0x05, 0x29, 0x85,
  774. 0xa5, 0x0c, 0x2a, 0x09, 0x96, 0x54, 0xb6, 0x08, 0x6c, 0x09, 0x64,
  775. 0x45, 0xd4, 0x0a, 0xa4, 0x05, 0x51, 0x25, 0x95, 0x0a, 0x2a, 0x72,
  776. 0x5b, 0x04, 0xb6, 0x04, 0xac, 0x52, 0x6a, 0x05, 0xd2, 0x0a, 0xa2,
  777. 0x4a, 0x4a, 0x05, 0x55, 0x94, 0x2d, 0x0a, 0x5a, 0x02, 0x75, 0x61,
  778. 0xb5, 0x02, 0x6a, 0x03, 0x61, 0x45, 0xa9, 0x0a, 0x4a, 0x05, 0x25,
  779. 0x25, 0x2d, 0x09, 0x9a, 0x68, 0xda, 0x08, 0xb4, 0x09, 0xa8, 0x59,
  780. 0x54, 0x03, 0xa5, 0x0a, 0x91, 0x3a, 0x96, 0x04, 0xad, 0xb0, 0xad,
  781. 0x04, 0xda, 0x04, 0xf4, 0x62, 0xb4, 0x05, 0x54, 0x0b, 0x44, 0x5d,
  782. 0x52, 0x0a, 0x95, 0x04, 0x55, 0x22, 0x6d, 0x02, 0x5a, 0x71, 0xda,
  783. 0x02, 0xaa, 0x05, 0xb2, 0x55, 0x49, 0x0b, 0x4a, 0x0a, 0x2d, 0x39,
  784. 0x36, 0x01, 0x6d, 0x80, 0x6d, 0x01, 0xd9, 0x02, 0xe9, 0x6a, 0xa8,
  785. 0x05, 0x29, 0x0b, 0x9a, 0x4c, 0xaa, 0x08, 0xb6, 0x08, 0xb4, 0x38,
  786. 0x6c, 0x09, 0x54, 0x75, 0xd4, 0x0a, 0xa4, 0x05, 0x45, 0x55, 0x95,
  787. 0x0a, 0x9a, 0x04, 0x55, 0x44, 0xb5, 0x04, 0x6a, 0x82, 0x6a, 0x05,
  788. 0xd2, 0x0a, 0x92, 0x6a, 0x4a, 0x05, 0x55, 0x0a, 0x2a, 0x4a, 0x5a,
  789. 0x02, 0xb5, 0x02, 0xb2, 0x31, 0x69, 0x03, 0x31, 0x73, 0xa9, 0x0a,
  790. 0x4a, 0x05, 0x2d, 0x55, 0x2d, 0x09, 0x5a, 0x01, 0xd5, 0x48, 0xb4,
  791. 0x09, 0x68, 0x89, 0x54, 0x0b, 0xa4, 0x0a, 0xa5, 0x6a, 0x95, 0x04,
  792. 0xad, 0x08, 0x6a, 0x44, 0xda, 0x04, 0x74, 0x05, 0xb0, 0x25, 0x54,
  793. 0x03 };
  794. }

使用方法:(例如已知公历日期)

  1. ChineseCalendar calendar = new ChineseCalendar(false, year, month-1, day);
  2. String result = calendar.toString();
  3. System.out.println(result);


附YYYY/MM/DD正则表达式(可以区分闰年):

Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/1017287
推荐阅读
相关标签
  

闽ICP备14008679号