Conversion from a Java Gregorian calendar to a lunar calendar (18:11:52)
Tags: Java Gregorian Calendar |
Category: J2EE |
Package com. ljtools;
Import java. Text. simpledateformat;
Import java. util. calendar;
Import java. util. date;
Import java. util. gregoriancalendar;
Import java. util. locale;
Public class chinadate {
Final Private 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 };
Final Private Static int [] year20 = new int [] {1, 4, 1, 2, 1, 2, 1, 1, 2, 1 };
Final Private Static int [] year19 = new int [] {0, 3, 0, 1, 0, 1, 0, 0, 1, 0 };
Final Private Static int [] year2000 = new int [] {0, 3, 1, 2, 1, 2, 1, 1, 2, 1 };
Public final static string [] nstr1 = new string [] {"," Zheng "," two "," three "," four "," five "," Six ", "7", "8", "9", "10", "11 ",
"12 "};
Private Final Static string [] Gan = new string [] {"A", "B", "C", "ding", "Wu", "Ji ", "Geng", "Xin", "shen", "Xi "};
Private Final Static string [] Zhi = new string [] {"sub", "ugly", "Yin", "Mao", "Chen", "Si ", "Wu", "wei", "shen", "you", "Wu", "Hai "};
Private Final Static string [] animals = new string [] {"rat", "Ox", "Tiger", "rabbit", "dragon", "snake ", "horse", "goat", "monkey", "chicken", "dog", "Swine "};
Private Final Static string [] solarterm = new string [] {"Xiao Han", "da Han", "Li Chun", "Rain", "stunned", "Spring Equinox ", "Qingming Festival", "Gu Yu", "lixia ",
"Fill", "Mountain", "Summer Solstice", "Summer heat", "Summer heat", "Autumn", "Summer heat", "White Dew", "Autumn Equinox", "Cold Dew ", "frost", "Winter", "light snow", "heavy snow", "Winter Solstice "};
Private Final Static string [] sftv = new string [] {"0101 * New Year's Day", "0214 Valentine's Day", "0308 Women's Day", "0312 Arbor Day", "0315 Consumer Rights Day ",
"0401 fools", "0501 Labor Day", "0504 Youth Day", "0512 nurse's Day", "0601 Children's Day", "0701 Party Building Festival", "0801 army building Festival", "0808 Father's Day ",
"0909 Mao Zedong's death memorial", "0910 Teachers' Day", "0928 Confucius 'birthday", "1001 * National Day", "1006 elders", "1024 United Nations Day", "1112 sun yat-sen's birthday ", "1220 Macao return ",
"1225 Christmas", "1226 birthday "};
Private Final Static string [] lftv = new string [] {"0101 * Lunar New Year", "0115 Lantern Festival", "0505 Dragon Boat Festival", "0707 Valentine's Day ", "0815 Mid-Autumn Festival ",
"0909 Double Ninth Festival", "1208 lable Festival", "1224 New Year", "0100 * New Year's Eve "};
/**
* Returns the total number of days of the lunar y year.
*
* @ Param y
* @ Return
*/
Final Private Static int lyeardays (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 ));
}
/**
* Returns the number of days of the lunar y-year leap month.
*
* @ Param y
* @ Return
*/
Final Private Static int leapdays (INT y ){
If (leapmonth (y )! = 0 ){
If (lunarinfo [Y-1900] & 0x10000 )! = 0)
Return 30;
Else
Return 29;
} Else
Return 0;
}
/**
* Returns the month 1-12 in the Y-year period of the lunar calendar. If no value is returned, the return value is 0.
*
* @ Param y
* @ Return
*/
Final Private Static int leapmonth (INT y ){
Return (INT) (lunarinfo [Y-1900] & 0xf );
}
/**
* Returns the total number of days of the lunar month y.
*
* @ Param y
* @ Param m
* @ Return
*/
Final Private Static int monthdays (INT y, int m ){
If (lunarinfo [Y-1900] & (0x10000> m) = 0)
Return 29;
Else
Return 30;
}
/**
* Returns the Chinese zodiac of Y years.
*
* @ Param y
* @ Return
*/
Final public static string animalsyear (INT y ){
Return animals [(Y-4) % 12];
}
/**
* The offset of the input month and day is returned as a dry branch, 0 = a child
*
* @ Param num
* @ Return
*/
Final Private Static string javasicalm (INT num ){
Return (GaN [num % 10] + Zhi [num % 12]);
}
/**
* Input offset to return the trunk, 0 = a child
*
* @ Param y
* @ Return
*/
Final public static string logical ical (INT y ){
Int num = Y-1900 + 36;
Return (cyclicalm (Num ));
}
/**
* Send the lunar calendar. year0. month1. day2. yearcyl3. moncyl4. daycyl5. isleap6
*
* @ Param y
* @ Param m
* @ Return
*/
Final private long [] Lunar (INT y, int m ){
Long [] nongdate = new long [7];
Int I = 0, temp = 0, leap = 0;
Date basedate = new gregoriancalendar (1900 + 1900, 1, 31). gettime ();
Date objdate = new gregoriancalendar (Y + 1900, M, 1). gettime ();
Long offset = (objdate. gettime ()-basedate. gettime ()/86400000l;
If (Y <1, 2000)
Offset + = year19 [m-1];
If (Y> 2000)
Offset + = year20 [m-1];
If (y = 2000)
Offset + = year2000 [m-1];
Nongdate [5] = offset + 40;
Nongdate [4] = 14;
For (I = 1900; I <2050 & Offset> 0; I ++ ){
Temp = lyeardays (I );
Offset-= temp;
Nongdate [4] + = 12;
}
If (offset <0 ){
Offset + = temp;
I --;
Nongdate [4]-= 12;
}
Nongdate [0] = I;
Nongdate [3] = I-1864;
Leap = leapmonth (I); // month of the queue
Nongdate [6] = 0;
For (I = 1; I <13 & Offset> 0; I ++ ){
// Leap month
If (LEAP> 0 & I = (LEAP + 1) & nongdate [6] = 0 ){
-- I;
Nongdate [6] = 1;
Temp = leapdays (INT) nongdate [0]);
} Else {
Temp = monthdays (INT) nongdate [0], I );
}
// Release the leap month
If (nongdate [6] = 1 & I = (LEAP + 1 ))
Nongdate [6] = 0;
Offset-= temp;
If (nongdate [6] = 0)
Nongdate [4] ++;
}
If (offset = 0 & leap> 0 & I = leap + 1 ){
If (nongdate [6] = 1 ){
Nongdate [6] = 0;
} Else {
Nongdate [6] = 1;
-- I;
-- Nongdate [4];
}
}
If (offset <0 ){
Offset + = temp;
-- I;
-- Nongdate [4];
}
Nongdate [1] = I;
Nongdate [2] = offset + 1;
Return nongdate;
}
/**
* The Lunar Calendar year0. month1. day2. yearcyl3. moncyl4. daycyl5. isleap6 corresponding to Y, M, and D
*
* @ Param y
* @ Param m
* @ Param d
* @ Return
*/
Final public static long [] calelement (INT y, int M, int d ){
Long [] nongdate = new long [7];
Int I = 0, temp = 0, leap = 0;
Date basedate = new gregoriancalendar (0 + 1900, 0, 31). gettime ();
Date objdate = new gregoriancalendar (Y, m-1, D). gettime ();
Long offset = (objdate. gettime ()-basedate. gettime ()/86400000l;
Nongdate [5] = offset + 40;
Nongdate [4] = 14;
For (I = 1900; I <2050 & Offset> 0; I ++ ){
Temp = lyeardays (I );
Offset-= temp;
Nongdate [4] + = 12;
}
If (offset <0 ){
Offset + = temp;
I --;
Nongdate [4]-= 12;
}
Nongdate [0] = I;
Nongdate [3] = I-1864;
Leap = leapmonth (I); // month of the queue
Nongdate [6] = 0;
For (I = 1; I <13 & Offset> 0; I ++ ){
// Leap month
If (LEAP> 0 & I = (LEAP + 1) & nongdate [6] = 0 ){
-- I;
Nongdate [6] = 1;
Temp = leapdays (INT) nongdate [0]);
} Else {
Temp = monthdays (INT) nongdate [0], I );
}
// Release the leap month
If (nongdate [6] = 1 & I = (LEAP + 1 ))
Nongdate [6] = 0;
Offset-= temp;
If (nongdate [6] = 0)
Nongdate [4] ++;
}
If (offset = 0 & leap> 0 & I = leap + 1 ){
If (nongdate [6] = 1 ){
Nongdate [6] = 0;
} Else {
Nongdate [6] = 1;
-- I;
-- Nongdate [4];
}
}
If (offset <0 ){
Offset + = temp;
-- I;
-- Nongdate [4];
}
Nongdate [1] = I;
Nongdate [2] = offset + 1;
Return nongdate;
}
Public final static string getchinadate (INT day ){
String A = "";
If (Day = 10)
Return "10th ";
If (Day = 20)
Return "20 ";
If (Day = 30)
Return "Thirty ";
Int two = (INT) (day)/10 );
If (two = 0)
A = "beginning ";
If (two = 1)
A = "10 ";
If (two = 2)
A = "success ";
If (two = 3)
A = "3 ";
Int one = (INT) (day % 10 );
Switch (one ){
Case 1:
A + = "1 ";
Break;
Case 2:
A + = "2 ";
Break;
Case 3:
A + = "3 ";
Break;
Case 4:
A + = "4 ";
Break;
Case 5:
A + = "5 ";
Break;
Case 6:
A + = "6 ";
Break;
Case 7:
A + = "7 ";
Break;
Case 8:
A + = "8 ";
Break;
Case 9:
A + = "9 ";
Break;
}
Return;
}
Public static string today (){
Calendar today = calendar. getinstance (locale. simplified_chinese );
Int year = today. Get (calendar. year );
Int month = today. Get (calendar. month) + 1;
Int date = today. Get (calendar. date );
Long [] L = calelement (year, month, date );
Stringbuffer stoday = new stringbuffer ();
Try {
Stoday. append (SDF. Format (today. gettime ()));
Stoday. append (" ");
Stoday. append (seasonal ical (year ));
Stoday. append ('(');
Stoday. append (animalsyear (year ));
Stoday. append (") year ");
Stoday. append (nstr1 [(INT) L [1]);
Stoday. append ("month ");
Stoday. append (getchinadate (INT) (L [2]);
Return stoday. tostring ();
} Finally {
Stoday = NULL;
}
}
Private Static simpledateformat SDF = new simpledateformat ("yyyy-mm-dd eeeee ");
/**
* Lunar calendar tool usage demonstration
*
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
System. Out. println (today ());
}
}