package clock;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[] = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};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;elsereturn 29;} elsereturn 0;}//====== 传回农历y年闰哪个月1-12 , 没闰传回0final 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;elsereturn 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) {@SuppressWarnings("unused") 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是当年的第几天In用java实现简单的万年历输出的代码(2009-12-14 19:08:55)转载标签:分类:计算机类itimport java.util.Scanner;public class Zuizhong{public static void main(String[] args){Scanner input=new Scanner(System.in);System.out.println("--------------------------欢迎使用万年历程序----------------------"); System.out.print("请输入年份:");int year=input.nextInt();System.out.print("\n请输入月份:");int month=input.nextInt();//打印换行符System.out.println();//计算1900年1月1日到指定年份前一年的天数int totalDays=0;//判断是否是1900后的年份if(year>=1900){for(int i=1900;i<year;i++){//判断是否闰年,闰年加366天,否则加365天if((i%4==0 && i%100!=0)||(i%400==0))totalDays+=366;else totalDays+=365;}//计算指定年份1月到指定月份1号之间的天数int daysOfMonth=0;int days;for(int i=1;i<month;i++){switch(i){case 2:if((year%4==0 && year%100!=0)|| year%400==0) days=29;else days=28;break;case 4:case 6:case 9:case 11:days=30;break;default:days=31;}daysOfMonth+=days;}//获得指定年月的天数switch(month){case 2:if((year%4==0 && year%100!=0)|| year%400==0) days=29;else days=28;break;case 4:case 6:case 9:case 11:days=30;break;default:days=31;}//1900.1.1到指定年月1号之间的总天数totalDays+=daysOfMonth;//计算指定年月1号的星期数int firstDay=(totalDays)%7+1;//上一行算出的星期数是1到7,因此要转换成0-6,即星期日=0if(firstDay==7)firstDay=0;//显示月历System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");//打印1号之前的空格for(int i=0;i<firstDay;i++)System.out.print("\t");//打印月历for(int i=1;i<=days;i++){System.out.print(i+"\t");//如果是星期六,换行if((i-1)%7+firstDay==6)System.out.println();}}else if(year>0&&year<1900){for(int i=1899;i>year;i--){//判断是否闰年,闰年加366天,否则加365天if((i%4==0 && i%100!=0)||(i%400==0))totalDays+=366;else totalDays+=365;}//计算指定年份12月到指定月份31号之后的天数int daysOfMonth=0;int days;for(int i=12;i>=month;i--){switch(i){case 2:if((year%4==0 && year%100!=0)|| year%400==0) days=29;else days=28;break;case 4:case 6:case 9:case 11:days=30;break;default:days=31;}daysOfMonth+=days;}//获得指定年月的天数switch(month){case 2:if((year%4==0 && year%100!=0)|| year%400==0) days=29;else days=28;break;case 4:case 6:case 9:case 11:days=30;break;default:days=31;}//1900.1.1到指定年月1号之间的总天数totalDays+=daysOfMonth;//计算指定年月1号的星期数int firstDay=8-(totalDays)%7;//上一行算出的星期数是1到7,因此要转换成0-6,即星期日=0if(firstDay==7)firstDay=0;if(firstDay==8)firstDay=1;//显示月历System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");//打印1号之前的空格for(int i=0;i<firstDay;i++)System.out.print("\t");//打印月历for(int i=1;i<=days;i++){System.out.print(i+"\t");//如果是星期六,换行if((i-1)%7+firstDay==6)System.out.println();}}System.out.println("\n程序结束");}}(完)在myeclipse运行效果图:--------------------------欢迎使用万年历程序---------------------- 请输入年份:2009请输入月份:12星期日星期一星期二星期三星期四星期五星期六1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31程序结束。