📄 calendar.java
字号:
/* * calendar.java * * Created on 2008年4月10日, 上午14:30 */package javaapplication3;/** * * @author qiliubiao */import java.io.*;import java.util.*;import javax.swing.JOptionPane;import myPackage.P;public class calendar {private String ChineseEraT[]={"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};//taiGanprivate String ChineseEraD[]={"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};//diZhiprivate static String ChineseEra[]=new String[60];//ChineseEra table //用一位来表示一个月的大小,大月记为1,小月记为0,这样就用掉12 位(无闰月)或13 位(有闰月),再用高4位来表示闰月的月份,没有闰月记为0。//阴历有数和天数private static final int[] lunarMonthDays = { 0x4ae0, 0xa570, 0x5268, 0xd260, 0xd950, 0x6aa8, 0x56a0, 0x9ad0, 0x4ae8, 0x4ae0, // 1910 0xa4d8, 0xa4d0, 0xd250, 0xd548, 0xb550, 0x56a0, 0x96d0, 0x95b0, 0x49b8, 0x49b0, // 1920 0xa4b0, 0xb258, 0x6a50, 0x6d40, 0xada8, 0x2b60, 0x9570, 0x4978, 0x4970, 0x64b0, // 1930 0xd4a0, 0xea50, 0x6d48, 0x5ad0, 0x2b60, 0x9370, 0x92e0, 0xc968, 0xc950, 0xd4a0, // 1940 0xda50, 0xb550, 0x56a0, 0xaad8, 0x25d0, 0x92d0, 0xc958, 0xa950, 0xb4a8, 0x6ca0, // 1950 0xb550, 0x55a8, 0x4da0, 0xa5b0, 0x52b8, 0x52b0, 0xa950, 0xe950, 0x6aa0, 0xad50, // 1960 0xab50, 0x4b60, 0xa570, 0xa570, 0x5260, 0xe930, 0xd950, 0x5aa8, 0x56a0, 0x96d0, // 1970 0x4ae8, 0x4ad0, 0xa4d0, 0xd268, 0xd250, 0xd528, 0xb540, 0xb6a0, 0x96d0, 0x95b0, // 1980 0x49b0, 0xa4b8, 0xa4b0, 0xb258, 0x6a50, 0x6d40, 0xada0, 0xab60, 0x9370, 0x4978, // 1990 0x4970, 0x64b0, 0x6a50, 0xea50, 0x6b28, 0x5ac0, 0xab60, 0x9368, 0x92e0, 0xc960, // 2000 0xd4a8, 0xd4a0, 0xda50, 0x5aa8, 0x56a0, 0xaad8, 0x25d0, 0x92d0, 0xc958, 0xa950, // 2010 0xb4a0, 0xb550, 0xb550, 0x55a8, 0x4ba0, 0xa5b0, 0x52b8, 0x52b0, 0xa930, 0x74a8, // 2020 0x6aa0, 0xad50, 0x4da8, 0x4b60, 0x9570, 0xa4e0, 0xd260, 0xe930, 0xd530, 0x5aa0, // 2030 0x6b50, 0x96d0, 0x4ae8, 0x4ad0, 0xa4d0, 0xd258, 0xd250, 0xd520, 0xdaa0, 0xb5a0, // 2040 0x56d0, 0x4ad8, 0x49b0, 0xa4b8, 0xa4b0, 0xaa50, 0xb528, 0x6d20, 0xada0, 0x55b0 // 2050 }; //阴历润月private static final char[] lunarLeapMonthTable = { 0x00, 0x50, 0x04, 0x00, 0x20, // 1910 0x60, 0x05, 0x00, 0x20, 0x70, // 1920 0x05, 0x00, 0x40, 0x02, 0x06, // 1930 0x00, 0x50, 0x03, 0x07, 0x00, // 1940 0x60, 0x04, 0x00, 0x20, 0x70, // 1950 0x05, 0x00, 0x30, 0x80, 0x06, // 1960 0x00, 0x40, 0x03, 0x07, 0x00, // 1970 0x50, 0x04, 0x08, 0x00, 0x60, // 1980 0x04, 0x0a, 0x00, 0x60, 0x05, // 1990 0x00, 0x30, 0x80, 0x05, 0x00, // 2000 0x40, 0x02, 0x07, 0x00, 0x50, // 2010 0x04, 0x09, 0x00, 0x60, 0x04, // 2020 0x00, 0x20, 0x60, 0x05, 0x00, // 2030 0x30, 0xb0, 0x06, 0x00, 0x50, // 2040 0x02, 0x07, 0x00, 0x50, 0x03 // 2050 };//公历与阴历的天数差;private static final char[] solarLunarOffsetTable = { 49, 38, 28, 46, 34, 24, 43, 32, 21, 40, // 1910 29, 48, 36, 25, 44, 34, 22, 41, 31, 50, // 1920 38, 27, 46, 35, 23, 43, 32, 22, 40, 29, // 1930 47, 36, 25, 44, 34, 23, 41, 30, 49, 38, // 1940 26, 45, 35, 24, 43, 32, 21, 40, 28, 47, // 1950 36, 26, 44, 33, 23, 42, 30, 48, 38, 27, // 1960 45, 35, 24, 43, 32, 20, 39, 29, 47, 36, // 1970 26, 45, 33, 22, 41, 30, 48, 37, 27, 46, // 1980 35, 24, 43, 32, 50, 39, 28, 47, 36, 26, // 1990 45, 34, 22, 40, 30, 49, 37, 27, 46, 35, // 2000 23, 42, 31, 21, 39, 28, 48, 37, 25, 44, // 2010 33, 23, 41, 31, 50, 39, 28, 47, 35, 24, // 2020 42, 30, 21, 40, 28, 47, 36, 25, 43, 33, // 2030 22, 41, 30, 49, 37, 26, 44, 33, 23, 42, // 2040 31, 21, 40, 29, 47, 36, 25, 44, 32, 22, // 2050 };private static String[]lunarMonth={ "正月","二月","三月", "四月","五月","六月", "七月","八月","九月", "十月","十一月","腊月" };private static String[]lunarDay={ "一日","二日","三日","四日","五日", "六日","七日","八日","九日","十日", "十一日","十二日","十三日","十四日","十五日", "十六日","十七日","十八日","十九日","廿日", "廿一日","廿二日","廿三日","廿四日","廿五日", "廿六日","廿七日","廿八日","廿九日","卅日" };private static int[]week={ 5,0,1,2,3,5,6,1,1,3, //1988 4,5,6,1,2,3,4,6,0,1, 2,4,5,6,0,2,3,4,5,0,1, 2,3 //2020};//1988-2050//定义生肖数组private static String[] animal={ "子鼠","丑牛","寅虎", "卯兔","辰龙","巳蛇", "午马","未羊","申猴", "酉鸡","戌狗","亥猪" }; private static String[] constellation={ "摩羯座","水瓶座","双鱼座","白羊座", "金牛座","双子座","巨蟹座","狮子座", "处女座","天秤座","天蝎座","射手座" };// constructor public calendar(){ for(int i=0,n=0;i<ChineseEra.length;i++) { ChineseEra[n++]=ChineseEraT[i%ChineseEraT.length]+ChineseEraD[i%ChineseEraD.length]; }}public static String findAnimal(int y){ P.rintln("aaa: "+y); P.rintln("(y-1983)%12++++++++++"+((y-1984)%12));return animal[((y-1984)%12)];}public static int findWeek(int y){return week[y-1988];}//judge是否为润月static boolean isLeapYear(int Y){ return (Y%4==0) && (Y%100!=0) || (Y%400==0);}//// 判断当前输入是大月还是小月并返加当前月份所有天数static int judgeMonthReturnDays(int Y, int M){ if((M == 1) || (M == 3) || (M == 5)|| (M == 7) || (M == 8) || (M == 10) || (M == 12)) return 31; else if((M == 4) || (M == 6) || (M == 9) || (M == 11)) return 30; else if(M == 2){ if(isLeapYear(Y)) return 29; else return 28; } else return 0;}//当输入数据不在1979~2020时,提示应信息public static int checkYear1(int Y){ if((Y > 1987) && (Y < 2021)) return Y; else{ return 1988; } }public static int checkYear(int Y){ if((Y > 1987) && (Y < 2021)) return Y; else{ JOptionPane.showMessageDialog(null, "你输入的年份应1979~2021,将置年为1988"); return 1988; } } //judge Month public static int checkMonth(int m){ if(m>0 && m<=13) return m; else{ JOptionPane.showMessageDialog(null, "你输入的月份应1~13,将置月为1"); return 1; } } //judge day public static int checkDay(int d){ if(d>0 && d<=31) return d; else{ JOptionPane.showMessageDialog(null, "你输入的天数应在1~31,将置年为1"); return 1; } } //农历月份转换public static String convertMonth(int m){ return lunarMonth[--m];} //农历天数转换public static String convertDay(int d){ return lunarDay[--d];} // The offset days from New Year and the day when point out in solar calendar static int getSolarLunarOffsetDays(int Y, int M, int D){ int lunarOffsetDays = 0; for(int i = 1; i < M; i++){ lunarOffsetDays += judgeMonthReturnDays(Y, i); } lunarOffsetDays += D -1; return lunarOffsetDays;}static int getLunarLeapMonth(int Y){ char M = lunarLeapMonthTable[(Y - 1901) / 2]; if(Y % 2 == 0) return (M & 0x0f); else return (M & 0xf0) >> 4;}static int getLunarMonthDays(int Y, int M){ int lunarLeapMonth = getLunarLeapMonth(Y); if((M > 12) && (M - 12 != lunarLeapMonth) || (M < 0)){ return -1; } if(M - 12 == lunarLeapMonth){ if((lunarMonthDays[Y - 1901] & (0x8000 >> lunarLeapMonth)) == 0) return 29; else return 30; } if((lunarLeapMonth > 0) && (M > lunarLeapMonth)) M++; if((lunarMonthDays[Y - 1901] & (0x8000 >> (M - 1))) == 0) return 29; else return 30;}public static String judge(int y){ int num=0; num=(y%60)-3; P.rint("Num: "+num); //System.out.println(y+"年所对应的农历为: "+ChineseEra[--num]); return ChineseEra[--num]+"年";}// Days in this year of lunar calendar static int getLunarYearDays(int Y){ int YDays = 0; int lunarLeapMonth = getLunarLeapMonth(Y); for(int i = 1; i < 13; i++) YDays += getLunarMonthDays(Y, i); if(lunarLeapMonth > 0) YDays += getLunarMonthDays(Y, lunarLeapMonth + 12); return YDays;}// The solar calendar is turned into the lunar calendar public static String solarCalendarConvertLunarCalendar(int Y, int M, int D){ int lunarCalendarDay, lunarCalendarMonth, lunarCalendarYear; int lunarOffsetDays ;//偏移的天数 int lunarLeapMonth;//阴历润月 String ganNian;//干年 Y=checkYear1(Y); M=checkMonth(M); D=checkDay(D); P.rintln(Y+" "+M+" "+D); lunarOffsetDays = getSolarLunarOffsetDays(Y, M, D); lunarLeapMonth = getLunarLeapMonth(Y); if(lunarOffsetDays < solarLunarOffsetTable[Y - 1901] ){ lunarCalendarYear = Y - 1; lunarOffsetDays = solarLunarOffsetTable[Y - 1901] -lunarOffsetDays; lunarCalendarDay = lunarOffsetDays; for(lunarCalendarMonth = 12; lunarOffsetDays >getLunarMonthDays(lunarCalendarYear,lunarCalendarMonth);lunarCalendarMonth--){ lunarCalendarDay = lunarOffsetDays; lunarOffsetDays -= getLunarMonthDays(lunarCalendarYear,lunarCalendarMonth); } if(0 ==lunarCalendarDay) lunarCalendarDay = 1; else lunarCalendarDay = getLunarMonthDays(lunarCalendarYear, lunarCalendarMonth) - lunarOffsetDays + 1; } else{ lunarCalendarYear= Y; lunarOffsetDays -= solarLunarOffsetTable[Y - 1901]; lunarCalendarDay =lunarOffsetDays + 1; for(lunarCalendarMonth = 1; lunarOffsetDays >= 0; lunarCalendarMonth++){ lunarCalendarDay= lunarOffsetDays + 1; lunarOffsetDays -= getLunarMonthDays(lunarCalendarYear, lunarCalendarMonth); if((lunarLeapMonth == lunarCalendarMonth) && (lunarOffsetDays > 0)){ lunarCalendarDay = lunarOffsetDays; lunarOffsetDays -= getLunarMonthDays(lunarCalendarYear,lunarCalendarMonth + 12); if(lunarOffsetDays <= 0){ lunarCalendarMonth += 12 + 1; break; } } } lunarCalendarMonth--; } ganNian=judge(lunarCalendarYear);//求干年 P.rintln(lunarCalendarYear+" month: "+lunarCalendarMonth+"Day: "+lunarCalendarDay); if(lunarCalendarYear==2007){ return ganNian+lunarMonth[--lunarCalendarMonth]+lunarDay[lunarCalendarDay] +" "+findAnimal(lunarCalendarYear); } return ganNian+lunarMonth[--lunarCalendarMonth]+lunarDay[--lunarCalendarDay] +" "+findAnimal(lunarCalendarYear);//}//display message`````此为测试信息 public void message(){ for(int i=0;i<ChineseEra.length;i++) { P.rintln("["+i+"]= "+ChineseEra[i]); } for(int j=0;j<lunarMonth.length;j++) { P.rintln("j["+j+"]= "+lunarMonth[j]); } for(int k=0;k<lunarDay.length;k++) { P.rintln("k["+k+"]= "+lunarDay[k]); } } public static String getConstellation(int m,int d){ switch(m){ case 1: if(d>21) { return constellation[0]; }else{ return constellation[11]; } case 2: if(d>19){ return constellation[2]; }else { return constellation[1]; } case 3: if(d>20){ return constellation[3]; }else{ return constellation[2]; } case 4: if(d>20){ return constellation[4]; }else { return constellation[3]; } case 5:if(d>20){ return constellation[5]; }else { return constellation[4]; } case 6:if(d>21){ return constellation[6]; }else { return constellation[5]; } case 7: if(d>22){ return constellation[7]; }else { return constellation[6]; } case 8:if(d>22){ return constellation[8]; }else { return constellation[7]; } case 9: if(d>22){ return constellation[9]; }else { return constellation[8]; } case 10: if(d>22){ return constellation[10]; }else { return constellation[9]; } case 11:if(d>21){ return constellation[11]; }else { return constellation[10]; } case 12:if(d>21){ P.rintln("day:+++++++"+d); return constellation[0]; }else { P.rintln("day++++++aa++++"+d); return constellation[11]; } } return null; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -