Convert a Java solar calendar to a lunar calendar

Source: Internet
Author: User
Package COM. work. util;/*** @ author wangmj * written at: 2005-8-2 <br/> * Project name: 00 newframe <br/> * Class Name: Lunar. java <br/> */import Java. util. calendar; import Java. util. date; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; // import COM. web. util. ourlog; public final class lauar {Private Static log = logfactory. getlog (lauar. class); Private Static int moncyl, Daycyl, yearcyl; Private Static int year, month, day; Private Static Boolean isleap; Private Static int [] lunarinfo = {0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, primary, primary, 0x0ada2, primary, 0x14977, 0x04970, Primary, Secondary, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, Primary, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, primary, 0x0c950, primary, 0x1d8a6, 0x0b550, 0x056a0, primary, 0x0d2b2, primary, primary, 0x06ca0, 0x0b550, 0x15355, 0x04da0, primary, 0x14573, primary, primary, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, primary, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d 250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, clerk, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, clerk, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, expires, 0x0c960, 0x0d954, expires, 0x0da50, 0x07552, expires, 0x0cab5, expires, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, primary, 0x05b52, 0x04b60, primary, primary, 0x0d260, 0x0ea65, primary, 0x05aa0, 0x076a3, primary, 0x04bd7, 0x04ad0, primary, primary, 0x0d250, primary, 0x0dd45, primary, primary, 0x055b2, primary, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0}; Private Static int [] solarmonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Private Static string [] Gan = {"A", "B ", "C", "ding", "E", "Ji", "Geng", "Xin", "Yi", "Xi "}; private Static string [] Zhi = {"sub", "ugly", "Yin", "Mao", "Chen", "Si", "Wu", "wei ", "shen", "you", "shen", "Hai"}; Private Static string [] animals = {"rat", "Ox", "Tiger ", "rabbit", "dragon", "snake", "horse", "goat", "monkey", "chicken", "dog", "Swine "}; private Static int [] sterminfo = {0, 21208,424 67, 63836,853 37, 107014,128 867, 150921,173 149, 195551,218 072, 240693,263 343, 285989,308 563, 331033,353 350, 375 494,397 447, 419210,440 795, 462224,483 532, 504758}; Private Static string [] nstr1 = {"day", "1", "2", "3", "4 ", "5", "6", "7", "8", "9", "10"}; Private Static string [] nstr2 = {" ", "10", "comment", "comment", ""}; Private Static string [] monthnong = {"zheng", "Zheng", "two", "three ", "4", "5", "6", "7", "8", "9", "10", "11", "12 "}; private Static string [] yearname = {"zero", "one", "two", "three", "Si", "Wu", "Lu", "Lu ", "success", "fail" }; Public lauar () {} // ============================================ = returns the total number of days of the lunar y year. Private Static int lyeardays (INT y) {int I; int sum = 348; // 29*12 for (I = 0x8000; I> 0x8; I >>=1) {// ourlog. debug ("I =" + I); sum + = (lunarinfo [Y-1900] & I) = 0? 0: 1; // large month + 1 day} return (sum + leapdays (y )); // + number of days in a leap month} // ================================== =========return the number of days in the y-year month Private Static int leapdays (INT y) {If (leapmonth (y )! = 0) Return (lunarinfo [Y-1900] & 0x10000) = 0? 29: 30); else return (0 );} // ================================================ returns the month from 1 to 12 in the Y-year lunar month, 0 Private Static int leapmonth (INT y) {return (lunarinfo [Y-1900] & 0xf );} // ================================================ returns the total number of days in the month of the lunar year Y. Private Static int monthdays (INT y, int m) {return (lunarinfo [Y-1900] & (0x10000> m) = 0? 29: 30 );} // ================================================ calculate the lunar calendar, input date object, and return the date object of the lunar calendar // This object has attributes. year. month. day. isleap. yearcyl. daycyl. moncyl Private Static void lunar1 (date objdate) {int I, leap = 0, temp = 0; // int moncyl, daycyl, yearcyl; // int year, month, day; // Boolean isleap; Calendar Cl = calendar. getinstance (); CL. set (1900, 0, 31); // lunar January 1, 1900 is the lunar calendar month date basedate = Cl. gettime (); // 1900-01- 31 is the lunar calendar month int offset = (INT) (objdate. gettime ()-basedate. gettime ()/86400000); // number of days (86400000 = 24*60*60*1000) // system. out. println (offset); daycyl = offset + 40; // 1899-12-21 is moncyl = 14, moncyl, January 1, lunar December 1899; // 1898-10-01 is the year of the lunar month. // The obtained year is for (I = 1900; I <2050 & Offset> 0; I ++) {temp = lyeardays (I); // the number of days of the lunar calendar year offset-= temp; moncyl + = 12 ;} if (offset <0) {Offset + = temp; I --; moncyl-= 12;} ye AR = I; // yearcyl = I-1864 of the lunar year; // The leap of June 1 = leapmonth (I); // The monthly isleap of June 1 = false; for (I = 1; I <13 & Offset> 0; I ++) {// leap month if (LEAP> 0 & I = (LEAP + 1) & isleap = false) {-- I; isleap = true; temp = leapdays (year);} else {temp = monthdays (year, I );} // cancel the leap month if (isleap = true & I = (LEAP + 1) isleap = false; offset-= temp; If (isleap = false) moncyl ++;} If (offset = 0 & & Amp; leap> 0 & amp; I = leap + 1) if (isleap) {isleap = false;} else {isleap = true; -- I; -- moncyl ;} if (offset <0) {Offset + = temp; -- I; -- moncyl;} month = I; // day of the lunar month = offset + 1; // lunar calendar // system. out. println (day);} Private Static int getyear () {return (year);} Private Static int getmonth () {return (month);} Private Static int getday () {return (day);} Private Static int getmoncyl (){ Return (moncyl);} Private Static int getyearcyl () {return (yearcyl);} Private Static int getdaycyl () {return (daycyl);} Private Static Boolean getisleap () {return (isleap );} /// ================================================ pass in the offset to send back a dry branch, 0 = Private Static string literal ical (INT num) {return (GaN [num % 10] + Zhi [num % 12]);} // ================================ Chinese Date Private Static string CDAY (INT d) {string s; switc H (d) {case 10: S = ""; break; case 20: S = ""; break; Case 30: S = "Thirty"; break; default: S = nstr2 [(INT) (D/10)]; // operator S + = nstr1 [d % 10]; // return (s );} private Static string cYear (INT y) {string S = ""; int D; while (Y> 0) {d = Y % 10; y = (Y-d) /10; S = yearname [d] + S;} return (s);} public static string getlunar (string year, string month, string day) {date sdobj; string s; Int Sy, Sm, SD; int sy; Sy = integer. parseint (year); Sm = integer. parseint (month); SD = integer. parseint (day); Sy = (sy-4) % 12; log. debug ("sy =" + Sy + "Sm =" + SM + "SD =" + SD + "sy =" + Sy); Calendar Cl = calendar. getinstance (); CL. set (SY, Sm-1, SD); sdobj = Cl. gettime (); // COM. web. util. ourlog. debug ("sdobj =" + sdobj); // Date lunar1 (sdobj ); // S = "" + "[" + animals [sy] + "]" + cYear (get Year () + "year" + ""; S + = (getisleap ()? "Expiration": "") + monthnong [getmonth ()] + "month" + (monthdays (getyear (), getmonth () = 29? "Small": "large"); S + = CDAY (getday () + ""; S + = semantic ical (getyearcyl ()) + "year" + semantic ical (getmoncyl () + "month" + semantic ical (getdaycyl () + "day"; // system. out. println (s); Return s;} public static void main (string [] ARGs) {system. out. println (getlunar ("2005", "8", "2"); system. out. println (getlunar ("2006", "8", "3"); system. out. println (getlunar ("2008", "6", "8"); system. out. println (getlunar ("2010", "8", "3"); system. out. println (getlunar ("2010", "7", "18 ")); // /[Tiger] June = // lunar calendar [chicken] June July /lunar calendar [Dog] // }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.