Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;/*** Lunar (lunar) tool class **/ Public classLunarcalendar {Private intYear//Lunar Year Private intMonth//Lunar month Private intDay//Lunar calendar Day Private BooleanLeap//Lunar leap Year//the above 4 values are calculated in the construction method based on the incoming date /*** Chinese month name*/ Final StaticString chinesenumber[] = {"One", "two", "three", "four", "five", "six", "seven", "eight", "Nine", "Ten", "11", "12" }; /*** Chinese date format*/ StaticSimpleDateFormat Chinesedateformat =NewSimpleDateFormat ("yyyy mm month DD Day"); /*** Lunar data, 1901 ~ 2,100 years between correct*/ Final Static Long[] Lunarinfo =New Long[] {0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0X06D40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0X1A5B4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0X06D40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0X074A3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0X0BAA4, 0X0AD50, 0x055d9, 0X04BA0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0X0AD50, 0x05b52, 0x04b60, 0x0a6e6, 0X0A4E0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0X076A3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0X0DD45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0X06D20, 0x0ada0 }; //the total number of days ====== returned to the lunar calendar y year Final Private Static intYeardays (inty) {intI, sum = 348; for(i = 0x8000; i > 0x8; I >>= 1) { if((lunarinfo[y-1900] & i)! = 0) Sum+ = 1; } return(Sum +leapdays (y)); } //====== Returns the number of days of the Lunar New Year leap month Final Private Static intLeapdays (inty) {if(Leapmonth (y)! = 0) { if((lunarinfo[y-1900] & 0x10000)! = 0) return30; Else return29; } Else return0; } //====== return to the lunar calendar y year which month 1-12, no leap back 0 Final Private Static intLeapmonth (inty) {return(int) (lunarinfo[y-1900] & 0xf); } //====== Returns the total number of days of the lunar year m month Final Private Static intMonthdays (intYintm) {if((lunarinfo[y-1900] & (0x10000 >> m) = = 0) return29; Else return30; } //====== returns to Lunar New Year's zodiac Final PublicString animalsyear () {Finalstring[] Animals =NewString[] {"Rat", "ox", "Tiger", "Rabbit", "dragon", "Snake", "horse", "sheep", "monkey", "Chicken", "dog", "pig" }; returnanimals[(year-4)% 12]; } //====== incoming monthly offset return to Ganzhi, 0 = Koshien Final Private StaticString Cyclicalm (intnum) { Finalstring[] Gan =NewString[] {"A", "B", "C", "Ding", "e", "own", "Geng", "Xin", "Ren", "GUI" }; Finalstring[] Zhi =NewString[] {"Zi", "ugly", "Yin", "Mao", "Chen", "WU", "noon", "not", "Shen", "unitary", "Xu", "Hai" }; return(gan[num%] + zhi[num% 12]); } //====== incoming offset returns Ganzhi, 0 = Koshien Final PublicString cyclical () {intnum = year-1900 + 36; return(Cyclicalm (num)); } /*** Construct a lunar calendar that represents the current date*/ PublicLunarcalendar () { This(Calendar.getinstance ()); } /*** Constructs a lunar calendar that represents a specified date*/ PublicLunarcalendar (date date) { This(Date2calendar (date)); } Private StaticCalendar Date2calendar (date date) {Calendar Tmpcale=calendar.getinstance (); Tmpcale.settime (date); returnTmpcale; } /*** Construct a lunar calendar with the specified calendar date * *@paramCal*/ PublicLunarcalendar (Calendar cal) {@SuppressWarnings ("Unused") intyearcyl, moncyl, daycyl; intLeapmonth = 0; Date basedate=NULL; Try{basedate= Chinesedateformat.parse ("January 31, 1900"); } Catch(ParseException e) {}//find out the difference between January 31, 1900 and the number of days intOffset = (int) ((Cal.gettime (). GetTime ()-basedate.gettime ())/86400000L); Daycyl= offset + 40; Moncyl= 14; //subtract the number of days per lunar year with offset//days of the day are calculated//I The final result is the year of the lunar calendar//offset is the first day of the year intIyear, daysofyear = 0; for(iyear = 1900; iyear < 2050 && offset > 0; iyear++) {daysofyear=yeardays (iyear); Offset-=daysofyear; Moncyl+ = 12; } if(Offset < 0) {offset+=daysofyear; Iyear--; Moncyl-= 12; } //Lunar YearYear =iyear; Yearcyl= iYear-1864; Leapmonth= Leapmonth (iyear);//Leap which month, 1-12Leap =false; //Use the days of the year offset, minus the number of days per month (lunar), to find out the day of the month intImonth, Daysofmonth = 0; for(imonth = 1; imonth < && offset > 0; imonth++) { //Leap Month if(Leapmonth > 0 && imonth = = (Leapmonth + 1) &&!Leap) { --Imonth; Leap=true; Daysofmonth=leapdays (year); } ElseDaysofmonth=monthdays (year, imonth); Offset-=Daysofmonth; //Lift Leap Month if(Leap && Imonth = = (Leapmonth + 1)) Leap=false; if(!leap) moncyl++; } //offset is 0 o'clock, and the month that you just calculated is leap months, to correct if(offset = = 0 && leapmonth > 0 && imonth = = Leapmonth + 1) { if(LEAP) {leap=false; } Else{Leap=true; --Imonth; --moncyl; } } //offset less than 0 o'clock, also to be corrected if(Offset < 0) {offset+=Daysofmonth; --Imonth; --moncyl; } Month=Imonth; Day= offset + 1; } /*** Date object representing the lunar calendar day *@return */ PublicDate getDate () {Calendar Calendar=calendar.getinstance (); Calendar.set (Calendar.year, year); Calendar.set (Calendar.month, MONTH-1); Calendar.set (Calendar.day_of_month, day); returnCalendar.gettime (); } Public StaticString getchinadaystring (intDay ) {String chineseten[]= {"Beginning", "Ten", "20", "30" }; intn = Day% 10 = = 0? 9:day% 10-1; if(Day > 30) return""; if(Day = = 10) returndecade; Else returnCHINESETEN[DAY/10] +Chinesenumber[n]; } PublicString toString () {returnYear + "Years" + (leap?) "Leap": "") + Chinesenumber[month-1] + "month" +getchinadaystring (day); }}
Lunar Calendar Tool Class