Android 農曆和節氣相關工具類(記錄)
阿新 • • 發佈:2019-06-08
中國農曆相關的工具類,可以獲取當前或者規定日期的農曆,節氣,生肖等。
LunarCalender.java
package com.zhou.ghost.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; /** * 中國農曆工具類- */ public class LunarCalender { private int year; // 農曆的年份 private int month; private int day; private String lunarMonth; // 農曆的月份 private boolean leap; public int leapMonth = 0; // 閏的是哪個月 final static String chineseNumber[] = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "臘" }; static SimpleDateFormat chineseDateFormat = new SimpleDateFormat( "yyyy年MM月dd日"); final static long[] lunarInfo = new long[] { 0x4bd8, 0x4ae0, 0xa570, 0x54d5, 0xd260, 0xd950, 0x5554, 0x56af, 0x9ad0, 0x55d2, 0x4ae0, 0xa5b6, 0xa4d0, 0xd250, 0xd255, 0xb54f, 0xd6a0, 0xada2, 0x95b0, 0x4977, 0x497f, 0xa4b0, 0xb4b5, 0x6a50, 0x6d40, 0xab54, 0x2b6f, 0x9570, 0x52f2, 0x4970, 0x6566, 0xd4a0, 0xea50, 0x6a95, 0x5adf, 0x2b60, 0x86e3, 0x92ef, 0xc8d7, 0xc95f, 0xd4a0, 0xd8a6, 0xb55f, 0x56a0, 0xa5b4, 0x25df, 0x92d0, 0xd2b2, 0xa950, 0xb557, 0x6ca0, 0xb550, 0x5355, 0x4daf, 0xa5b0, 0x4573, 0x52bf, 0xa9a8, 0xe950, 0x6aa0, 0xaea6, 0xab50, 0x4b60, 0xaae4, 0xa570, 0x5260, 0xf263, 0xd950, 0x5b57, 0x56a0, 0x96d0, 0x4dd5, 0x4ad0, 0xa4d0, 0xd4d4, 0xd250, 0xd558, 0xb540, 0xb6a0, 0x95a6, 0x95bf, 0x49b0, 0xa974, 0xa4b0, 0xb27a, 0x6a50, 0x6d40, 0xaf46, 0xab60, 0x9570, 0x4af5, 0x4970, 0x64b0, 0x74a3, 0xea50, 0x6b58, 0x5ac0, 0xab60, 0x96d5, 0x92e0, 0xc960, 0xd954, 0xd4a0, 0xda50, 0x7552, 0x56a0, 0xabb7, 0x25d0, 0x92d0, 0xcab5, 0xa950, 0xb4a0, 0xbaa4, 0xad50, 0x55d9, 0x4ba0, 0xa5b0, 0x5176, 0x52bf, 0xa930, 0x7954, 0x6aa0, 0xad50, 0x5b52, 0x4b60, 0xa6e6, 0xa4e0, 0xd260, 0xea65, 0xd530, 0x5aa0, 0x76a3, 0x96d0, 0x4afb, 0x4ad0, 0xa4d0, 0xd0b6, 0xd25f, 0xd520, 0xdd45, 0xb5a0, 0x56d0, 0x55b2, 0x49b0, 0xa577, 0xa4b0, 0xaa50, 0xb255, 0x6d2f, 0xada0, 0x4b63, 0x937f, 0x49f8, 0x4970, 0x64b0, 0x68a6, 0xea5f, 0x6b20, 0xa6c4, 0xaaef, 0x92e0, 0xd2e3, 0xc960, 0xd557, 0xd4a0, 0xda50, 0x5d55, 0x56a0, 0xa6d0, 0x55d4, 0x52d0, 0xa9b8, 0xa950, 0xb4a0, 0xb6a6, 0xad50, 0x55a0, 0xaba4, 0xa5b0, 0x52b0, 0xb273, 0x6930, 0x7337, 0x6aa0, 0xad50, 0x4b55, 0x4b6f, 0xa570, 0x54e4, 0xd260, 0xe968, 0xd520, 0xdaa0, 0x6aa6, 0x56df, 0x4ae0, 0xa9d4, 0xa4d0, 0xd150, 0xf252, 0xd520}; // 農曆部分假日 final static String[] lunarHoliday = new String[] { "0101 春節", "0115 元宵節", "0505 端午節", "0707 七夕情人節", "0715 中元節 孟蘭節", "0730 地藏節", "0802 灶君誕", "0815 中秋節", "0827 先師誕", "0909 重陽節", "1208 臘八節 釋迦如來成道日", "1223 小年", "0100 除夕" }; // 公曆部分節假日 final static String[] solarHoliday = new String[] { "0101 元旦", "0110 中國110宣傳日", "0214 情人", "0221 國際母語日", "0303 國際愛耳日", "0308 婦女節", "0312 植樹節", "0315 消費者權益日", "0322 世界水日", "0323 世界氣象日", "0401 愚人節", "0407 世界衛生日", "0501 勞動節", "0504 青年節", "0512 護士節", "0519 全國助殘日", "0531 世界無煙日", "0601 兒童節", "0626 國際禁毒日", "0701 建黨節", //1921 "0801 建軍節", //1933 //"0808 父親節", "0909 毛澤東逝世紀念", //1976 "0910 教師節", "0917 國際和平日", "0927 世界旅遊日", "0928 孔子誕辰", "1001 國慶節", "1006 老人節", "1007 國際住房日", "1014 世界標準日", "1024 聯合國日", "1112 孫中山誕辰紀念", "1210 世界人權日", "1220 澳門迴歸紀念", "1224 平安夜", "1225 聖誕節", "1226 毛澤東誕辰紀念" }; //24節氣 final static String[] sTermInfo = new String[]{ // 時節 氣候 "小寒","大寒", "立春","雨水", "驚蟄","春分", "清明","穀雨", "立夏","小滿", "芒種","夏至", "小暑","大暑", "立秋","處暑", "白露","秋分", "寒露","霜降", "立冬","小雪", "大雪","冬至" }; final static String[] constellations = new String[]{ "摩蠍座:12.22—01.19","水瓶座:01.20—02.18","雙魚座:02.19—03.20","白羊座:03.21—04.19", "金牛座:04.20—05.20","雙子座:05.21—06.20","巨蟹座:06.21—07.21","獅子座:07.22—08.22", "處女座:08.23—09.22","天秤座:09.23—10.22","天蠍座:10.23—11.21","射手座:11.22—12.21" }; final static String[] yi_string = new String[]{ "出行.上任.會友.上書.見工","除服.療病.出行.拆卸.入宅", "祈福.祭祀.結親.開市.交易","祭祀.修填.塗泥.餘事勿取", "交易.立券.會友.簽約.納畜","祈福.祭祀.求子.結婚.立約", "求醫.赴考.祭祀.餘事勿取","經營.交易.求官.納畜.動土", "祈福.入學.開市.求醫.成服","祭祀.求財.簽約.嫁娶.訂盟", "療病.結婚.交易.入倉.求職","祭祀.交易.收財.安葬" }; final static String[] ji_string = new String[]{ "動土.開倉.嫁娶.納采","求官.上任.開張.搬家.探病", "服藥.求醫.栽種.動土.遷移","移徙.入宅.嫁娶.開市.安葬", "種植.置業.賣田.掘井.造船","開市.交易.搬家.遠行", "動土.出行.移徙.開市.修造","登高.行船.安床.入宅", "詞訟.安門.移徙","開市.安床.安葬.入宅.破土", "安葬.動土.鍼灸","宴會.安床.出行.嫁娶.移徙" }; final static String[][] jcName = new String[][]{ {"建","除","滿","平","定","執","破","危","成","收","開","閉"}, {"閉","建","除","滿","平","定","執","破","危","成","收","開"}, {"開","閉","建","除","滿","平","定","執","破","危","成","收"}, {"收","開","閉","建","除","滿","平","定","執","破","危","成"}, {"成","收","開","閉","建","除","滿","平","定","執","破","危"}, {"危","成","收","開","閉","建","除","滿","平","定","執","破"}, {"破","危","成","收","開","閉","建","除","滿","平","定","執"}, {"執","破","危","成","收","開","閉","建","除","滿","平","定"}, {"定","執","破","危","成","收","開","閉","建","除","滿","平"}, {"平","定","執","破","危","成","收","開","閉","建","除","滿"}, {"滿","平","定","執","破","危","成","收","開","閉","建","除"}, {"除","滿","平","定","執","破","危","成","收","開","閉","建"} }; final static String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; final static String[] Zhi = new String[] { "子", "醜", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; // ====== 傳回農曆 y年的總天數 1900--2100 public int yearDays(int y) { int i, sum = 348; for (i = 0x8000; i > 0x8; i >>= 1) { if ((lunarInfo[y - 1900] & i) != 0) sum += 1; } return (sum + leapDays(y)); } // ====== 傳回農曆 y年閏月的天數 public int leapDays(int y) { if (leapMonth(y) != 0) { if ((lunarInfo[y - 1899] & 0xf) != 0) return 30; else return 29; } else return 0; } // ====== 傳回農曆 y年閏哪個月 1-12 , 沒閏傳回 0 public int leapMonth(int y) { long var = lunarInfo[y - 1900] & 0xf; return (int)(var == 0xf ? 0 : var); } // ====== 傳回農曆 y年m月的總天數 public int monthDays(int y, int m) { if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0) return 29; else return 30; } // ====== 傳回農曆 y年的生肖 public String animalsYear(int year) { final String[] Animals = new String[] { "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" }; return Animals[(year - 4) % 12]+"年"; } // ====== 傳入 月日的offset 傳回干支, 0=甲子 final private static String cyclicalm(int num) { return (Gan[num % 10] + Zhi[num % 12]); } // ====== 傳入 offset 傳回干支, 0=甲子 final public String cyclical(int year, int month,int day) { int num = year - 1900 + 36; //立春日期 int term2 = sTerm(year, 2); if(month>2 || (month==2 && day>= term2)){ num = num + 0; }else{ num = num -1; } return (cyclicalm(num)); } public static String getChinaDayString(int day) {//將農曆day日格式化成農曆表示的字串 String chineseTen[] = { "初", "十", "廿", "卅" }; String chineseDay[] = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"}; String var=""; if(day!=20 && day != 30){ var = chineseTen[(int)((day-1)/10)]+ chineseDay[(int)((day-1)%10)]; }else if(day !=20){ var = chineseTen[(int)(day/10)]+"十"; }else{ var = "二十"; } return var; } /* * 計算公曆nY年nM月nD日和bY年bM月bD日漸相差多少天 * */ public int getDaysOfTwoDate(int bY,int bM, int bD,int nY, int nM,int nD){ Date baseDate = null; Date nowaday = null; try { baseDate = chineseDateFormat.parse(bY+"年"+bM+"月"+bD+"日"); nowaday = chineseDateFormat.parse(nY+"年"+nM+"月"+nD+"日"); } catch (ParseException e) { e.printStackTrace(); } // 求出相差的天數 int offset = (int) ((nowaday.getTime() - baseDate.getTime()) / 86400000L); return offset; } /*農曆lunYear年lunMonth月lunDay日 * isLeap 當前年月是否是閏月 * 從農曆日期轉換成公曆日期 * */ public Calendar getSunDate(int lunYear, int lunMonth, int lunDay, boolean isLeap){ //公曆1900年1月31日為1900年正月初一 int years = lunYear -1900; int days=0; for(int i=0;i<years;i++){ days += yearDays(1900+i);//農曆某年總天數 } for(int i=1;i<lunMonth;i++){ days +=monthDays(lunYear,i); } if(leapMonth(lunYear)!=0 && lunMonth>leapMonth(lunYear)){ days += leapDays(lunYear);//lunYear年閏月天數 } if(isLeap){ days +=monthDays(lunYear,lunMonth);//lunYear年lunMonth月 閏月 } days +=lunDay; days = days-1; Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR, 1900); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH,31); cal.add(Calendar.DATE, days); /* Date date=cal.getTime(); int year_c = cal.get(Calendar.YEAR); int month_c = cal.get(Calendar.MONTH)+1; int day_c = cal.get(Calendar.DAY_OF_MONTH); */ return cal; } public String getLunarString(int year, int month, int day){ int var = getLunarDateINT(year,month,day); int lYear = (int)var/10000; int lMonth = (int)(var%10000)/100; int lDay = var - lYear*10000 - lMonth*100; String lunY = cyclical(year,month,day)+"年"; String lunM=""; int testMonth = getSunDate(lYear,lMonth,lDay,false).get(Calendar.MONTH)+1; if(testMonth!=month){ lunM = "閏"; } lunM += chineseNumber[(lMonth-1)%12]+"月"; //int leap = leapMonth(lYear); String lunD = getChinaDayString(lDay); int animalsYear = 0; int term2 = sTerm(year, 2);//立春 if(month>2 ||(month==2&& day>=term2)){ animalsYear = year; }else{ animalsYear = year-1; } String Festival = getFestival(year,month,day); //return lunY+"-"+lunM+"-"+lunD+"-"+animalsYear(animalsYear); // return "農曆 "+lunM+lunD+" "+Festival; return ""+lunM+" "+lunD+" "; } /* * 將公曆year年month月day日轉換成農曆 * 返回格式為20140506(int) * */ public int getLunarDateINT(int year, int month, int day){ int iYear,LYear,LMonth,LDay, daysOfYear = 0; // 求出和1900年1月31日相差的天數 //year =1908; //month = 3; //day =3; int offset = getDaysOfTwoDate(1900,1,31,year,month,day); //Log.i("--ss--","公曆:"+year+"-"+month+"-"+day+":"+offset); // 用offset減去每農曆年的天數 // 計算當天是農曆第幾天 // i最終結果是農曆的年份 // offset是當年的第幾天 for (iYear = 1900; iYear < 2100 && offset >0; iYear++) { daysOfYear = yearDays(iYear); offset -= daysOfYear; //Log.i("--ss--","農曆:"+iYear+":"+daysOfYear+"/"+offset); } if (offset < 0) { offset += daysOfYear; iYear--; //Log.i("--ss--","農曆:"+iYear+":"+daysOfYear+"/"+offset); } // 農曆年份 LYear = iYear; leapMonth = leapMonth(iYear); // 閏哪個月,1-12 leap = false; // 用當年的天數offset,逐個減去每月(農曆)的天數,求出當天是本月的第幾天 int iMonth=1, daysOfMonth = 0; for (iMonth = 1; iMonth < 13 && offset >0; iMonth++) { // 閏月 if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) { --iMonth; leap = true; daysOfMonth = leapDays(iYear); } else{ daysOfMonth = monthDays(iYear, iMonth); } // 解除閏月 if (leap && iMonth == (leapMonth + 1)) leap = false; offset -= daysOfMonth; //Log.i("--ss--","農曆:"+iYear+"-"+iMonth+":"+daysOfMonth+"/"+offset); } // offset為0時,並且剛才計算的月份是閏月,要校正 if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) { if (leap) { leap = false; } else { leap = true; --iMonth; } } // offset小於0時,也要校正 if (offset < 0) { offset += daysOfMonth; --iMonth; //Log.i("--ss--","農曆:"+iYear+"-"+iMonth+":"+daysOfMonth+"/"+offset); } LMonth = iMonth; LDay = offset + 1; //Log.i("--ss--","農曆:"+LYear+"-"+LMonth+"-"+LDay); return LYear * 10000 + LMonth *100 + LDay; } public String getFestival(int year, int month, int day){//獲取公曆對應的節假日或二十四節氣 //農曆節假日 int var = getLunarDateINT(year, month, day); int lun_year = (int)var/10000; int lun_month = (int)(var%10000)/100; int lun_Day = (int)(var - lun_year*10000 - lun_month*100); for (int i = 0; i < lunarHoliday.length; i++) { // 返回農曆節假日名稱 String ld = lunarHoliday[i].split(" ")[0]; // 節假日的日期 String ldv = lunarHoliday[i].split(" ")[1]; // 節假日的名稱 String lmonth_v = lun_month + ""; String lday_v = lun_Day + ""; String lmd = ""; if (month < 10) { lmonth_v = "0" + lun_month; } if (day < 10) { lday_v = "0" + lun_Day; } lmd = lmonth_v + lday_v; if (ld.trim().equals(lmd.trim())) { return ldv; } } //公曆節假日 for (int i = 0; i < solarHoliday.length; i++) { if(i==solarHoliday.length && year<1893 //1893年前沒有此節日 || i+3 == solarHoliday.length && year<1999 || i+6 == solarHoliday.length && year<1942 || i+10 == solarHoliday.length && year<1949 || i == 19 && year<1921 || i == 20 && year<1933 || i == 22 && year<1976){ break; } // 返回公曆節假日名稱 String sd = solarHoliday[i].split(" ")[0]; // 節假日的日期 String sdv = solarHoliday[i].split(" ")[1]; // 節假日的名稱 String smonth_v = month + ""; String sday_v = day + ""; String smd = ""; if (month < 10) { smonth_v = "0" + month; } if (day < 10) { sday_v = "0" + day; } smd = smonth_v + sday_v; if (sd.trim().equals(smd.trim())) { return sdv; } } int b = getDateOfSolarTerms(year,month); if(day==(int)b/100){ return sTermInfo[(month-1)*2]; }else if(day== b%100){ return sTermInfo[(month-1)*2+1]; } return ""; } /** */ /** * 傳出y年m月d日對應的農曆. yearCyl3:農曆年與1864的相差數 ? monCyl4:從1900年1月31日以來,閏月數 * dayCyl5:與1900年1月31日相差的天數,再加40 ? * * isday: 這個引數為false---日期為節假日時,陰曆日期就返回節假日 ,true---不管日期是否為節假日依然返回這天對應的陰曆日期 * * @param * @return */ public String getLunarDate(int year_log, int month_log, int day_log, boolean isday) { int var = getLunarDateINT(year_log,month_log,day_log); // 農曆年份 year = (int)var/10000; setYear(year); // 設定公曆對應的農曆年份 //農曆月份 month = (int)(var%10000)/100; setLunarMonth(chineseNumber[(month-1)%12] + "月"); // 設定對應的陰曆月份 day = var - year*10000 - month*100; if (!isday) { String Festival = getFestival(year_log,month_log,day_log); if(Festival.length()>1){ return Festival; } } if (day == 1) return chineseNumber[(month-1)%12] + "月"; else return getChinaDayString(day); } public String toString() { if (chineseNumber[(month-1)%12] == "正" && getChinaDayString(day) == "初一") return "農曆" + year + "年"; else if (getChinaDayString(day) == "初一") return chineseNumber[(month-1)%12] + "月"; else return getChinaDayString(day); // return year + "年" + (leap ? "閏" : "") + chineseNumber[month - 1] + // "月" + getChinaDayString(day); } /* * public static void main(String[] args) { System.out.println(new * LunarCalendar().getLunarDate(2012, 1, 23)); } */ public int getLeapMonth() { return leapMonth; } public void setLeapMonth(int leapMonth) { this.leapMonth = leapMonth; } /** * 得到當前日期對應的陰曆月份 * * @return */ public String getLunarMonth() { return lunarMonth; } public void setLunarMonth(String lunarMonth) { this.lunarMonth = lunarMonth; } /** * 得到當前年對應的農曆年份 * * @return */ public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getConstellation(int month, int day){//計算星座 int date = month*100 + day; String constellation=""; if(date>=120 && date <=218){ constellation = constellations[1]; }else if(date>=219 && date<=320){ constellation = constellations[2]; }else if(date>=321 && date<=419){ constellation = constellations[3]; }else if(date>=420 && date<=520){ constellation = constellations[4]; }else if(date>=521 && date<=620){ constellation = constellations[5]; }else if(date>=621 && date<=721){ constellation = constellations[6]; }else if(date>=722 && date<=822){ constellation = constellations[7]; }else if(date>=823 && date<=922){ constellation = constellations[8]; }else if(date>=923 && date<=1022){ constellation = constellations[9]; }else if(date>=1023 && date<=1121){ constellation = constellations[10]; }else if(date>=1122 && date<=1221){ constellation = constellations[11]; }else{ constellation = constellations[0]; } return constellation; } private int sTerm(int y, int n) { int[] sTermInfo = new int[]{0,21208,42467,63836,85337,107014, 128867,150921,173149,195551,218072,240693, 263343,285989,308563,331033,353350,375494, 397447,419210,440795,462224,483532,504758}; Calendar cal = Calendar.getInstance(); cal.set(1900, 0, 6, 2, 5, 0); long temp = cal.getTime().getTime(); cal.setTime(new Date( (long) ((31556925974.7 * (y - 1900) + sTermInfo[n] * 60000L) + temp))); int a=cal.get(Calendar.DAY_OF_MONTH); return a; } public int getDateOfSolarTerms(int year, int month){ int a = sTerm(year,(month-1)*2); int b = sTerm(year,(month-1)*2 +1); return a*100+b; //return 0; } public String jcrt(String d) { String jcrjxt=""; String yj0 = "宜:\t",yj1 = "忌:\t",br = "-"; //String yj0 = "",yj1 = "",br = "-"; if (d == "建") jcrjxt = yj0 + yi_string[0] + br + yj1 + ji_string[0]; if (d == "除") jcrjxt = yj0 + yi_string[1] + br + yj1 + ji_string[1]; if (d == "滿") jcrjxt = yj0 + yi_string[2] + br + yj1 + ji_string[2]; if (d == "平") jcrjxt = yj0 + yi_string[3] + br + yj1 + ji_string[3]; if (d == "定") jcrjxt = yj0 + yi_string[4] + br + yj1 + ji_string[4]; if (d == "執") jcrjxt = yj0 + yi_string[5] + br + yj1 + ji_string[5]; if (d == "破") jcrjxt = yj0 + yi_string[6] + br + yj1 + ji_string[6]; if (d == "危") jcrjxt = yj0 + yi_string[7] + br + yj1 + ji_string[7]; if (d == "成") jcrjxt = yj0 + yi_string[8] + br + yj1 + ji_string[8]; if (d == "收") jcrjxt = yj0 + yi_string[9] + br + yj1 + ji_string[9]; if (d == "開") jcrjxt = yj0 + yi_string[10] + br + yj1 + ji_string[10]; if (d == "閉") jcrjxt = yj0 + yi_string[11] + br + yj1 + ji_string[11]; return jcrjxt; } /* num_y%12, num_m%12, num_d%12, num_y%10, num_d%10 * m:農曆月份 1---12 * dt:農曆日 * */ public String CalConv2(int yy, int mm, int dd, int y, int d, int m, int dt) { String dy = d + "" + dd; if ((yy == 0 && dd == 6) || (yy == 6 && dd == 0) || (yy == 1 && dd == 7) || (yy == 7 && dd == 1) || (yy == 2 && dd == 8) || (yy == 8 && dd == 2) || (yy == 3 && dd == 9) || (yy == 9 && dd == 3) || (yy == 4 && dd == 10) || (yy == 10 && dd == 4) || (yy == 5 && dd == 11) || (yy == 11 && dd == 5)) { /* * 地支共有六對相沖,俗稱“六沖”,即:子午相沖、醜未相沖、寅申相沖、卯酉相沖、辰戌相沖、巳亥相沖。 * 如當年是甲子年,子為太歲,與子相沖者是午,假如當日是午日,則為歲破,其餘的以此類推,即丑年的歲破為未日, * 寅年的歲破為申日,卯年的歲破為酉日,辰年的歲破為戌日,巳年的歲破為亥日,午年的歲破為子日,未年的歲破為丑日, * 申年的歲破為寅日,酉年的歲破為卯日,戌年的歲破為辰日,亥年的歲破為巳日。 * */ return "日值歲破 大事不宜"; } else if ((mm == 0 && dd == 6) || (mm == 6 && dd == 0) || (mm == 1 && dd == 7) || (mm == 7 && dd == 1) || (mm == 2 && dd == 8) || (mm == 8 && dd == 2) || (mm == 3 && dd == 9) || (mm == 9 && dd == 3) || (mm == 4 && dd == 10) || (mm == 10 && dd == 4) || (mm == 5 && dd == 11) || (mm == 11 && dd == 5)) { return "日值月破 大事不宜"; } else if ((y == 0 && dy == "911") || (y == 1 && dy == "55") || (y == 2 && dy == "111") || (y == 3 && dy == "75") || (y == 4 && dy == "311") || (y == 5 && dy == "95") || (y == 6 && dy == "511")|| (y == 7 && dy == "15") || (y == 8 && dy == "711") || (y == 9 && dy == "35")) { return "日值上朔 大事不宜"; } else if ((m == 1 && dt == 13) || (m == 2 && dt == 11) || (m == 3 && dt == 9) || (m == 4 && dt == 7) || (m == 5 && dt == 5) || (m == 6 && dt == 3) || (m == 7 && dt == 1) || (m == 7 && dt == 29) || (m == 8 && dt == 27) || (m == 9 && dt == 25) || (m == 10 && dt == 23) || (m == 11 && dt == 21) || (m == 12 && dt == 19)) { /* * 楊公十三忌 以農曆正月十三日始,以後每月提前兩天為百事禁忌日 * */ return "日值楊公十三忌 大事不宜"; //}else if(var == getSiLi(m,dt)){ //return "日值四離 大事勿用"; } else { return null; } } public int getDaysOfMonth(int year, int month){//返回公曆year年month月的當月總天數 if(month == 2){ return(((year%4 == 0) && (year%100 != 0) || (year%400 == 0))? 29: 28); }else if(month == 1 || month == 3 ||month == 5 || month == 7 || month == 8 ||month == 10 || month == 12){ return 31; } return 30; } /*輸入 * year 公曆年份,大於1900 * month 公曆月份 1--12 * 輸出 * ArrayList<String> year年month月全月宜忌 * */ public ArrayList<String> getyiji(int year, int month) { int num_y = -1; int num_m = (year - 1900) * 12 + month-1 +12; int num_d; int mLMonth=1,mLDay=1,mLun_x=0; int days_of_month = getDaysOfMonth(year, month); ArrayList<String> yiji = new ArrayList<String>(); //年柱 1900年立春後為庚子年(60進位制36) //cyclical(year,month); //立春日期 int term2 = sTerm(year, 2); //月柱 1900年1月小寒以前為 丙子月(60進位制12) int firstNode = sTerm(year, (month-1)*2);//當月的24節氣中的節開始日 //cyclicalm(num_m); //Calendar cal = Calendar.getInstance(); //cal.set(year, month, 1, 0, 0, 0); //1900/1/1與 1970/1/1 相差25567日, 1900/1/1 日柱為甲戌日(60進位制10) int dayCyclical = (int) (getDaysOfTwoDate(1900,1,1,year,month,1)+10); for(int i=0;i<days_of_month;i++){ if(mLDay > mLun_x){ //Log.i("","mLDay > mLun_x "+mLDay+":"+mLun_x); int var = getLunarDateINT(year,month,i+1); int mLYear =(int)var/10000; mLMonth = (int)(var%10000)/100; mLDay = (int)(var - mLYear*10000 - mLMonth*100); mLun_x = (leapMonth(mLYear)!=0)?leapDays(mLYear):monthDays(mLYear, mLMonth); //Log.i("","mLDay > mLun_x ?"+mLDay+":"+mLun_x); } //依節氣調整二月分的年柱, 以立春為界 if(month==2 && (i+1)==term2){ //cY = cyclicalm(year-1900 + 36); num_y = year - 1900 + 36; } //依節氣 調整月柱, 以「節」為界 if((i+1)==firstNode) { num_m =(year - 1900) * 12 + month +12 ; //cM = cyclicalm(num_m); } //日柱 //cD = cyclicalm(dayCyclical + i); num_d = dayCyclical +i; mLDay++; //Log.i("","---num_y:"+num_y+","+num_m+","+num_d+",n_m:"+mLMonth+",n_d:"+mLDay+",mLun_x:"+mLun_x); String str = CalConv2(num_y%12, num_m%12, num_d%12, num_y%10, num_d%10, mLMonth, mLDay-1); if(str == null){ String var = jcName[num_m%12][num_d%12]; str = jcrt(var); //Log.i("","---"+month+"-"+(i+1)+","+var+":"+str); } //Log.i("","---"+year+"-"+month+"-"+(i+1)+","+str); yiji.add(str); } return yiji; } /* * 公曆某日宜忌 * */ public String getyiji(int year, int month, int day) { int num_y = -1; int num_m = (year - 1900) * 12 + month-1 +12; int num_d; int mLMonth=1,mLDay=1; int days_of_month = getDaysOfMonth(year, month); if(day > days_of_month) day = days_of_month; //年柱 1900年立春後為庚子年(60進位制36) //cyclical(year,month); //立春日期 int term2 = sTerm(year, 2); int firstNode = sTerm(year, (month-1)*2);//當月的24節氣中的節開始日 int dayCyclical = (int) (getDaysOfTwoDate(1900,1,1,year,month,1)+10); if(month==2 && day==term2){ //cY = cyclicalm(year-1900 + 36);//依節氣調整二月分的年柱, 以立春為界 num_y = year - 1900 + 36; } if(day==firstNode) { num_m =(year - 1900) * 12 + month +12 ;//依節氣 調整月柱, 以「節」為界 //cM = cyclicalm(num_m); } num_d = dayCyclical +day -1; int var = getLunarDateINT(year,month,day); int mLYear =(int)var/10000; mLMonth = (int)(var%10000)/100; mLDay = (int)(var - mLYear*10000 - mLMonth*100); //Log.i("","---num_y:"+num_y+","+num_m+","+num_d+",n_m:"+mLMonth+",n_d:"+mLDay+",mLun_x:"+mLun_x); String str = CalConv2(num_y%12, num_m%12, num_d%12, num_y%10, num_d%10, mLMonth, mLDay); if(str == null){ str = jcrt(jcName[num_m%12][num_d%12]); //Log.i("","---"+month+"-"+(i+1)+","+var+":"+str); } //Log.i("","---"+year+"-"+month+"-"+(i+1)+","+str); return str; } /* * 計算距離1900年12月31日days天后的日期 * */ public int getDateFromBaseDate(int days){ int year = 0,month = 0,day = 0; Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR, 1900); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH,31); cal.add(Calendar.DATE, days); year = cal.get(Calendar.YEAR); month = cal.get(Calendar.MONTH)+1; day = cal.get(Calendar.DAY_OF_MONTH); return 10000*year+100*month+day; } }
呼叫 舉例
/**
* 獲取現在農曆的日期
*/
public static String getDayLunar() {
LunarCalender lunarCalender = new LunarCalender();
String lunarDate = "00";
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DATE);
// lunarDate = lunarCalender.getLunarDate(year, month, day, true);
// String lunarMonth = lunarCalender.getLunarMonth();
String lunarAnimal = lunarCalender.animalsYear(year);
String lunarString = lunarCalender.getLunarString(year, month, day);
return lunarString+" ["+lunarAnimal+"]";
}