前些天在网上找阳历转农历的源码找了好久都找不到,但是幸运的是找到了李宁老师《Android/OPhone开发完全讲义》日历的源码,但是可惜的是源码中农历的部分没有了。。
于是在网上找了个Java类的阳历转农历的类Lunar.java。下面开始实现这功能。
Lunar.java类:[html] view plaincopyprint? 1. public class Lunar { 2. private int year; 3. private int month; 4. private int day; 5. private boolean leap; 6. final static String chineseNumber[] = {"一", "二", "三", "四", "五","六", "七", "八", "九", "十", "十一", "十二"}; 7. static SimpleDateFormat chineseDateFormat = newSimpleDateFormat("yyyy年MM月dd日"); 8. final static long[] lunarInfo = new long[] 9. {0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554,0x056a0, 0x09ad0, 0x055d2, 10. 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0,0x0ada2, 0x095b0, 0x14977, 11. 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60,0x09570, 0x052f2, 0x04970, 12. 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3,0x092e0, 0x1c8d7, 0x0c950, 13. 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0,0x0d2b2, 0x0a950, 0x0b557, 14. 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0,0x0a9a8, 0x0e950, 0x06aa0, 15. 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263,0x0d950, 0x05b57, 0x056a0, 16. 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558,0x0b540, 0x0b5a0, 0x195a6, 17. 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40,0x0af46, 0x0ab60, 0x09570, 18. 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0,0x0ab60, 0x096d5, 0x092e0, 19. 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7,0x025d0, 0x092d0, 0x0cab5, 20. 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0,0x15176, 0x052b0, 0x0a930, 21. 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0,0x0d260, 0x0ea65, 0x0d530, 22. 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6,0x0d250, 0x0d520, 0x0dd45, 23. 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50,0x1b255, 0x06d20, 0x0ada0}; 24. 25. //====== 传回农历 y年的总天数 26. final private static int yearDays(int y) { 27. int i, sum = 348; 28. for (i = 0x8000; i > 0x8; i>>= 1) { 29. if ((lunarInfo[y - 1900] & i) != 0) sum += 1; 30. } 31. return (sum + leapDays(y)); 32. } 33. 34. //====== 传回农历 y年闰月的天数 35. final private static int leapDays(int y) { 36. if (leapMonth(y) != 0) { 37. if ((lunarInfo[y - 1900] & 0x10000) != 0) 38. return 30; 39. else 40. return 29; 41. } else 42. return 0; 43. } 44. 45. //====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0 46. final private static int leapMonth(int y) { 47. return (int) (lunarInfo[y - 1900] & 0xf); 48. } 49. 50. //====== 传回农历 y年m月的总天数 51. final private static int monthDays(int y, int m) { 52. if ((lunarInfo[y - 1900] & (0x10000>> m)) == 0) 53. return 29; 54. else 55. return 30; 56. } 57. 58. //====== 传回农历 y年的生肖 59. final public String animalsYear() { 60. final String[] Animals = new String[]{"鼠", "牛", "虎", "兔", "龙", "蛇","马", "羊", "猴", "鸡", "狗", "猪"}; 61. return Animals[(year - 4) % 12]; 62. } 63. 64. //====== 传入 月日的offset 传回干支, 0=甲子 65. final private static String cyclicalm(int num) { 66. final String[] Gan = new String[]{"甲", "乙", "丙", "丁", "戊", "己","庚", "辛", "壬", "癸"}; 67. final String[] Zhi = new String[]{"子", "丑", "寅", "卯", "辰", "巳","午", "未", "申", "酉", "戌", "亥"}; 68. return (Gan[num % 10] + Zhi[num % 12]); 69. } 70. 71. //====== 传入 offset 传回干支, 0=甲子 72. final public String cyclical() { 73. int num = year - 1900 + 36; 74. return (cyclicalm(num)); 75. } 76. public String getLunarMonthString() { 77. // TODO Auto-generated method stub 78. return null; 79. } 80. 81. public Lunar(Calendar cal) { 82. @SuppressWarnings("unused") int yearCyl, monCyl, dayCyl; 83. int leapMonth = 0; 84. Date baseDate = null; 85. try { 86. baseDate = chineseDateFormat.parse("1900年1月31日"); 87. } catch (ParseException e) { 88. e.printStackTrace(); //To change body of catch statement useOptions | File Templates. 89. } 90. 91. //求出和1900年1月31日相差的天数 92. int offset = (int) ((cal.getTime().getTime() - baseDate.getTime())/ 86400000L); 93. dayCyl = offset + 40; 94. monCyl = 14; 95. 96. //用offset减去每农历年的天数 97. // 计算当天是农历第几天 98. //i最终结果是农历的年份 99. //offset是当年的第几天 100. int iYear, daysOfYear = 0; 101. for (iYear = 1900; iYear < 2050&& offset > 0;iYear++) { 102. daysOfYear = yearDays(iYear); 103. offset -= daysOfYear; 104. monCyl += 12; 105. } 106. if (offset < 0) { 107. offset += daysOfYear; 108. iYear--; 109. monCyl -= 12; 110. } 111. //农历年份 112. year = iYear; 113. 114. yearCyl = iYear - 1864; 115. leapMonth = leapMonth(iYear); //闰哪个月,1-12 116. leap = false; 117. 118. //用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天 119. int iMonth, daysOfMonth = 0; 120. for (iMonth = 1; iMonth < 13&& offset > 0;iMonth++) { 121. //闰月 122. if (leapMonth > 0 &&iMonth == (leapMonth + 1) && !leap){ 123. --iMonth; 124. leap = true; 125. daysOfMonth = leapDays(year); 126. } else 127. daysOfMonth = monthDays(year, iMonth); 128. 129. offset -= daysOfMonth; 130. //解除闰月 131. if (leap && iMonth == (leapMonth +1)) leap = false; 132. if (!leap) monCyl++; 133. } 134. //offset为0时,并且刚才计算的月份是闰月,要校正 135. if (offset == 0 && leapMonth> 0 && iMonth ==leapMonth + 1) { 136. if (leap) { 137. leap = false; 138. } else { 139. leap = true; 140. --iMonth; 141. --monCyl; 142. } 143. } 144. //offset小于0时,也要校正 145. if (offset < 0) { 146. offset += daysOfMonth; 147. --iMonth; 148. --monCyl; 149. } 150. month = iMonth; 151. day = offset + 1; 152. } 153. 154. public static String getChinaDayString(int day) { 155. String chineseTen[] = {"初", "十", "廿", "三"}; 156. int n = day % 10 == 0 ? 9 : day % 10 - 1; 157. if (day > 30) 158. return ""; 159. if (day == 10) 160. return "初十"; 161. else 162. return chineseTen[day / 10] + chineseNumber[n]; 163. } 164. 165. public String toString() { 166. return year + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] +"月" + getChinaDayString(day); 167. } 168. }
(1)打印一个农历信息在TextView上 在Grid类的updateMsg方法添加以下代码: [html] view plaincopyprint? 1. Lunar lunar = new Lunar(calendar); 2. 3. String lunarStr = ""; 4. lunarStr=lunar.animalsYear()+"年("; 5. lunarStr +=lunar.cyclical()+"年)"; 6. lunarStr +=lunar.toString(); 7. 8. tvMsg3.setText(lunarStr); Lunar lunar = new Lunar(calendar); String lunarStr = ""; lunarStr=lunar.animalsYear()+"年("; lunarStr +=lunar.cyclical()+"年)"; lunarStr +=lunar.toString(); tvMsg3.setText(lunarStr); 这样就能实现了,很简单吧!
(2)全部日期都显示农历信息,效果图如下:
同样在Grid类里的draw(Cavas cavas)方法中 在for (int i = 0; i < days.length;i++)循环前面添加以下代码: [html] view plaincopyprint? 1. calendar.set(currentYear, currentMonth, currentDay); 2. SimpleDateFormat abc = new SimpleDateFormat("yyyy年MM月dd日"); calendar.set(currentYear, currentMonth, currentDay); SimpleDateFormat abc = new SimpleDateFormat("yyyy年MM月dd日");在for (int i = 0; i < days.length;i++)循环里添加以下代码: [html] view plaincopyprint? 1. if (days[i].startsWith("*")) 2. { 3. // 下月 4. if (i > 20) 5. { 6. String nowday = currentYear + "年" + (currentMonth+2) + "月" + myText+ "日"; 7. java.util.Calendar currentDay =java.util.Calendar.getInstance(); 8. try { 9. //定义传入ChineseCalendar参数的格式 10. currentDay.setTime(abc.parse(nowday)); 11. // System.out.println("currentDay:"+currentDay); 12. } catch (ParseException e) { 13. e.printStackTrace(); 14. } 15. Lunar lunar = new Lunar(currentDay); 16. String currentDate = lunar.toString(); 17. int startIndex = currentDate.length()-2; 18. //获取农历月份 19. String showMonth = currentDate.substring(startIndex-2,startIndex); 20. //获取农历的日 21. String showDay = currentDate.substring(startIndex); 22. //当农历的日为“初一时”,画出该天的月份 23. //否则画出该天农历的日 24. if (showDay.equals("初一")) { 25. canvas.drawText(showMonth, textLeft1, textTop1, paint); 26. } else { 27. canvas.drawText(showDay, textLeft1, textTop1, paint); 28. } 29. } 30. // 上月 31. else 32. { 33. String nowday = currentYear + "年" + (currentMonth) + "月" + myText +"日"; 34. java.util.Calendar currentDay =java.util.Calendar.getInstance(); 35. try { 36. //定义传入ChineseCalendar参数的格式 37. currentDay.setTime(abc.parse(nowday)); 38. // System.out.println("currentDay:"+currentDay); 39. } catch (ParseException e) { 40. e.printStackTrace(); 41. } 42. Lunar lunar = new Lunar(currentDay); 43. String currentDate = lunar.toString(); 44. int startIndex = currentDate.length()-2; 45. //获取农历月份 46. String showMonth = currentDate.substring(startIndex-2,startIndex); 47. //获取农历的日 48. String showDay = currentDate.substring(startIndex); 49. //当农历的日为“初一时”,画出该天的月份 50. //否则画出该天农历的日 51. if (showDay.equals("初一")) { 52. canvas.drawText(showMonth, textLeft1, textTop1, paint); 53. } else { 54. canvas.drawText(showDay, textLeft1, textTop1, paint); 55. } 56. } 57. }else{ 58. String nowday = currentYear + "年" + (currentMonth+1) + "月" + myText+ "日"; 59. java.util.Calendar currentDay =java.util.Calendar.getInstance(); 60. try { 61. //定义传入ChineseCalendar参数的格式 62. currentDay.setTime(abc.parse(nowday)); 63. // System.out.println("currentDay:"+currentDay); 64. } catch (ParseException e) { 65. e.printStackTrace(); 66. } 67. Lunar lunar = new Lunar(currentDay); 68. String currentDate = lunar.toString(); 69. int startIndex = currentDate.length()-2; 70. //获取农历月份 71. String showMonth = currentDate.substring(startIndex-2,startIndex); 72. //获取农历的日 73. String showDay = currentDate.substring(startIndex); 74. //当农历的日为“初一时”,画出该天的月份 75. //否则画出该天农历的日 76. if (showDay.equals("初一")) { 77. canvas.drawText(showMonth, textLeft1, textTop1, paint); 78. } else { 79. canvas.drawText(showDay, textLeft1, textTop1, paint); 80. } 81. } 82. canvas.drawText(myText, textLeft, textTop, paint);