当前位置:   article > 正文

日历 农历算法_chineselunarcalendar库

chineselunarcalendar库
  1. package com.microdu.common;
  2. /**
  3. * ChineseCalendarGB.java Copyright (c) 1997-2002 by Dr. Herong Yang 中国农历算法 -
  4. * 实用于公历 1901 年至 2100 年之间的 200 年
  5. */
  6. public class CalendarUtil {
  7. private int gregorianYear;
  8. private int gregorianMonth;
  9. private int gregorianDate;
  10. private boolean isGregorianLeap;
  11. private int dayOfYear;
  12. private int dayOfWeek; // 周日一星期的第一天
  13. private int chineseYear;
  14. private int chineseMonth; // 负数表示闰月
  15. private int chineseDate;
  16. private int sectionalTerm;
  17. private int principleTerm;
  18. private static char[] daysInGregorianMonth = { 31, 28, 31, 30, 31, 30, 31,
  19. 31, 30, 31, 30, 31 };
  20. private static String[] stemNames = { "甲", "乙", "丙", "丁", "戊", "己", "庚",
  21. "辛", "壬", "癸" };
  22. private static String[] branchNames = { "子", "丑", "寅", "卯", "辰", "巳", "午",
  23. "未", "申", "酉", "戌", "亥" };
  24. private static String[] animalNames = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马",
  25. "羊", "猴", "鸡", "狗", "猪" };
  26. public static final String[] daysOfMonth = { "1", "2", "3", "4", "5", "6",
  27. "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
  28. "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
  29. "29", "30", "31" };
  30. private String monthOfAlmanac[] = {"正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","冬月","腊月"};
  31. private String daysOfAlmanac[] = { "初一", "初二", "初三", "初四", "初五", "初六",
  32. "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七",
  33. "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八",
  34. "廿九", "三十" }; // 农历的天数
  35. public CalendarUtil() {
  36. setGregorian(1901, 1, 1);
  37. }
  38. /**
  39. * 得到对应天的农历 要判断闰月 月初 月末 *
  40. * @param y
  41. * @param m
  42. * @param d
  43. * @return String
  44. */
  45. public String getChineseDay(int y, int m, int d) {
  46. CalendarUtil c = new CalendarUtil();
  47. c.setGregorian(y, m, d);
  48. c.computeChineseFields();
  49. c.computeSolarTerms();
  50. int cd = c.getChineseDate();
  51. return daysOfAlmanac[cd - 1];
  52. }
  53. /**
  54. * 得到对应天的农历
  55. * 要判断闰月 月初 月末
  56. * @param y
  57. * @param m
  58. * @param d
  59. * @return
  60. */
  61. public String getChineseMonth(int y, int m, int d) {
  62. setGregorian(y,m,d);
  63. computeChineseFields();
  64. computeSolarTerms();
  65. int cd = getChineseMonth();
  66. if(cd < 1 || cd > 29)
  67. cd = 1;
  68. return monthOfAlmanac[cd -1];
  69. }
  70. public void setGregorian(int y, int m, int d) {
  71. gregorianYear = y;
  72. gregorianMonth = m;
  73. gregorianDate = d;
  74. isGregorianLeap = isGregorianLeapYear(y);
  75. dayOfYear = dayOfYear(y, m, d);
  76. dayOfWeek = dayOfWeek(y, m, d);
  77. chineseYear = 0;
  78. chineseMonth = 0;
  79. chineseDate = 0;
  80. sectionalTerm = 0;
  81. principleTerm = 0;
  82. }
  83. // 判断是否是闰年
  84. public static boolean isGregorianLeapYear(int year) {
  85. boolean isLeap = false;
  86. if (year % 4 == 0)
  87. isLeap = true;
  88. if (year % 100 == 0)
  89. isLeap = false;
  90. if (year % 400 == 0)
  91. isLeap = true;
  92. return isLeap;
  93. }
  94. // 返回一个月有几天
  95. public static int daysInGregorianMonth(int y, int m) {
  96. int d = daysInGregorianMonth[m - 1];
  97. if (m == 2 && isGregorianLeapYear(y))
  98. d++; // 公历闰年二月多一天
  99. return d;
  100. }
  101. // 计算当前天在本年中是第几天
  102. public static int dayOfYear(int y, int m, int d) {
  103. int c = 0;
  104. for (int i = 1; i < m; i++) {
  105. c = c + daysInGregorianMonth(y, i);
  106. }
  107. c = c + d;
  108. return c;
  109. }
  110. // 当前天是本周的第几天 , 从星期天开始算
  111. public static int dayOfWeek(int y, int m, int d) {
  112. int w = 1; // 公历一年一月一日是星期一,所以起始值为星期日
  113. y = (y - 1) % 400 + 1; // 公历星期值分部 400 年循环一次
  114. int ly = (y - 1) / 4; // 闰年次数
  115. ly = ly - (y - 1) / 100;
  116. ly = ly + (y - 1) / 400;
  117. int ry = y - 1 - ly; // 常年次数
  118. w = w + ry; // 常年星期值增一
  119. w = w + 2 * ly; // 闰年星期值增二
  120. w = w + dayOfYear(y, m, d);
  121. w = (w - 1) % 7 + 1;
  122. return w;
  123. }
  124. // 农历月份大小压缩表,两个字节表示一年。两个字节共十六个二进制位数,
  125. // 前四个位数表示闰月月份,后十二个位数表示十二个农历月份的大小。
  126. private static char[] chineseMonths = { 0x00, 0x04, 0xad, 0x08, 0x5a, 0x01,
  127. 0xd5, 0x54, 0xb4, 0x09, 0x64, 0x05, 0x59, 0x45, 0x95, 0x0a, 0xa6,
  128. 0x04, 0x55, 0x24, 0xad, 0x08, 0x5a, 0x62, 0xda, 0x04, 0xb4, 0x05,
  129. 0xb4, 0x55, 0x52, 0x0d, 0x94, 0x0a, 0x4a, 0x2a, 0x56, 0x02, 0x6d,
  130. 0x71, 0x6d, 0x01, 0xda, 0x02, 0xd2, 0x52, 0xa9, 0x05, 0x49, 0x0d,
  131. 0x2a, 0x45, 0x2b, 0x09, 0x56, 0x01, 0xb5, 0x20, 0x6d, 0x01, 0x59,
  132. 0x69, 0xd4, 0x0a, 0xa8, 0x05, 0xa9, 0x56, 0xa5, 0x04, 0x2b, 0x09,
  133. 0x9e, 0x38, 0xb6, 0x08, 0xec, 0x74, 0x6c, 0x05, 0xd4, 0x0a, 0xe4,
  134. 0x6a, 0x52, 0x05, 0x95, 0x0a, 0x5a, 0x42, 0x5b, 0x04, 0xb6, 0x04,
  135. 0xb4, 0x22, 0x6a, 0x05, 0x52, 0x75, 0xc9, 0x0a, 0x52, 0x05, 0x35,
  136. 0x55, 0x4d, 0x0a, 0x5a, 0x02, 0x5d, 0x31, 0xb5, 0x02, 0x6a, 0x8a,
  137. 0x68, 0x05, 0xa9, 0x0a, 0x8a, 0x6a, 0x2a, 0x05, 0x2d, 0x09, 0xaa,
  138. 0x48, 0x5a, 0x01, 0xb5, 0x09, 0xb0, 0x39, 0x64, 0x05, 0x25, 0x75,
  139. 0x95, 0x0a, 0x96, 0x04, 0x4d, 0x54, 0xad, 0x04, 0xda, 0x04, 0xd4,
  140. 0x44, 0xb4, 0x05, 0x54, 0x85, 0x52, 0x0d, 0x92, 0x0a, 0x56, 0x6a,
  141. 0x56, 0x02, 0x6d, 0x02, 0x6a, 0x41, 0xda, 0x02, 0xb2, 0xa1, 0xa9,
  142. 0x05, 0x49, 0x0d, 0x0a, 0x6d, 0x2a, 0x09, 0x56, 0x01, 0xad, 0x50,
  143. 0x6d, 0x01, 0xd9, 0x02, 0xd1, 0x3a, 0xa8, 0x05, 0x29, 0x85, 0xa5,
  144. 0x0c, 0x2a, 0x09, 0x96, 0x54, 0xb6, 0x08, 0x6c, 0x09, 0x64, 0x45,
  145. 0xd4, 0x0a, 0xa4, 0x05, 0x51, 0x25, 0x95, 0x0a, 0x2a, 0x72, 0x5b,
  146. 0x04, 0xb6, 0x04, 0xac, 0x52, 0x6a, 0x05, 0xd2, 0x0a, 0xa2, 0x4a,
  147. 0x4a, 0x05, 0x55, 0x94, 0x2d, 0x0a, 0x5a, 0x02, 0x75, 0x61, 0xb5,
  148. 0x02, 0x6a, 0x03, 0x61, 0x45, 0xa9, 0x0a, 0x4a, 0x05, 0x25, 0x25,
  149. 0x2d, 0x09, 0x9a, 0x68, 0xda, 0x08, 0xb4, 0x09, 0xa8, 0x59, 0x54,
  150. 0x03, 0xa5, 0x0a, 0x91, 0x3a, 0x96, 0x04, 0xad, 0xb0, 0xad, 0x04,
  151. 0xda, 0x04, 0xf4, 0x62, 0xb4, 0x05, 0x54, 0x0b, 0x44, 0x5d, 0x52,
  152. 0x0a, 0x95, 0x04, 0x55, 0x22, 0x6d, 0x02, 0x5a, 0x71, 0xda, 0x02,
  153. 0xaa, 0x05, 0xb2, 0x55, 0x49, 0x0b, 0x4a, 0x0a, 0x2d, 0x39, 0x36,
  154. 0x01, 0x6d, 0x80, 0x6d, 0x01, 0xd9, 0x02, 0xe9, 0x6a, 0xa8, 0x05,
  155. 0x29, 0x0b, 0x9a, 0x4c, 0xaa, 0x08, 0xb6, 0x08, 0xb4, 0x38, 0x6c,
  156. 0x09, 0x54, 0x75, 0xd4, 0x0a, 0xa4, 0x05, 0x45, 0x55, 0x95, 0x0a,
  157. 0x9a, 0x04, 0x55, 0x44, 0xb5, 0x04, 0x6a, 0x82, 0x6a, 0x05, 0xd2,
  158. 0x0a, 0x92, 0x6a, 0x4a, 0x05, 0x55, 0x0a, 0x2a, 0x4a, 0x5a, 0x02,
  159. 0xb5, 0x02, 0xb2, 0x31, 0x69, 0x03, 0x31, 0x73, 0xa9, 0x0a, 0x4a,
  160. 0x05, 0x2d, 0x55, 0x2d, 0x09, 0x5a, 0x01, 0xd5, 0x48, 0xb4, 0x09,
  161. 0x68, 0x89, 0x54, 0x0b, 0xa4, 0x0a, 0xa5, 0x6a, 0x95, 0x04, 0xad,
  162. 0x08, 0x6a, 0x44, 0xda, 0x04, 0x74, 0x05, 0xb0, 0x25, 0x54, 0x03 };
  163. // 初始日,公历农历对应日期:
  164. // 公历 1901 年 1 月 1 日,对应农历 4598 年 11 月 11 日
  165. private static int baseYear = 1901;
  166. private static int baseMonth = 1;
  167. private static int baseDate = 1;
  168. private static int baseIndex = 0;
  169. private static int baseChineseYear = 4598 - 1;
  170. private static int baseChineseMonth = 11;
  171. private static int baseChineseDate = 11;
  172. public int computeChineseFields() {
  173. if (gregorianYear < 1901 || gregorianYear > 2100)
  174. return 1;
  175. int startYear = baseYear;
  176. int startMonth = baseMonth;
  177. int startDate = baseDate;
  178. chineseYear = baseChineseYear;
  179. chineseMonth = baseChineseMonth;
  180. chineseDate = baseChineseDate;
  181. // 第二个对应日,用以提高计算效率
  182. // 公历 2000 年 1 月 1 日,对应农历 4697 年 11 月 25 日
  183. if (gregorianYear >= 2000) {
  184. startYear = baseYear + 99;
  185. startMonth = 1;
  186. startDate = 1;
  187. chineseYear = baseChineseYear + 99;
  188. chineseMonth = 11;
  189. chineseDate = 25;
  190. }
  191. int daysDiff = 0;
  192. for (int i = startYear; i < gregorianYear; i++) {
  193. daysDiff += 365;
  194. if (isGregorianLeapYear(i))
  195. daysDiff += 1; // leap year
  196. }
  197. for (int i = startMonth; i < gregorianMonth; i++) {
  198. daysDiff += daysInGregorianMonth(gregorianYear, i);
  199. }
  200. daysDiff += gregorianDate - startDate;
  201. chineseDate += daysDiff;
  202. int lastDate = daysInChineseMonth(chineseYear, chineseMonth);
  203. int nextMonth = nextChineseMonth(chineseYear, chineseMonth);
  204. while (chineseDate > lastDate) {
  205. if (Math.abs(nextMonth) < Math.abs(chineseMonth))
  206. chineseYear++;
  207. chineseMonth = nextMonth;
  208. chineseDate -= lastDate;
  209. lastDate = daysInChineseMonth(chineseYear, chineseMonth);
  210. nextMonth = nextChineseMonth(chineseYear, chineseMonth);
  211. }
  212. return 0;
  213. }
  214. private static int[] bigLeapMonthYears = {
  215. // 大闰月的闰年年份
  216. 6, 14, 19, 25, 33, 36, 38, 41, 44, 52, 55, 79, 117, 136, 147, 150,
  217. 155, 158, 185, 193 };
  218. public static int daysInChineseMonth(int y, int m) {
  219. // 注意:闰月 m < 0
  220. int index = y - baseChineseYear + baseIndex;
  221. int v = 0;
  222. int l = 0;
  223. int d = 30;
  224. if (1 <= m && m <= 8) {
  225. v = chineseMonths[2 * index];
  226. l = m - 1;
  227. if (((v >> l) & 0x01) == 1)
  228. d = 29;
  229. } else if (9 <= m && m <= 12) {
  230. v = chineseMonths[2 * index + 1];
  231. l = m - 9;
  232. if (((v >> l) & 0x01) == 1)
  233. d = 29;
  234. } else {
  235. v = chineseMonths[2 * index + 1];
  236. v = (v >> 4) & 0x0F;
  237. if (v != Math.abs(m)) {
  238. d = 0;
  239. } else {
  240. d = 29;
  241. for (int i = 0; i < bigLeapMonthYears.length; i++) {
  242. if (bigLeapMonthYears[i] == index) {
  243. d = 30;
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. return d;
  250. }
  251. public static int nextChineseMonth(int y, int m) {
  252. int n = Math.abs(m) + 1;
  253. if (m > 0) {
  254. int index = y - baseChineseYear + baseIndex;
  255. int v = chineseMonths[2 * index + 1];
  256. v = (v >> 4) & 0x0F;
  257. if (v == m)
  258. n = -m;
  259. }
  260. if (n == 13)
  261. n = 1;
  262. return n;
  263. }
  264. private static char[][] sectionalTermMap = {
  265. { 7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 5, 5,
  266. 5, 5, 5, 4, 5, 5 },
  267. { 5, 4, 5, 5, 5, 4, 4, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 3,
  268. 3, 4, 4, 3, 3, 3 },
  269. { 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5,
  270. 5, 5, 4, 5, 5, 5, 5 },
  271. { 5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 4, 4, 5, 5, 4, 4,
  272. 4, 5, 4, 4, 4, 4, 5 },
  273. { 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5,
  274. 5, 5, 4, 5, 5, 5, 5 },
  275. { 6, 6, 7, 7, 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5,
  276. 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 5 },
  277. { 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6,
  278. 7, 7, 6, 6, 6, 7, 7 },
  279. { 8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7,
  280. 7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 7 },
  281. { 8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7,
  282. 7, 7, 6, 7, 7, 7, 7 },
  283. { 9, 9, 9, 9, 8, 9, 9, 9, 8, 8, 9, 9, 8, 8, 8, 9, 8, 8, 8, 8, 7, 8,
  284. 8, 8, 7, 7, 8, 8, 8 },
  285. { 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7,
  286. 7, 7, 6, 6, 7, 7, 7 },
  287. { 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6,
  288. 7, 7, 6, 6, 6, 7, 7 } };
  289. private static char[][] sectionalTermYear = {
  290. { 13, 49, 85, 117, 149, 185, 201, 250, 250 },
  291. { 13, 45, 81, 117, 149, 185, 201, 250, 250 },
  292. { 13, 48, 84, 112, 148, 184, 200, 201, 250 },
  293. { 13, 45, 76, 108, 140, 172, 200, 201, 250 },
  294. { 13, 44, 72, 104, 132, 168, 200, 201, 250 },
  295. { 5, 33, 68, 96, 124, 152, 188, 200, 201 },
  296. { 29, 57, 85, 120, 148, 176, 200, 201, 250 },
  297. { 13, 48, 76, 104, 132, 168, 196, 200, 201 },
  298. { 25, 60, 88, 120, 148, 184, 200, 201, 250 },
  299. { 16, 44, 76, 108, 144, 172, 200, 201, 250 },
  300. { 28, 60, 92, 124, 160, 192, 200, 201, 250 },
  301. { 17, 53, 85, 124, 156, 188, 200, 201, 250 } };
  302. private static char[][] principleTermMap = {
  303. { 21, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20,
  304. 20, 20, 20, 20, 20, 19, 20, 20, 20, 19, 19, 20 },
  305. { 20, 19, 19, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 18, 19, 19,
  306. 19, 18, 18, 19, 19, 18, 18, 18, 18, 18, 18, 18 },
  307. { 21, 21, 21, 22, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21,
  308. 20, 20, 20, 21, 20, 20, 20, 20, 19, 20, 20, 20, 20 },
  309. { 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 20, 20, 20, 20,
  310. 19, 20, 20, 20, 19, 19, 20, 20, 19, 19, 19, 20, 20 },
  311. { 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21,
  312. 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 21 },
  313. { 22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22,
  314. 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 21 },
  315. { 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23,
  316. 22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 23 },
  317. { 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23,
  318. 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 23 },
  319. { 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23,
  320. 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 23 },
  321. { 24, 24, 24, 24, 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24,
  322. 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 23 },
  323. { 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23,
  324. 22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 22 },
  325. { 22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 21, 22, 22, 22,
  326. 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21, 22 } };
  327. private static char[][] principleTermYear = {
  328. { 13, 45, 81, 113, 149, 185, 201 },
  329. { 21, 57, 93, 125, 161, 193, 201 },
  330. { 21, 56, 88, 120, 152, 188, 200, 201 },
  331. { 21, 49, 81, 116, 144, 176, 200, 201 },
  332. { 17, 49, 77, 112, 140, 168, 200, 201 },
  333. { 28, 60, 88, 116, 148, 180, 200, 201 },
  334. { 25, 53, 84, 112, 144, 172, 200, 201 },
  335. { 29, 57, 89, 120, 148, 180, 200, 201 },
  336. { 17, 45, 73, 108, 140, 168, 200, 201 },
  337. { 28, 60, 92, 124, 160, 192, 200, 201 },
  338. { 16, 44, 80, 112, 148, 180, 200, 201 },
  339. { 17, 53, 88, 120, 156, 188, 200, 201 } };
  340. public int computeSolarTerms() {
  341. if (gregorianYear < 1901 || gregorianYear > 2100)
  342. return 1;
  343. sectionalTerm = sectionalTerm(gregorianYear, gregorianMonth);
  344. principleTerm = principleTerm(gregorianYear, gregorianMonth);
  345. return 0;
  346. }
  347. public static int sectionalTerm(int y, int m) {
  348. if (y < 1901 || y > 2100)
  349. return 0;
  350. int index = 0;
  351. int ry = y - baseYear + 1;
  352. while (ry >= sectionalTermYear[m - 1][index])
  353. index++;
  354. int term = sectionalTermMap[m - 1][4 * index + ry % 4];
  355. if ((ry == 121) && (m == 4))
  356. term = 5;
  357. if ((ry == 132) && (m == 4))
  358. term = 5;
  359. if ((ry == 194) && (m == 6))
  360. term = 6;
  361. return term;
  362. }
  363. public static int principleTerm(int y, int m) {
  364. if (y < 1901 || y > 2100)
  365. return 0;
  366. int index = 0;
  367. int ry = y - baseYear + 1;
  368. while (ry >= principleTermYear[m - 1][index])
  369. index++;
  370. int term = principleTermMap[m - 1][4 * index + ry % 4];
  371. if ((ry == 171) && (m == 3))
  372. term = 21;
  373. if ((ry == 181) && (m == 5))
  374. term = 21;
  375. return term;
  376. }
  377. public String toString() {
  378. StringBuffer buf = new StringBuffer();
  379. buf.append("Gregorian Year: " + gregorianYear + "\n");
  380. buf.append("Gregorian Month: " + gregorianMonth + "\n");
  381. buf.append("Gregorian Date: " + gregorianDate + "\n");
  382. buf.append("Is Leap Year: " + isGregorianLeap + "\n");
  383. buf.append("Day of Year: " + dayOfYear + "\n");
  384. buf.append("Day of Week: " + dayOfWeek + "\n");
  385. buf.append("Chinese Year: " + chineseYear + "\n");
  386. buf.append("Heavenly Stem: " + ((chineseYear - 1) % 10) + "\n");
  387. buf.append("Earthly Branch: " + ((chineseYear - 1) % 12) + "\n");
  388. buf.append("Chinese Month: " + chineseMonth + "\n");
  389. buf.append("Chinese Date: " + chineseDate + "\n");
  390. buf.append("Sectional Term: " + sectionalTerm + "\n");
  391. buf.append("Principle Term: " + principleTerm + "\n");
  392. return buf.toString();
  393. }
  394. public String[] getYearTable() {
  395. setGregorian(gregorianYear, 1, 1);
  396. computeChineseFields();
  397. computeSolarTerms();
  398. String[] table = new String[58]; // 6*9 + 4
  399. table[0] = getTextLine(27, "公历年历:" + gregorianYear);
  400. table[1] = getTextLine(27, "农历年历:" + (chineseYear + 1) + " ("
  401. + stemNames[(chineseYear + 1 - 1) % 10]
  402. + branchNames[(chineseYear + 1 - 1) % 12] + " - "
  403. + animalNames[(chineseYear + 1 - 1) % 12] + "年)");
  404. int ln = 2;
  405. String blank = " " + " "
  406. + " ";
  407. String[] mLeft = null;
  408. String[] mRight = null;
  409. for (int i = 1; i <= 6; i++) {
  410. table[ln] = blank;
  411. ln++;
  412. mLeft = getMonthTable();
  413. mRight = getMonthTable();
  414. for (int j = 0; j < mLeft.length; j++) {
  415. String line = mLeft[j] + " " + mRight[j];
  416. table[ln] = line;
  417. ln++;
  418. }
  419. }
  420. table[ln] = blank;
  421. ln++;
  422. table[ln] = getTextLine(0, "##/## - 公历日期/农历日期,(*)#月 - (闰)农历月第一天");
  423. ln++;
  424. return table;
  425. }
  426. public static String getTextLine(int s, String t) {
  427. String str = " " + " "
  428. + " ";
  429. if (t != null && s < str.length() && s + t.length() < str.length())
  430. str = str.substring(0, s) + t + str.substring(s + t.length());
  431. return str;
  432. }
  433. private static String[] monthNames = { "一", "二", "三", "四", "五", "六", "七",
  434. "八", "九", "十", "十一", "十二" };
  435. public String[] getMonthTable() {
  436. setGregorian(gregorianYear, gregorianMonth, 1);
  437. computeChineseFields();
  438. computeSolarTerms();
  439. String[] table = new String[8];
  440. String title = null;
  441. if (gregorianMonth < 11)
  442. title = " ";
  443. else
  444. title = " ";
  445. title = title + monthNames[gregorianMonth - 1] + "月"
  446. + " ";
  447. String header = " 日 一 二 三 四 五 六 ";
  448. String blank = " ";
  449. table[0] = title;
  450. table[1] = header;
  451. int wk = 2;
  452. String line = "";
  453. for (int i = 1; i < dayOfWeek; i++) {
  454. line += " " + ' ';
  455. }
  456. int days = daysInGregorianMonth(gregorianYear, gregorianMonth);
  457. for (int i = gregorianDate; i <= days; i++) {
  458. line += getDateString() + ' ';
  459. rollUpOneDay();
  460. if (dayOfWeek == 1) {
  461. table[wk] = line;
  462. line = "";
  463. wk++;
  464. }
  465. }
  466. for (int i = dayOfWeek; i <= 7; i++) {
  467. line += " " + ' ';
  468. }
  469. table[wk] = line;
  470. for (int i = wk + 1; i < table.length; i++) {
  471. table[i] = blank;
  472. }
  473. for (int i = 0; i < table.length; i++) {
  474. table[i] = table[i].substring(0, table[i].length() - 1);
  475. }
  476. return table;
  477. }
  478. private static String[] chineseMonthNames = { "正", "二", "三", "四", "五", "六",
  479. "七", "八", "九", "十", "冬", "腊" };
  480. private static String[] principleTermNames = { "雨水", "春分", "谷雨", "夏满",
  481. "夏至", "大暑", "处暑", "秋分", "霜降", "小雪", "冬至", "大寒" };
  482. private static String[] sectionalTermNames = { "立春", "惊蛰", "清明", "立夏",
  483. "芒种", "小暑", "立秋", "白露", "寒露", "立冬", "大雪", "小寒" };
  484. public String getDateString() {
  485. String str = "* / ";
  486. String gm = String.valueOf(gregorianMonth);
  487. if (gm.length() == 1)
  488. gm = ' ' + gm;
  489. String cm = String.valueOf(Math.abs(chineseMonth));
  490. if (cm.length() == 1)
  491. cm = ' ' + cm;
  492. String gd = String.valueOf(gregorianDate);
  493. if (gd.length() == 1)
  494. gd = ' ' + gd;
  495. String cd = String.valueOf(chineseDate);
  496. if (cd.length() == 1)
  497. cd = ' ' + cd;
  498. if (gregorianDate == sectionalTerm) {
  499. str = " " + sectionalTermNames[gregorianMonth - 1];
  500. } else if (gregorianDate == principleTerm) {
  501. str = " " + principleTermNames[gregorianMonth - 1];
  502. } else if (chineseDate == 1 && chineseMonth > 0) {
  503. str = " " + chineseMonthNames[chineseMonth - 1] + "月";
  504. } else if (chineseDate == 1 && chineseMonth < 0) {
  505. str = "*" + chineseMonthNames[-chineseMonth - 1] + "月";
  506. } else {
  507. str = gd + '/' + cd;
  508. }
  509. return str;
  510. }
  511. public int rollUpOneDay() {
  512. dayOfWeek = dayOfWeek % 7 + 1;
  513. dayOfYear++;
  514. gregorianDate++;
  515. int days = daysInGregorianMonth(gregorianYear, gregorianMonth);
  516. if (gregorianDate > days) {
  517. gregorianDate = 1;
  518. gregorianMonth++;
  519. if (gregorianMonth > 12) {
  520. gregorianMonth = 1;
  521. gregorianYear++;
  522. dayOfYear = 1;
  523. isGregorianLeap = isGregorianLeapYear(gregorianYear);
  524. }
  525. sectionalTerm = sectionalTerm(gregorianYear, gregorianMonth);
  526. principleTerm = principleTerm(gregorianYear, gregorianMonth);
  527. }
  528. chineseDate++;
  529. days = daysInChineseMonth(chineseYear, chineseMonth);
  530. if (chineseDate > days) {
  531. chineseDate = 1;
  532. chineseMonth = nextChineseMonth(chineseYear, chineseMonth);
  533. if (chineseMonth == 1)
  534. chineseYear++;
  535. }
  536. return 0;
  537. }
  538. public int getGregorianYear() {
  539. return gregorianYear;
  540. }
  541. public void setGregorianYear(int gregorianYear) {
  542. this.gregorianYear = gregorianYear;
  543. }
  544. public int getGregorianMonth() {
  545. return gregorianMonth;
  546. }
  547. public void setGregorianMonth(int gregorianMonth) {
  548. this.gregorianMonth = gregorianMonth;
  549. }
  550. public int getGregorianDate() {
  551. return gregorianDate;
  552. }
  553. public void setGregorianDate(int gregorianDate) {
  554. this.gregorianDate = gregorianDate;
  555. }
  556. public boolean isGregorianLeap() {
  557. return isGregorianLeap;
  558. }
  559. public void setGregorianLeap(boolean isGregorianLeap) {
  560. this.isGregorianLeap = isGregorianLeap;
  561. }
  562. public int getDayOfYear() {
  563. return dayOfYear;
  564. }
  565. public void setDayOfYear(int dayOfYear) {
  566. this.dayOfYear = dayOfYear;
  567. }
  568. public int getDayOfWeek() {
  569. return dayOfWeek;
  570. }
  571. public void setDayOfWeek(int dayOfWeek) {
  572. this.dayOfWeek = dayOfWeek;
  573. }
  574. public int getChineseYear() {
  575. return chineseYear;
  576. }
  577. public void setChineseYear(int chineseYear) {
  578. this.chineseYear = chineseYear;
  579. }
  580. public int getChineseMonth() {
  581. return chineseMonth;
  582. }
  583. public void setChineseMonth(int chineseMonth) {
  584. this.chineseMonth = chineseMonth;
  585. }
  586. public int getChineseDate() {
  587. return chineseDate;
  588. }
  589. public void setChineseDate(int chineseDate) {
  590. this.chineseDate = chineseDate;
  591. }
  592. public int getSectionalTerm() {
  593. return sectionalTerm;
  594. }
  595. public void setSectionalTerm(int sectionalTerm) {
  596. this.sectionalTerm = sectionalTerm;
  597. }
  598. public int getPrincipleTerm() {
  599. return principleTerm;
  600. }
  601. public void setPrincipleTerm(int principleTerm) {
  602. this.principleTerm = principleTerm;
  603. }
  604. public static char[] getDaysInGregorianMonth() {
  605. return daysInGregorianMonth;
  606. }
  607. public static void setDaysInGregorianMonth(char[] daysInGregorianMonth) {
  608. CalendarUtil.daysInGregorianMonth = daysInGregorianMonth;
  609. }
  610. public static String[] getStemNames() {
  611. return stemNames;
  612. }
  613. public static void setStemNames(String[] stemNames) {
  614. CalendarUtil.stemNames = stemNames;
  615. }
  616. public static String[] getBranchNames() {
  617. return branchNames;
  618. }
  619. public static void setBranchNames(String[] branchNames) {
  620. CalendarUtil.branchNames = branchNames;
  621. }
  622. public static String[] getAnimalNames() {
  623. return animalNames;
  624. }
  625. public static void setAnimalNames(String[] animalNames) {
  626. CalendarUtil.animalNames = animalNames;
  627. }
  628. public static char[] getChineseMonths() {
  629. return chineseMonths;
  630. }
  631. public static void setChineseMonths(char[] chineseMonths) {
  632. CalendarUtil.chineseMonths = chineseMonths;
  633. }
  634. public static int getBaseYear() {
  635. return baseYear;
  636. }
  637. public static void setBaseYear(int baseYear) {
  638. CalendarUtil.baseYear = baseYear;
  639. }
  640. public static int getBaseMonth() {
  641. return baseMonth;
  642. }
  643. public static void setBaseMonth(int baseMonth) {
  644. CalendarUtil.baseMonth = baseMonth;
  645. }
  646. public static int getBaseDate() {
  647. return baseDate;
  648. }
  649. public static void setBaseDate(int baseDate) {
  650. CalendarUtil.baseDate = baseDate;
  651. }
  652. public static int getBaseIndex() {
  653. return baseIndex;
  654. }
  655. public static void setBaseIndex(int baseIndex) {
  656. CalendarUtil.baseIndex = baseIndex;
  657. }
  658. public static int getBaseChineseYear() {
  659. return baseChineseYear;
  660. }
  661. public static void setBaseChineseYear(int baseChineseYear) {
  662. CalendarUtil.baseChineseYear = baseChineseYear;
  663. }
  664. public static int getBaseChineseMonth() {
  665. return baseChineseMonth;
  666. }
  667. public static void setBaseChineseMonth(int baseChineseMonth) {
  668. CalendarUtil.baseChineseMonth = baseChineseMonth;
  669. }
  670. public static int getBaseChineseDate() {
  671. return baseChineseDate;
  672. }
  673. public static void setBaseChineseDate(int baseChineseDate) {
  674. CalendarUtil.baseChineseDate = baseChineseDate;
  675. }
  676. public static int[] getBigLeapMonthYears() {
  677. return bigLeapMonthYears;
  678. }
  679. public static void setBigLeapMonthYears(int[] bigLeapMonthYears) {
  680. CalendarUtil.bigLeapMonthYears = bigLeapMonthYears;
  681. }
  682. public static char[][] getSectionalTermMap() {
  683. return sectionalTermMap;
  684. }
  685. public static void setSectionalTermMap(char[][] sectionalTermMap) {
  686. CalendarUtil.sectionalTermMap = sectionalTermMap;
  687. }
  688. public static char[][] getSectionalTermYear() {
  689. return sectionalTermYear;
  690. }
  691. public static void setSectionalTermYear(char[][] sectionalTermYear) {
  692. CalendarUtil.sectionalTermYear = sectionalTermYear;
  693. }
  694. public static char[][] getPrincipleTermMap() {
  695. return principleTermMap;
  696. }
  697. public static void setPrincipleTermMap(char[][] principleTermMap) {
  698. CalendarUtil.principleTermMap = principleTermMap;
  699. }
  700. public static char[][] getPrincipleTermYear() {
  701. return principleTermYear;
  702. }
  703. public static void setPrincipleTermYear(char[][] principleTermYear) {
  704. CalendarUtil.principleTermYear = principleTermYear;
  705. }
  706. public static String[] getMonthNames() {
  707. return monthNames;
  708. }
  709. public static void setMonthNames(String[] monthNames) {
  710. CalendarUtil.monthNames = monthNames;
  711. }
  712. public static String[] getChineseMonthNames() {
  713. return chineseMonthNames;
  714. }
  715. public static void setChineseMonthNames(String[] chineseMonthNames) {
  716. CalendarUtil.chineseMonthNames = chineseMonthNames;
  717. }
  718. public static String[] getPrincipleTermNames() {
  719. return principleTermNames;
  720. }
  721. public static void setPrincipleTermNames(String[] principleTermNames) {
  722. CalendarUtil.principleTermNames = principleTermNames;
  723. }
  724. public static String[] getSectionalTermNames() {
  725. return sectionalTermNames;
  726. }
  727. public static void setSectionalTermNames(String[] sectionalTermNames) {
  728. CalendarUtil.sectionalTermNames = sectionalTermNames;
  729. }
  730. public static void main(String[] arg) {
  731. CalendarUtil c = new CalendarUtil();
  732. String cmd = "day";
  733. int y = 2010;
  734. int m = 8;
  735. int d = 11;
  736. c.setGregorian(y, m, d);
  737. c.computeChineseFields();
  738. c.computeSolarTerms();
  739. if (cmd.equalsIgnoreCase("year")) {
  740. String[] t = c.getYearTable();
  741. for (int i = 0; i < t.length; i++)
  742. System.out.println(t[i]);
  743. } else if (cmd.equalsIgnoreCase("month")) {
  744. String[] t = c.getMonthTable();
  745. for (int i = 0; i < t.length; i++)
  746. System.out.println(t[i]);
  747. } else {
  748. System.out.println(c.toString());
  749. }
  750. System.out.println(c.getDateString());
  751. }
  752. }

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

闽ICP备14008679号