点击打开链接

前些天在网上找阳历转农历的源码找了好久都找不到,但是幸运的是找到了李宁老师《Android/OPhone开发完全讲义》日历的源码,但是可惜的是源码中农历的部分没有了。。

于是在网上找了个Java类的阳历转农历的类Lunar.java。下面开始实现这功能。

Lunar.java类:

[java]  view plain copy
  1. [html] view plaincopyprint?  
  2.   
  3.    1public class Lunar {    
  4.    2private int year;    
  5.    3private int month;    
  6.    4private int day;    
  7.    5private boolean leap;    
  8.    6final static String chineseNumber[] = {"一""二""三""四""五","六""七""八""九""十""十一""十二"};    
  9.    7static SimpleDateFormat chineseDateFormat = newSimpleDateFormat("yyyy年MM月dd日");    
  10.    8final static long[] lunarInfo = new long[]    
  11.    9. {0x04bd80x04ae00x0a5700x054d50x0d2600x0d9500x16554,0x056a00x09ad00x055d2,    
  12.   100x04ae00x0a5b60x0a4d00x0d2500x1d2550x0b5400x0d6a0,0x0ada20x095b00x14977,    
  13.   110x049700x0a4b00x0b4b50x06a500x06d400x1ab540x02b60,0x095700x052f20x04970,    
  14.   120x065660x0d4a00x0ea500x06e950x05ad00x02b600x186e3,0x092e00x1c8d70x0c950,    
  15.   130x0d4a00x1d8a60x0b5500x056a00x1a5b40x025d00x092d0,0x0d2b20x0a9500x0b557,    
  16.   140x06ca00x0b5500x153550x04da00x0a5d00x145730x052d0,0x0a9a80x0e9500x06aa0,    
  17.   150x0aea60x0ab500x04b600x0aae40x0a5700x052600x0f263,0x0d9500x05b570x056a0,    
  18.   160x096d00x04dd50x04ad00x0a4d00x0d4d40x0d2500x0d558,0x0b5400x0b5a00x195a6,    
  19.   170x095b00x049b00x0a9740x0a4b00x0b27a0x06a500x06d40,0x0af460x0ab600x09570,    
  20.   180x04af50x049700x064b00x074a30x0ea500x06b580x055c0,0x0ab600x096d50x092e0,    
  21.   190x0c9600x0d9540x0d4a00x0da500x075520x056a00x0abb7,0x025d00x092d00x0cab5,    
  22.   200x0a9500x0b4a00x0baa40x0ad500x055d90x04ba00x0a5b0,0x151760x052b00x0a930,    
  23.   210x079540x06aa00x0ad500x05b520x04b600x0a6e60x0a4e0,0x0d2600x0ea650x0d530,    
  24.   220x05aa00x076a30x096d00x04bd70x04ad00x0a4d00x1d0b6,0x0d2500x0d5200x0dd45,    
  25.   230x0b5a00x056d00x055b20x049b00x0a5770x0a4b00x0aa50,0x1b2550x06d200x0ada0};    
  26.   24.     
  27.   25//====== 传回农历 y年的总天数    
  28.   26final private static int yearDays(int y) {    
  29.   27int i, sum = 348;    
  30.   28for (i = 0x8000; i > 0x8; i>>= 1) {    
  31.   29if ((lunarInfo[y - 1900] & i) != 0) sum += 1;    
  32.   30. }    
  33.   31return (sum + leapDays(y));    
  34.   32. }    
  35.   33.     
  36.   34//====== 传回农历 y年闰月的天数    
  37.   35final private static int leapDays(int y) {    
  38.   36if (leapMonth(y) != 0) {    
  39.   37if ((lunarInfo[y - 1900] & 0x10000) != 0)    
  40.   38return 30;    
  41.   39else    
  42.   40return 29;    
  43.   41. } else    
  44.   42return 0;    
  45.   43. }    
  46.   44.     
  47.   45//====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0    
  48.   46final private static int leapMonth(int y) {    
  49.   47return (int) (lunarInfo[y - 1900] & 0xf);    
  50.   48. }    
  51.   49.     
  52.   50//====== 传回农历 y年m月的总天数    
  53.   51final private static int monthDays(int y, int m) {    
  54.   52if ((lunarInfo[y - 1900] & (0x10000>> m)) == 0)    
  55.   53return 29;    
  56.   54else    
  57.   55return 30;    
  58.   56. }    
  59.   57.     
  60.   58//====== 传回农历 y年的生肖    
  61.   59final public String animalsYear() {    
  62.   60final String[] Animals = new String[]{"鼠""牛""虎""兔""龙""蛇","马""羊""猴""鸡""狗""猪"};    
  63.   61return Animals[(year - 4) % 12];    
  64.   62. }    
  65.   63.     
  66.   64//====== 传入 月日的offset 传回干支, 0=甲子    
  67.   65final private static String cyclicalm(int num) {    
  68.   66final String[] Gan = new String[]{"甲""乙""丙""丁""戊""己","庚""辛""壬""癸"};    
  69.   67final String[] Zhi = new String[]{"子""丑""寅""卯""辰""巳","午""未""申""酉""戌""亥"};    
  70.   68return (Gan[num % 10] + Zhi[num % 12]);    
  71.   69. }    
  72.   70.     
  73.   71//====== 传入 offset 传回干支, 0=甲子    
  74.   72final public String cyclical() {    
  75.   73int num = year - 1900 + 36;    
  76.   74return (cyclicalm(num));    
  77.   75. }    
  78.   76public String getLunarMonthString() {    
  79.   77// TODO Auto-generated method stub    
  80.   78return null;    
  81.   79. }    
  82.   80.     
  83.   81public Lunar(Calendar cal) {    
  84.   82@SuppressWarnings("unused"int yearCyl, monCyl, dayCyl;    
  85.   83int leapMonth = 0;    
  86.   84. Date baseDate = null;    
  87.   85try {    
  88.   86. baseDate = chineseDateFormat.parse("1900年1月31日");    
  89.   87. } catch (ParseException e) {    
  90.   88. e.printStackTrace(); //To change body of catch statement useOptions | File Templates.    
  91.   89. }    
  92.   90.     
  93.   91//求出和1900年1月31日相差的天数    
  94.   92int offset = (int) ((cal.getTime().getTime() - baseDate.getTime())/ 86400000L);    
  95.   93. dayCyl = offset + 40;    
  96.   94. monCyl = 14;    
  97.   95.     
  98.   96//用offset减去每农历年的天数    
  99.   97// 计算当天是农历第几天    
  100.   98//i最终结果是农历的年份    
  101.   99//offset是当年的第几天    
  102.  100int iYear, daysOfYear = 0;    
  103.  101for (iYear = 1900; iYear < 2050&& offset > 0;iYear++) {    
  104.  102. daysOfYear = yearDays(iYear);    
  105.  103. offset -= daysOfYear;    
  106.  104. monCyl += 12;    
  107.  105. }    
  108.  106if (offset < 0) {    
  109.  107. offset += daysOfYear;    
  110.  108. iYear--;    
  111.  109. monCyl -= 12;    
  112.  110. }    
  113.  111//农历年份    
  114.  112. year = iYear;    
  115.  113.     
  116.  114. yearCyl = iYear - 1864;    
  117.  115. leapMonth = leapMonth(iYear); //闰哪个月,1-12    
  118.  116. leap = false;    
  119.  117.     
  120.  118//用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天    
  121.  119int iMonth, daysOfMonth = 0;    
  122.  120for (iMonth = 1; iMonth < 13&& offset > 0;iMonth++) {    
  123.  121//闰月    
  124.  122if (leapMonth > 0 &&iMonth == (leapMonth + 1) && !leap){    
  125.  123. --iMonth;    
  126.  124. leap = true;    
  127.  125. daysOfMonth = leapDays(year);    
  128.  126. } else    
  129.  127. daysOfMonth = monthDays(year, iMonth);    
  130.  128.     
  131.  129. offset -= daysOfMonth;    
  132.  130//解除闰月    
  133.  131if (leap && iMonth == (leapMonth +1)) leap = false;    
  134.  132if (!leap) monCyl++;    
  135.  133. }    
  136.  134//offset为0时,并且刚才计算的月份是闰月,要校正    
  137.  135if (offset == 0 && leapMonth> 0 && iMonth ==leapMonth + 1) {    
  138.  136if (leap) {    
  139.  137. leap = false;    
  140.  138. } else {    
  141.  139. leap = true;    
  142.  140. --iMonth;    
  143.  141. --monCyl;    
  144.  142. }    
  145.  143. }    
  146.  144//offset小于0时,也要校正    
  147.  145if (offset < 0) {    
  148.  146. offset += daysOfMonth;    
  149.  147. --iMonth;    
  150.  148. --monCyl;    
  151.  149. }    
  152.  150. month = iMonth;    
  153.  151. day = offset + 1;    
  154.  152. }    
  155.  153.     
  156.  154public static String getChinaDayString(int day) {    
  157.  155. String chineseTen[] = {"初""十""廿""三"};    
  158.  156int n = day % 10 == 0 ? 9 : day % 10 - 1;    
  159.  157if (day > 30)    
  160.  158return "";    
  161.  159if (day == 10)    
  162.  160return "初十";    
  163.  161else    
  164.  162return chineseTen[day / 10] + chineseNumber[n];    
  165.  163. }    
  166.  164.     
  167.  165public String toString() {    
  168.  166return year + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] +"月" + getChinaDayString(day);    
  169.  167. }    
  170.  168. }    

[java]  view plain copy
  1. 1)打印一个农历信息在TextView上  
  2.   
  3. 在Grid类的updateMsg方法添加以下代码:  
  4. [html] view plaincopyprint?  
  5.   
  6.    1. Lunar lunar = new Lunar(calendar);    
  7.    2.     
  8.    3. String lunarStr = "";    
  9.    4. lunarStr=lunar.animalsYear()+"年(";    
  10.    5. lunarStr +=lunar.cyclical()+"年)";    
  11.    6. lunarStr +=lunar.toString();    
  12.    7.     
  13.    8. tvMsg3.setText(lunarStr);    
  14.   
  15. Lunar lunar = new Lunar(calendar); String lunarStr = ""; lunarStr=lunar.animalsYear()+"年("; lunarStr +=lunar.cyclical()+"年)"; lunarStr +=lunar.toString(); tvMsg3.setText(lunarStr);  
  16. 这样就能实现了,很简单吧!   


(2)全部日期都显示农历信息,效果图如下:


[java]  view plain copy
  1. 同样在Grid类里的draw(Cavas cavas)方法中  
  2.   
  3. for (int i = 0; i < days.length;i++)循环前面添加以下代码:  
  4.   
  5. [html] view plaincopyprint?  
  6.   
  7.    1. calendar.set(currentYear, currentMonth, currentDay);    
  8.    2. SimpleDateFormat abc = new SimpleDateFormat("yyyy年MM月dd日");    
  9.   
  10. calendar.set(currentYear, currentMonth, currentDay); SimpleDateFormat abc = new SimpleDateFormat("yyyy年MM月dd日");在for (int i = 0; i < days.length;i++)循环里添加以下代码:  
  11.   
  12. [html] view plaincopyprint?  
  13.   
  14.    1if (days[i].startsWith("*"))    
  15.    2. {    
  16.    3// 下月    
  17.    4if (i > 20)    
  18.    5. {    
  19.    6. String nowday = currentYear + "年" + (currentMonth+2) + "月" + myText+ "日";    
  20.    7. java.util.Calendar currentDay =java.util.Calendar.getInstance();    
  21.    8try {    
  22.    9//定义传入ChineseCalendar参数的格式    
  23.   10. currentDay.setTime(abc.parse(nowday));    
  24.   11// System.out.println("currentDay:"+currentDay);    
  25.   12. } catch (ParseException e) {    
  26.   13. e.printStackTrace();    
  27.   14. }    
  28.   15. Lunar lunar = new Lunar(currentDay);    
  29.   16. String currentDate = lunar.toString();    
  30.   17int startIndex = currentDate.length()-2;    
  31.   18//获取农历月份    
  32.   19. String showMonth = currentDate.substring(startIndex-2,startIndex);    
  33.   20//获取农历的日    
  34.   21. String showDay = currentDate.substring(startIndex);    
  35.   22//当农历的日为“初一时”,画出该天的月份    
  36.   23//否则画出该天农历的日    
  37.   24if (showDay.equals("初一")) {    
  38.   25. canvas.drawText(showMonth, textLeft1, textTop1, paint);    
  39.   26. } else {    
  40.   27. canvas.drawText(showDay, textLeft1, textTop1, paint);    
  41.   28. }    
  42.   29. }    
  43.   30// 上月    
  44.   31else    
  45.   32. {    
  46.   33. String nowday = currentYear + "年" + (currentMonth) + "月" + myText +"日";    
  47.   34. java.util.Calendar currentDay =java.util.Calendar.getInstance();    
  48.   35try {    
  49.   36//定义传入ChineseCalendar参数的格式    
  50.   37. currentDay.setTime(abc.parse(nowday));    
  51.   38// System.out.println("currentDay:"+currentDay);    
  52.   39. } catch (ParseException e) {    
  53.   40. e.printStackTrace();    
  54.   41. }    
  55.   42. Lunar lunar = new Lunar(currentDay);    
  56.   43. String currentDate = lunar.toString();    
  57.   44int startIndex = currentDate.length()-2;    
  58.   45//获取农历月份    
  59.   46. String showMonth = currentDate.substring(startIndex-2,startIndex);    
  60.   47//获取农历的日    
  61.   48. String showDay = currentDate.substring(startIndex);    
  62.   49//当农历的日为“初一时”,画出该天的月份    
  63.   50//否则画出该天农历的日    
  64.   51if (showDay.equals("初一")) {    
  65.   52. canvas.drawText(showMonth, textLeft1, textTop1, paint);    
  66.   53. } else {    
  67.   54. canvas.drawText(showDay, textLeft1, textTop1, paint);    
  68.   55. }    
  69.   56. }    
  70.   57. }else{    
  71.   58. String nowday = currentYear + "年" + (currentMonth+1) + "月" + myText+ "日";    
  72.   59. java.util.Calendar currentDay =java.util.Calendar.getInstance();    
  73.   60try {    
  74.   61//定义传入ChineseCalendar参数的格式    
  75.   62. currentDay.setTime(abc.parse(nowday));    
  76.   63// System.out.println("currentDay:"+currentDay);    
  77.   64. } catch (ParseException e) {    
  78.   65. e.printStackTrace();    
  79.   66. }    
  80.   67. Lunar lunar = new Lunar(currentDay);    
  81.   68. String currentDate = lunar.toString();    
  82.   69int startIndex = currentDate.length()-2;    
  83.   70//获取农历月份    
  84.   71. String showMonth = currentDate.substring(startIndex-2,startIndex);    
  85.   72//获取农历的日    
  86.   73. String showDay = currentDate.substring(startIndex);    
  87.   74//当农历的日为“初一时”,画出该天的月份    
  88.   75//否则画出该天农历的日    
  89.   76if (showDay.equals("初一")) {    
  90.   77. canvas.drawText(showMonth, textLeft1, textTop1, paint);    
  91.   78. } else {    
  92.   79. canvas.drawText(showDay, textLeft1, textTop1, paint);    
  93.   80. }    
  94.   81. }    
  95.   82. canvas.drawText(myText, textLeft, textTop, paint);    

更多相关文章

  1. Android中带签到功能的日历(积分)
  2. Android处理时间各种方法汇总
  3. jdk1.8签名apk
  4. [Android] ICS | JB 锁屏农历的添加
  5. android 日历 农历算法
  6. Android 农历和节气相关工具类(记录)
  7. Android Studio 自定义日历的简单实现
  8. Android 计算出生日期至今天数(获取宝宝出生到现在天数)
  9. Android获取两个日期之间间隔的天数

随机推荐

  1. android webview 中处理网页中的400、404
  2. Android Fresco的使用
  3. Android图片解决方案
  4. Android的字符编码转换问题,Unicode,GB2312
  5. android 项目收获01
  6. android中dialog工具类的实现(多种dialog
  7. android根据包名获取签名MD5信息
  8. android HTTP post方法时,如何使用cookies
  9. Android中如何使用对话框(单选对话框和多
  10. Android 如何让Dialog实现背景透明