import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Lunar { private int year; private int month; private int day; private boolean leap; final static String chineseNumber[] = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊"}; final static String chineseNumber1[] = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"}; static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd日"); 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}; //====== 传回农历 y年的总天数 final private static 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年闰月的天数 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; } //====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0 final private static int leapMonth(int y) { return (int) (lunarInfo[y - 1900] & 0xf); } //====== 传回农历 y年m月的总天数 final private static int monthDays(int y, int m) { if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0) return 29; else return 30; } //====== 传回农历 y年的生肖 final public String animalsYear() { final String[] Animals = new String[]{"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"}; return Animals[(year - 4) % 12]; } //====== 传入 月日的offset 传回干支, 0=甲子 final private static String cyclicalm(int num) { final String[] Gan = new String[]{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}; final String[] Zhi = new String[]{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}; return (Gan[num % 10] + Zhi[num % 12]); } //====== 传入 offset 传回干支, 0=甲子 final public String cyclical() { int num = year - 1900 + 36; return (cyclicalm(num)); } /** * 传出y年m月d日对应的农历. * yearCyl3:农历年与1864的相差数 ? * monCyl4:从1900年1月31日以来,闰月数 * dayCyl5:与1900年1月31日相差的天数,再加40 ? * * @param cal * @return */ public Lunar(Calendar cal) { int yearCyl, monCyl, dayCyl; int leapMonth = 0; Date baseDate = null; try { baseDate = chineseDateFormat.parse("1900年1月31日"); } catch (ParseException e) { e.printStackTrace(); //To change body of catch statement use Options | File Templates. } //求出和1900年1月31日相差的天数 int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L); dayCyl = offset + 40; monCyl = 14; //用offset减去每农历年的天数 // 计算当天是农历第几天 //i最终结果是农历的年份 //offset是当年的第几天 int iYear, 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; } //农历年份 year = iYear; yearCyl = iYear - 1864; leapMonth = leapMonth(iYear); //闰哪个月,1-12 leap = false; //用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天 int iMonth, daysOfMonth = 0; for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) { //闰月 if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) { --iMonth; leap = true; daysOfMonth = leapDays(year); } else daysOfMonth = monthDays(year, iMonth); offset -= daysOfMonth; //解除闰月 if (leap && iMonth == (leapMonth + 1)) leap = false; if (!leap) monCyl++; } //offset为0时,并且刚才计算的月份是闰月,要校正 if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) { if (leap) { leap = false; } else { leap = true; --iMonth; --monCyl; } } //offset小于0时,也要校正 if (offset < 0) { offset += daysOfMonth; --iMonth; --monCyl; } month = iMonth; day = offset + 1; } public static String getChinaDayString(int day) { String chineseTen[] = {"初", "十", "廿", "卅"}; int n = day % 10 == 0 ? 9 : day % 10 - 1; if (day > 30) return ""; if (day == 10) return "初十"; else return chineseTen[day / 10] + chineseNumber1[n]; } public String toString() { return (leap ? "闰" : "") + chineseNumber[month - 1] + "月" + getChinaDayString(day); } public String getChinaWeekdayString(String weekday){ if(weekday.equals("Mon")) return "一"; if(weekday.equals("Tue")) return "二"; if(weekday.equals("Wed")) return "三"; if(weekday.equals("Thu")) return "四"; if(weekday.equals("Fri")) return "五"; if(weekday.equals("Sat")) return "六"; if(weekday.equals("Sun")) return "日"; else return ""; } public static void main(String[] args) throws ParseException{ Calendar today = Calendar.getInstance(); today.setTime(new java.util.Date());//加载当前日期 //today.setTime(chineseDateFormat.parse("2006年10月30日"));//加载自定义日期 Lunar lunar = new Lunar(today); System.out.print(lunar.cyclical()+"年");//计算输出阴历年份 System.out.println(lunar.toString());//计算输出阴历日期 System.out.println(lunar.animalsYear());//计算输出属相 System.out.println(new java.sql.Date(today.getTime().getTime()));//输出阳历日期 System.out.println("星期"+lunar.getChinaWeekdayString(today.getTime().toString().substring(0,3)));//计算输出星期几 } } <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档 </title> <mce:script language="javascript"><!-- function RunGLNL(){ var today=new Date(); var d=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); var DDDD=(today.getYear() <100 ? today.getYear()+1900:today.getYear())+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日"; DDDD = DDDD + " " + d[today.getDay()]; DDDD = DDDD+ " " + (CnDateofDateStr(today)); //DDDD = DDDD+ " " + SolarTerm(today); document.write(DDDD); } function DaysNumberofDate(DateGL){ return parseInt((Date.parse(DateGL)-Date.parse(DateGL.getYear()+"/1/1"))/86400000)+1; } function CnDateofDate(DateGL){ var CnData=new Array( 0x16,0x2a,0xda,0x00,0x83,0x49,0xb6,0x05,0x0e,0x64,0xbb,0x00,0x19,0xb2,0x5b,0x00, 0x87,0x6a,0x57,0x04,0x12,0x75,0x2b,0x00,0x1d,0xb6,0x95,0x00,0x8a,0xad,0x55,0x02, 0x15,0x55,0xaa,0x00,0x82,0x55,0x6c,0x07,0x0d,0xc9,0x76,0x00,0x17,0x64,0xb7,0x00, 0x86,0xe4,0xae,0x05,0x11,0xea,0x56,0x00,0x1b,0x6d,0x2a,0x00,0x88,0x5a,0xaa,0x04, 0x14,0xad,0x55,0x00,0x81,0xaa,0xd5,0x09,0x0b,0x52,0xea,0x00,0x16,0xa9,0x6d,0x00, 0x84,0xa9,0x5d,0x06,0x0f,0xd4,0xae,0x00,0x1a,0xea,0x4d,0x00,0x87,0xba,0x55,0x04 ); var CnMonth=new Array(); var CnMonthDays=new Array(); var CnBeginDay; var LeapMonth; var Bytes=new Array(); var I; var CnMonthData; var DaysCount; var CnDaysCount; var ResultMonth; var ResultDay; var yyyy=DateGL.getYear(); var mm=DateGL.getMonth()+1; var dd=DateGL.getDate(); if(yyyy <100) yyyy+=1900; if ((yyyy < 1997) || (yyyy > 2020)){ return 0; } Bytes[0] = CnData[(yyyy - 1997) * 4]; Bytes[1] = CnData[(yyyy - 1997) * 4 + 1]; Bytes[2] = CnData[(yyyy - 1997) * 4 + 2]; Bytes[3] = CnData[(yyyy - 1997) * 4 + 3]; if ((Bytes[0] & 0x80) != 0) {CnMonth[0] = 12;} else {CnMonth[0] = 11;} CnBeginDay = (Bytes[0] & 0x7f); CnMonthData = Bytes[1]; CnMonthData = CnMonthData < < 8; CnMonthData = CnMonthData | Bytes[2]; LeapMonth = Bytes[3]; for (I=15;I>=0;I--){ CnMonthDays[15 - I] = 29; if (((1 < < I) & CnMonthData) != 0 ){ CnMonthDays[15 - I]++;} if (CnMonth[15 - I] == LeapMonth ){ CnMonth[15 - I + 1] = - LeapMonth;} else{ if (CnMonth[15 - I] < 0 ){CnMonth[15 - I + 1] = - CnMonth[15 - I] + 1;} else {CnMonth[15 - I + 1] = CnMonth[15 - I] + 1;} if (CnMonth[15 - I + 1] > 12 ){ CnMonth[15 - I + 1] = 1;} } } DaysCount = DaysNumberofDate(DateGL) - 1; if (DaysCount <= (CnMonthDays[0] - CnBeginDay)){ if ((yyyy > 1901) && (CnDateofDate(new Date((yyyy - 1)+"/12/31")) < 0)){ ResultMonth = - CnMonth[0];} else {ResultMonth = CnMonth[0];} ResultDay = CnBeginDay + DaysCount; } else{ CnDaysCount = CnMonthDays[0] - CnBeginDay; I = 1; while ((CnDaysCount < DaysCount) && (CnDaysCount + CnMonthDays[I] < DaysCount)){ CnDaysCount+= CnMonthDays[I]; I++; } ResultMonth = CnMonth[I]; ResultDay = DaysCount - CnDaysCount; } if (ResultMonth > 0){ return ResultMonth * 100 + ResultDay;} else{return ResultMonth * 100 - ResultDay;} } function CnYearofDate(DateGL){ var YYYY=DateGL.getYear(); var MM=DateGL.getMonth()+1; var CnMM=parseInt(Math.abs(CnDateofDate(DateGL))/100); if(YYYY <100) YYYY+=1900; if(CnMM>MM) YYYY--; YYYY-=1864; return CnEra(YYYY)+"年"; } function CnMonthofDate(DateGL){ var CnMonthStr=new Array("零","正","二","三","四","五","六","七","八","九","十","冬","腊"); var Month; Month = parseInt(CnDateofDate(DateGL)/100); if (Month < 0){return "闰" + CnMonthStr[-Month] + "月";} else{return CnMonthStr[Month] + "月";} } function CnDayofDate(DateGL){ var CnDayStr=new Array("零", "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"); var Day; Day = (Math.abs(CnDateofDate(DateGL)))%100; return CnDayStr[Day]; } function DaysNumberofMonth(DateGL){ var MM1=DateGL.getYear(); MM1 <100 ? MM1+=1900:MM1; var MM2=MM1; MM1+="/"+(DateGL.getMonth()+1); MM2+="/"+(DateGL.getMonth()+2); MM1+="/1"; MM2+="/1"; return parseInt((Date.parse(MM2)-Date.parse(MM1))/86400000); } function CnEra(YYYY){ var Tiangan=new Array("甲","乙","丙","丁","戊","己","庚","辛","壬","癸"); //var Dizhi=new Array("子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)","巳(蛇)", //"午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)"); var Dizhi=new Array("子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"); return Tiangan[YYYY%10]+Dizhi[YYYY%12]; } function CnDateofDateStr(DateGL){ if(CnMonthofDate(DateGL)=="零月") return " 请调整您的计算机日期!"; else return "农历"+CnYearofDate(DateGL)+ " " + CnMonthofDate(DateGL) + CnDayofDate(DateGL); } function SolarTerm(DateGL){ var SolarTermStr=new Array( "小寒","大寒","立春","雨水","惊蛰","春分", "清明","谷雨","立夏","小满","芒种","夏至", "小暑","大暑","立秋","处暑","白露","秋分", "寒露","霜降","立冬","小雪","大雪","冬至"); var DifferenceInMonth=new Array( 1272060,1275495,1281180,1289445,1299225,1310355, 1321560,1333035,1342770,1350855,1356420,1359045, 1358580,1355055,1348695,1340040,1329630,1318455, 1306935,1297380,1286865,1277730,1274550,1271556); var DifferenceInYear=31556926; var BeginTime=new Date(1901/1/1); BeginTime.setTime(947120460000); for(;DateGL.getYear() <BeginTime.getYear();){ BeginTime.setTime(BeginTime.getTime()-DifferenceInYear*1000); } for(;DateGL.getYear()>BeginTime.getYear();){ BeginTime.setTime(BeginTime.getTime()+DifferenceInYear*1000); } for(var M=0;DateGL.getMonth()>BeginTime.getMonth();M++){ BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000); } if(DateGL.getDate()>BeginTime.getDate()){ BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000); M++; } if(DateGL.getDate()>BeginTime.getDate()){ BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000); M==23?M=0:M++; } var JQ; if(DateGL.getDate()==BeginTime.getDate()){ JQ=" 今天是 <font color='#FF9999'> <b>"+SolarTermStr[M] + " </b> </font>"; } else if(DateGL.getDate()==BeginTime.getDate()-1){ JQ=" 明天是 <font color='#FF9999'> <b>"+SolarTermStr[M] + " </b> </font>"; } else if(DateGL.getDate()==BeginTime.getDate()-2){ JQ=" 后天是 <font color='#FF9999'> <b>"+SolarTermStr[M] + " </b> </font>"; } else{ JQ=" " if(DateGL.getMonth()==BeginTime.getMonth()){ JQ+=" 本月"; } else{ JQ+=" 下月"; } JQ+=BeginTime.getDate()+"日"+" <font color='#FF9999'> <b>"+SolarTermStr[M]+" </b> </font>"; } return JQ; } function CAL1() {RunGLNL(); } function CurentTime(){ var now = new Date(); var hh = now.getHours(); var mm = now.getMinutes(); var ss = now.getTime() % 60000; ss = (ss - (ss % 1000)) / 1000; var clock = hh+':'; if (mm < 10) clock += '0'; clock += mm+':'; if (ss < 10) clock += '0'; clock += ss; return(clock); } // --></mce:script> </head> <body οnlοad="CAL1()"> </body> </html> <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %> <%@ page import="java.util.*,java.io.*,java.text.*"%> <% //世纪万年历 农历每月的天数 int everymonth[][]={ {8,0,0,0,0,0,0,0,0,0,0,0,29,30,7,1}, {0,29,30,29,29,30,29,30,29,30,30,30,29,0,8,2}, {0,30,29,30,29,29,30,29,30,29,30,30,30,0,9,3}, {5,29,30,29,30,29,29,30,29,29,30,30,29,30,10,4}, {0,30,30,29,30,29,29,30,29,29,30,30,29,0,1,5}, {0,30,30,29,30,30,29,29,30,29,30,29,30,0,2,6}, {4,29,30,30,29,30,29,30,29,30,29,30,29,30,3,7}, {0,29,30,29,30,29,30,30,29,30,29,30,29,0,4,8}, {0,30,29,29,30,30,29,30,29,30,30,29,30,0,5,9}, {2,29,30,29,29,30,29,30,29,30,30,30,29,30,6,10}, {0,29,30,29,29,30,29,30,29,30,30,30,29,0,7,11}, {6,30,29,30,29,29,30,29,29,30,30,29,30,30,8,12}, {0,30,29,30,29,29,30,29,29,30,30,29,30,0,9,1}, {0,30,30,29,30,29,29,30,29,29,30,29,30,0,10,2}, {5,30,30,29,30,29,30,29,30,29,30,29,29,30,1,3}, {0,30,29,30,30,29,30,29,30,29,30,29,30,0,2,4}, {0,29,30,29,30,29,30,30,29,30,29,30,29,0,3,5}, {2,30,29,29,30,29,30,30,29,30,30,29,30,29,4,6}, {0,30,29,29,30,29,30,29,30,30,29,30,30,0,5,7}, {7,29,30,29,29,30,29,29,30,30,29,30,30,30,6,8}, {0,29,30,29,29,30,29,29,30,30,29,30,30,0,7,9}, {0,30,29,30,29,29,30,29,29,30,29,30,30,0,8,10}, {5,30,29,30,30,29,29,30,29,29,30,29,30,30,9,11}, {0,29,30,30,29,30,29,30,29,29,30,29,30,0,10,12}, {0,29,30,30,29,30,30,29,30,29,30,29,29,0,1,1}, {4,30,29,30,29,30,30,29,30,30,29,30,29,30,2,2}, {0,29,29,30,29,30,29,30,30,29,30,30,29,0,3,3}, {0,30,29,29,30,29,30,29,30,29,30,30,30,0,4,4}, {2,29,30,29,29,30,29,29,30,29,30,30,30,30,5,5}, {0,29,30,29,29,30,29,29,30,29,30,30,30,0,6,6}, {6,29,30,30,29,29,30,29,29,30,29,30,30,29,7,7}, {0,30,30,29,30,29,30,29,29,30,29,30,29,0,8,8}, {0,30,30,30,29,30,29,30,29,29,30,29,30,0,9,9}, {5,29,30,30,29,30,30,29,30,29,30,29,29,30,10,10}, {0,29,30,29,30,30,29,30,29,30,30,29,30,0,1,11}, {0,29,29,30,29,30,29,30,30,29,30,30,29,0,2,12}, {3,30,29,29,30,29,29,30,30,29,30,30,30,29,3,1}, {0,30,29,29,30,29,29,30,29,30,30,30,29,0,4,2}, {7,30,30,29,29,30,29,29,30,29,30,30,29,30,5,3}, {0,30,30,29,29,30,29,29,30,29,30,29,30,0,6,4}, {0,30,30,29,30,29,30,29,29,30,29,30,29,0,7,5}, {6,30,30,29,30,30,29,30,29,29,30,29,30,29,8,6}, {0,30,29,30,30,29,30,29,30,29,30,29,30,0,9,7}, {0,29,30,29,30,29,30,30,29,30,29,30,29,0,10,8}, {4,30,29,30,29,30,29,30,29,30,30,29,30,30,1,9}, {0,29,29,30,29,29,30,29,30,30,30,29,30,0,2,10}, {0,30,29,29,30,29,29,30,29,30,30,29,30,0,3,11}, {2,30,30,29,29,30,29,29,30,29,30,29,30,30,4,12}, {0,30,29,30,29,30,29,29,30,29,30,29,30,0,5,1}, {7,30,29,30,30,29,30,29,29,30,29,30,29,30,6,2}, {0,29,30,30,29,30,30,29,29,30,29,30,29,0,7,3}, {0,30,29,30,30,29,30,29,30,29,30,29,30,0,8,4}, {5,29,30,29,30,29,30,29,30,30,29,30,29,30,9,5}, {0,29,30,29,29,30,30,29,30,30,29,30,29,0,10,6}, {0,30,29,30,29,29,30,29,30,30,29,30,30,0,1,7}, {3,29,30,29,30,29,29,30,29,30,29,30,30,30,2,8}, {0,29,30,29,30,29,29,30,29,30,29,30,30,0,3,9}, {8,30,29,30,29,30,29,29,30,29,30,29,30,29,4,10}, {0,30,30,30,29,30,29,29,30,29,30,29,30,0,5,11}, {0,29,30,30,29,30,29,30,29,30,29,30,29,0,6,12}, {6,30,29,30,29,30,30,29,30,29,30,29,30,29,7,1}, {0,30,29,30,29,30,29,30,30,29,30,29,30,0,8,2}, {0,29,30,29,29,30,29,30,30,29,30,30,29,0,9,3}, {4,30,29,30,29,29,30,29,30,29,30,30,30,29,10,4}, {0,30,29,30,29,29,30,29,30,29,30,30,30,0,1,5}, {0,29,30,29,30,29,29,30,29,29,30,30,29,0,2,6}, {3,30,30,30,29,30,29,29,30,29,29,30,30,29,3,7}, {0,30,30,29,30,30,29,29,30,29,30,29,30,0,4,8}, {7,29,30,29,30,30,29,30,29,30,29,30,29,30,5,9}, {0,29,30,29,30,29,30,30,29,30,29,30,29,0,6,10}, {0,30,29,29,30,29,30,30,29,30,30,29,30,0,7,11}, {5,29,30,29,29,30,29,30,29,30,30,30,29,30,8,12}, {0,29,30,29,29,30,29,30,29,30,30,29,30,0,9,1}, {0,30,29,30,29,29,30,29,29,30,30,29,30,0,10,2}, {4,30,30,29,30,29,29,30,29,29,30,30,29,30,1,3}, {0,30,30,29,30,29,29,30,29,29,30,29,30,0,2,4}, {8,30,30,29,30,29,30,29,30,29,29,30,29,30,3,5}, {0,30,29,30,30,29,30,29,30,29,30,29,29,0,4,6}, {0,30,29,30,30,29,30,30,29,30,29,30,29,0,5,7}, {6,30,29,29,30,29,30,30,29,30,30,29,30,29,6,8}, {0,30,29,29,30,29,30,29,30,30,29,30,30,0,7,9}, {0,29,30,29,29,30,29,29,30,30,29,30,30,0,8,10}, {4,30,29,30,29,29,30,29,29,30,29,30,30,30,9,11}, {0,30,29,30,29,29,30,29,29,30,29,30,30,0,10,12}, {10,30,29,30,30,29,29,30,29,29,30,29,30,30,1,1}, {0,29,30,30,29,30,29,30,29,29,30,29,30,0,2,2}, {0,29,30,30,29,30,30,29,30,29,30,29,29,0,3,3}, {6,30,29,30,29,30,30,29,30,30,29,30,29,29,4,4}, {0,30,29,30,29,30,29,30,30,29,30,30,29,0,5,5}, {0,30,29,29,30,29,29,30,30,29,30,30,30,0,6,6}, {5,29,30,29,29,30,29,29,30,29,30,30,30,30,7,7}, {0,29,30,29,29,30,29,29,30,29,30,30,30,0,8,8}, {0,29,30,30,29,29,30,29,29,30,29,30,30,0,9,9}, {3,29,30,30,29,30,29,30,29,29,30,29,30,29,10,10}, {0,30,30,30,29,30,29,30,29,29,30,29,30,0,1,11}, {8,29,30,30,29,30,29,30,30,29,29,30,29,30,2,12}, {0,29,30,29,30,30,29,30,29,30,30,29,29,0,3,1}, {0,30,29,30,29,30,29,30,30,29,30,30,29,0,4,2}, {5,30,29,29,30,29,29,30,30,29,30,30,29,30,5,3}, {0,30,29,29,30,29,29,30,29,30,30,30,29,0,6,4}, {0,30,30,29,29,30,29,29,30,29,30,30,29,0,7,5}, {4,30,30,29,30,29,30,29,29,30,29,30,29,30,8,6}, {0,30,30,29,30,29,30,29,29,30,29,30,29,0,9,7}, {0,30,30,29,30,30,29,30,29,29,30,29,30,0,10,8}, {2,29,30,29,30,30,29,30,29,30,29,30,29,30,1,9}, {0,29,30,29,30,29,30,30,29,30,29,30,29,0,2,10}, {7,30,29,30,29,30,29,30,29,30,30,29,30,30,3,11}, {0,29,29,30,29,29,30,29,30,30,30,29,30,0,4,12}, {0,30,29,29,30,29,29,30,29,30,30,29,30,0,5,1}, {5,30,30,29,29,30,29,29,30,29,30,29,30,30,6,2}, {0,30,29,30,29,30,29,29,30,29,30,29,30,0,7,3}, {0,30,29,30,30,29,30,29,29,30,29,30,29,0,8,4}, {4,30,29,30,30,29,30,29,30,29,30,29,30,29,9,5}, {0,30,29,30,29,30,30,29,30,29,30,29,30,0,10,6}, {9,29,30,29,30,29,30,29,30,30,29,30,29,30,1,7}, {0,29,30,29,29,30,29,30,30,30,29,30,29,0,2,8}, {0,30,29,30,29,29,30,29,30,30,29,30,30,0,3,9}, {6,29,30,29,30,29,29,30,29,30,29,30,30,30,4,10}, {0,29,30,29,30,29,29,30,29,30,29,30,30,0,5,11}, {0,30,29,30,29,30,29,29,30,29,29,30,30,0,6,12}, {4,29,30,30,30,29,30,29,29,30,29,30,29,30,7,1} }; //农历天干 String[] mten={"null","甲","乙","丙","丁","戊","己","庚","辛","壬","癸"}; //农历地支 String[] mtwelve={"null","子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)", "巳(蛇)","午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)"}; //农历月份 String[] mmonth={"闰","正","二","三","四","五","六", "七","八","九","十","十一","十二","月"}; //农历日 String[] mday={"null","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十", "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十", "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"}; //赋给初值 //天干地支 int ten=0; int twelve=0; //星期 int week=5; //农历日 int md=0; //农历月 int mm=0; //阳历总天数 至1900年12月21日 int total=11; //阴历总天数 int mtotal=0; //获得当日日期 //today=getdate(); GregorianCalendar today = new GregorianCalendar(); //如果没有输入,设为当日日期 String s_year=null,s_month=null; s_year=request.getParameter("year"); s_month=request.getParameter("month"); int year,month; if (s_year==null){ year = today.get(Calendar.YEAR); month = today.get(Calendar.MONTH); } else { year=Integer.valueOf(s_year).intValue(); month=Integer.valueOf(s_month).intValue(); month=month-1; } //计算到所求日期阳历的总天数-自1900年12月21日始 //先算年的和 for (int y=1901;y <year;y++){ total+=365; if (y%4==0) total ++; } //再加当年的几个月 switch (month){ case 11: total+=30; case 10: total+=31; case 9: total+=30; case 8: total+=31; case 7: total+=31; case 6: total+=30; case 5: total+=31; case 4: total+=30; case 3: total+=31; case 2: total+=28; case 1: total+=31; } //如果当年是闰年还要加一天 if (year%4==0 && month>1){ total++; } //顺便算出当月1日星期几 today.set(year,month,1); week=today.get(Calendar.DAY_OF_WEEK); //用农历的天数累加来判断是否超过阳历的天数 int flag1=0;//判断跳出循环的条件 int j=0,i=1; while (j <=120){ i=1; while (i <=13){ mtotal+=everymonth[j][i]; if (mtotal>=total){ flag1=1; break; } i++; } if (flag1==1) break; j++; } //计算所求月份1号的农历日期 md=everymonth[j][i]-(mtotal-total); //月头空开的天数 int k=week-1; //是否跨越一年 int dd=0; switch (month){ case 0: case 2: case 4: case 6: case 7: case 9: case 11: dd=31; break; case 3: case 5: case 8: case 10: dd=30; break; case 1: if (year%4==0){ dd=29; }else{ dd=28; } break; } //是否跨越一年 int ty=0; if (((everymonth[j][0]!=0 && i==13)||(everymonth[j][0]==0 && i==12)) && ((mtotal-total) <dd) )ty=1; %> <html> <head> <title>jsp万年历 </title> <mce:style type="text/css"><!-- input { font-size:9pt;} A:link {text-decoration: underline; font-size:9pt;color:000059} A:visited {text-decoration: underline; font-size:9pt;color:000059} A:active {text-decoration: none; font-size:9pt} A:hover {text-decoration:underline;color:red} body,table {font-size: 9pt} tr,td{font-size:9pt} --></mce:style><style type="text/css" mce_bogus="1"> input { font-size:9pt;} A:link {text-decoration: underline; font-size:9pt;color:000059} A:visited {text-decoration: underline; font-size:9pt;color:000059} A:active {text-decoration: none; font-size:9pt} A:hover {text-decoration:underline;color:red} body,table {font-size: 9pt} tr,td{font-size:9pt}</style> </head> <body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgcolor="#F0F6E2"> <table width=640 bgcolor="#F0F6E2"> <tr> <td > <% //打印年月抬头 out.println(" <div align=/"center/"> <b> <font size=/"2/">"+year+"年"+(month+1)+"月 </font> </b> "); if(ty==0) { out.println(" <b> <font size=/"2/">"+mten[everymonth[j][14]]+mtwelve[everymonth[j][15]]+"年 </font> </b> "); } else { out.println(" <b> <font size=/"2/">"+mten[everymonth[j][14]]+mtwelve[everymonth[j][15]]+"/"+mten[everymonth[j+1][14]]+mtwelve[everymonth[j+1][15]]+"年 </font> </b> </div>"); } %> <% out.println(" <form method=/"POST/" action=/"year.jsp/">"); //打印上一月,下一月 int ly,ny; ly=year; ny=year; int last=month; if (last==0){ last=12; ly--; } int next=month+1; if (next==12){ next=0; ny++; } if (ly>1900) out.println(" <div align='center'> <a href="/" mce_href="/""year.jsp?year="+ly+"&month="+last+"/"><<上一个月 </a> "); if (ny <=2020) out.println(" <a href="/" mce_href="/""year.jsp?year="+ny+"&month="+(next+1)+"/">下一个月>> </a> "); %> <font color="#000000">年份: <select name="year" onChange=""> <% int ii; for(ii=2020;ii>=1901;ii--){ %> <option value=" <%=ii%>" <%if(ii==year) out.println("selected");%>> <%out.println(ii);%> </option> <% } %> </select> </font> <font color="#000000">年 </font> <font color="#000000"> 月份: <select name="month" onChange=""> <% for(ii=1;ii <=12;ii++){ %> <option value=" <%=ii%>" <%if(ii==month+1) out.println("selected");%>> <%out.println(ii);%> </option> <% } %> </select> 月 </font> <input type="submit" value="查询" name="B1"> </p> </form> </td> </td> </table>