用java实现简单的万年历输出的代码
java日期处理输入输出(度微尔开发者联盟编辑整理)
// 年的加减
public static final int SUB_YEAR = Calendar.YEAR;
// 月加减
public static final int SUB_MONTH = Calendar.MONTH;
// 天的加减
public static int getToday() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.DATE);
}
return d;
}
/**
* 把日期转换为字符串
*/
public static String dateToString(java.util.Date date, String format) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(dateKind, amount);
return dateToString(calendar.getTime(), FORMAT_ONE);
calendar.set(year, month - 1, 1);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 获得当前日期
*/
import java.util.GregorianCalendar;
import java.util.regex.Pattern;
import mons.logging.Log;
java控制台输出万年历
import java.util.Scanner;//蔡勒(Zeller)公式(只适合于1582年10月15日之后的情形):w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1//公式中的符号含义如下: c:世纪(年的高两位数); y:年(年的低两位数);//m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,//比如2005年1月1日要看作2004年的13月1日来计算); d:日; []代表取整,即只要整数部分。
//w:星期;w对7取模得:0-星期日,1-星期一,2-星期二,3-星期三,4-星期四, 5-星期五,6-星期六//以2005年2月14日为例:c=20,y=4,m=14,d=14//w = 4 + [4/4] + [20/4] - 2*20 + [26*(14+1)/10] + 14 - 1 = 4 + 1 + 5 - 40 + 39 + 14 - 1 = 22//(除以7余1) 所以2005年2月14日是星期一。
public class PerpetualCalendarII {public static void main(String[] args) {// declare variable month,yearint month, monthDays = 0, year;int firstDayOfMonth, a, b, w;int count = 0;// declare flagboolean flag;Scanner sc = new Scanner(System.in);System.out.println("请输入年份:");year = sc.nextInt();System.out.println("请输入月份:");month = sc.nextInt();// judge year is leap year or notif ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { flag = true;} else {flag = false;}// get days of the monthswitch (month) {case 4:case 6:case 9:case 11:monthDays = 30;break;case 1:case 3:case 5:case 7:case 8:case 10:case 12:monthDays = 31;break;case 2:if (flag == true) {monthDays = 29;} else {monthDays = 28;}}// get the true yearif (month == 1) {month = 13;year -= 1;}if (month == 2) {month = 14;year -= 1;}// judge the firstDayOfMontha = year / 100;b = year % 100;// get the value of ww = b + (b / 4) + (a / 4) - 2 * a + 26 * (month + 1) / 10;if (w % 7 == 0) {firstDayOfMonth = 1;} else {firstDayOfMonth = w % 7;}System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");// output \tfor (int i = 0; i < firstDayOfMonth; i++) {System.out.print("\t");count++;}//loop daysfor (int i = 1; i <= monthDays; i++) {System.out.print(i + "\t");count++;if (count % 7 == 0) {System.out.println();}}}}。
Java万年历源代码,可显示公历、农历、系统时间、国际时间
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.table.DefaultTableModel;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Locale;import java.util.TimeZone;public class wannianli extends JFrame implements ActionListener, MouseListener {private Calendar cld = Calendar.getInstance();//获取一个Calendar类的实例对象private String[] astr = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };private DefaultTableModel dtm = new DefaultTableModel(null, astr);private JTable table = new JTable(dtm);private JScrollPane sp = new JScrollPane(table);private JButton bLastYear = new JButton("上一年");private JButton bNextYear = new JButton("下一年");private JButton bLastMonth = new JButton("上月");private JButton bNextMonth = new JButton("下月");private JPanel p1 = new JPanel(); // 设立八个中间容器,装入布局控制日期的按钮模块private JPanel p2 = new JPanel(new GridLayout(3,2));//网格布局private JPanel p3 = new JPanel(new BorderLayout());//边界布局private JPanel p4 = new JPanel(new GridLayout(2,1));private JPanel p5 = new JPanel(new BorderLayout());private JPanel p6 = new JPanel(new GridLayout(2,2));private JPanel p7 = new JPanel(new GridLayout(2,1));private JPanel p8 = new JPanel(new BorderLayout());private JComboBox timeBox = newJComboBox(TimeZone.getAvailableIDs());//对所有支持时区进行迭代,获取所有的id;private JTextField jtfYear = new JTextField(5);// jtfYeaar年份显示输入框private JTextField jtfMonth = new JTextField(2);// jtfMouth月份显示输入框private JTextField timeField=new JTextField();//各城市时间显示框private static JTextArea jta = new JTextArea(10,5);//农历显示区private JScrollPane jsp = new JScrollPane(jta);private JLabel l = new JLabel("花江小精灵:亲!你可以直接输入年月查询.");private JLabel lt = new JLabel();private JLabel ld = new JLabel();private JLabel lu = new JLabel("农历和节气");private JLabel null1=new JLabel();private int lastTime;//private String localTime = null;private String s = null;private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd 日 hh时mm分ss秒");public wannianli() {super("花江日历过去仅留追忆,未来刚生憧憬,唯有坚守本心,把握今天 ZYT 詹永堂 ");// 框架命名this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗口关闭函数this.getContentPane().setLayout(new BorderLayout(9, 10));jta.setLineWrap(true);// 长度大于分配长度时候则换行jta.setFont(new Font("黑体", Font.BOLD, 16));table.setBackground(Color.white);table.setGridColor(Color.pink);// 星期之间的网格线是灰色的table.setBackground(Color.white);table.setColumnSelectionAllowed(true);// 将table中的列设置为可选择的table.setSelectionBackground(Color.pink);// 当选定某一天时背景颜色为黑色table.setSelectionForeground(Color.GREEN);table.setBackground(new Color(184,207, 229));// 日期显示表格为浅蓝色table.setFont(new Font("黑体", Font.BOLD, 24));// 日期数字字体格式table.setRowHeight(26);// 表格的高度table.addMouseListener(this); // 鼠标监听器、lu.setFont(new Font("黑体", Font.BOLD, 22));//农历标签格氏jtfYear.addActionListener(this);// 可输入年份的文本框// 为各个按钮添加监听函数bLastYear.addActionListener(this);bNextYear.addActionListener(this);bLastMonth.addActionListener(this);bNextMonth.addActionListener(this);timeBox.addItemListener(new TimeSelectedChangedListener());// 将按钮添加到Jpane上p1.add(bLastYear);p1.add(jtfYear);// 年份输入文本框p1.add(bNextYear);p1.add(bLastMonth);p1.add(jtfMonth);p1.add(bNextMonth);p3.add(jsp, BorderLayout.SOUTH);p3.add(lu,BorderLayout.CENTER);p3.add(ld, BorderLayout.NORTH);p4.add(lt);p4.add(l);p5.add(p4, BorderLayout.SOUTH);p5.add(sp, BorderLayout.CENTER);p5.add(p1, BorderLayout.NORTH);p6.add(timeBox);p6.add(null1);p6.add(timeField);p8.add(p2,BorderLayout.CENTER);p8.add(p7,BorderLayout.SOUTH);this.getContentPane().add(p3, BorderLayout.EAST);this.getContentPane().add(p5, BorderLayout.CENTER);this.getContentPane().add(p6,BorderLayout.SOUTH);this.getContentPane().add(p8,BorderLayout.WEST);String[] strDate = DateFormat.getDateInstance().format(new Date()) .split("-");// 获取日期cld.set(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1]) - 1,0);showCalendar(Integer.parseInt(strDate[0]),Integer.parseInt(strDate[1]), cld);jtfMonth.setEditable(false);// 设置月份文本框为不可编辑jtfYear.setText(strDate[0]);jtfMonth.setText(strDate[1]);this.showTextArea(strDate[2]);ld.setFont(new Font("新宋体", Font.BOLD, 24));new Timer(lt).start();new TimeThread().start();this.setBounds(200, 200, 700, 350);this.setResizable(false);this.setVisible(true);}public void showCalendar(int localYear, int localMonth, Calendar cld) {int Days = getDaysOfMonth(localYear, localMonth) +cld.get(Calendar.DAY_OF_WEEK) -2;Object [] ai = new Object[7];lastTime = 0;for (int i = cld.get(Calendar.DAY_OF_WEEK)-1; i <= Days; i++) {ai[i%7] =String.valueOf(i-(cld.get(Calendar.DAY_OF_WEEK)-2));if (i%7 == 6){dtm.addRow(ai);ai = new Object[7];lastTime++;}}dtm.addRow(ai);}public int getDaysOfMonth(int Year, int Month) {//计算各月的天数if(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Mont h==12){return 31;}if(Month==4||Month==6||Month==9||Month==11){return 30;}if(Year%4==0&&Year%100!=0||Year%400==0)//闰年{return 29;}else {return 28;}}public void actionPerformed(ActionEvent e)//从界面上获取年月数据{if(e.getSource() == jtfYear || e.getSource() == bLastYear || e.getSource() == bNextYear ||e.getSource() == bLastMonth || e.getSource() == bNextMonth){int m, y;try//控制输入的年份正确,异常控制{if (jtfYear.getText().length() != 4){throw new NumberFormatException();}y = Integer.parseInt(jtfYear.getText());m = Integer.parseInt(jtfMonth.getText());}catch (NumberFormatException ex){JOptionPane.showMessageDialog(this, "请输入4位0-9的数字!", "年份有误", JOptionPane.ERROR_MESSAGE);return;}ld.setText("没有选择日期");for (int i = 0; i < lastTime+1; i++){ dtm.removeRow(0);}if(e.getSource() ==bLastYear){ jtfYear.setText(String.valueOf(--y)); }if(e.getSource() ==bNextYear){jtfYear.setText(String.valueOf(++y)); }if(e.getSource() == bLastMonth){if(m == 1){jtfYear.setText(String.valueOf(--y));m = 12;jtfMonth.setText(String.valueOf(m));}else{jtfMonth.setText(String.valueOf(--m));}}if(e.getSource() == bNextMonth){if(m == 12){jtfYear.setText(String.valueOf(++y));m = 1;jtfMonth.setText(String.valueOf(m));}else{jtfMonth.setText(String.valueOf(++m));}}cld.set(y, m-1, 0);showCalendar(y, m, cld);}}public void mouseClicked(MouseEvent e){jta.setText(null);int r = table.getSelectedRow();int c = table.getSelectedColumn();if (table.getValueAt(r,c) == null){ld.setText("没有选择日期");}else{this.showTextArea(table.getValueAt(r,c));}}private void showTextArea(Object selected){ld.setText(jtfYear.getText()+"年"+jtfMonth.getText()+"月"+selected+"日");}public static void main(String[] args){JFrame.setDefaultLookAndFeelDecorated(true);JDialog.setDefaultLookAndFeelDecorated(true);new wannianli();jta.setText(today());}private void updateTimeText(String timeZoneId) {if(timeZoneId != null){TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);dateFormat.setTimeZone(timeZone);Calendar calendar = Calendar.getInstance();calendar.setTimeZone(timeZone);timeField.setText(dateFormat.format(calendar.getTime()));}else{timeField.setText(null);}}private class TimeSelectedChangedListener implements ItemListener { public void itemStateChanged(ItemEvent e) {if (e.getStateChange()==ItemEvent.SELECTED) {if (e.getItem() instanceof String) {s = e.getItem().toString();}}}}private class TimeThread extends Thread{public void run(){while(true){updateTimeText(s);try{Thread.sleep(100);}catch(InterruptedException e){e.printStackTrace();}}}}class Timer extends Thread //显示系统时间{private JLabel lt;private SimpleDateFormat fy = new SimpleDateFormat(" Gyyyy.MM.dd HH:mm:ss ");public Timer(JLabel lt){this.lt=lt;}public void run(){while(true){try{lt.setText(fy.format(new Date()));this.sleep(500);}catch(InterruptedException ex){ex.printStackTrace();}}}}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, 2, 1 };final private static int[] year19 = new int[] { 0, 3, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 };final private static int[] year2000 = new int[] { 0, 3, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1 };public final static String[] nStr1 = new String[] { "", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一","十二" };private final static String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };private final static String[] Zhi = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };private final static String[] Animals = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };// 传回农历 y年的总天数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));}// 传回农历 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 static String AnimalsYear(int y) {return Animals[(y - 4) % 12];}//传入月日的offset 传回干支,0=甲子final private static String cyclicalm(int num) {return (Gan[num % 10] + Zhi[num % 12]);}// 传入 offset 传回干支, 0=甲子final public static String cyclical(int y) {int num = y - 1900 + 36;return (cyclicalm(num));}// 传出农历.year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6final 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 < 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); // 闰哪个月nongDate[6] = 0;for (i = 1; i < 13 && offset > 0; i++) {// 闰月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);}// 解除闰月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;}// 传出y年m月d日对应的农历.year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6final 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); // 闰哪个月nongDate[6] = 0;for (i = 1; i < 13 && offset > 0; i++) {// 闰月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);}// 解除闰月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"初十";if (day == 20)return"二十";if (day == 30)return"三十";int two = (int) ((day) / 10);if (two == 0)a = "初";if (two == 1)a = "十";if (two == 2)a = "廿";if (two == 3)a = "三";int one = (int) (day % 10);switch (one) {case 1:a += "一";break;case 2:a += "二";break;case 3:a += "三";break;case 4:a += "四";break;case 5:a += "五";break;case 6:a += "六";break;case 7:a += "七";break;case 8:a += "八";break;case 9:a += "九";break;}return a;}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(" \n");sToday.append(" \n");sToday.append(" \n");sToday.append(" 农历");sToday.append(cyclical(year));sToday.append('(');sToday.append(AnimalsYear(year));sToday.append(")年");sToday.append(" \n");sToday.append(" ");sToday.append(nStr1[(int) l[1]]);sToday.append("月");sToday.append(getChinaDate((int) (l[2])));return sToday.toString();} finally {sToday = null;}}private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年M月d日 EEEEE");public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) { }}。
万年历代码
{if(shuori[0][i]!=0 || zhongqi[0][i]!=0) printf("\n第%d个:",i+1);
if(shuori[0][i]!=0) {printf("\t朔日在:");count4(shuori[0][i],0);}
if (M>=11) result=result-7;
else if (M>=9) result=result-6;
else if (M>=6) result=result-5;
else if (M>=4) result=result-4;
else if (M>=2) result=result-3;
{date[2][j]=shuori[1][i];
#include "stdio.h"
#include "math.h"
int count_day(int Y,int M,int D)//根据日期,求出在年历里是第几天
{return (Y-1)*365+count(Y,M)+count2(M)+D;}
int count(int Y,int M)//到今天为止有多少2月28日;但当前月少于3月,不把当月2月28号不用加进去
else printf("%d\n",RESULT);
printf("是第%d次润年!\n",count(Y,5));
printf("是这一年的第%d天!\n",count2(M)+D);
JAVA处理日期时间常用方法
JA V A处理日期时间常用方法:java.util.CalendarCalendar 类是一个抽象类,它为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期提供了一些方法。
瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间1970 年 1 月1 日的00:00:00。
000,格里高利历的偏移量.例:Calendar cal = Calendar。
getInstance(;//使用默认时区和语言环境获得一个日历。
cal。
add(Calendar.DAY_OF_MONTH, -1;//取当前日期的前一天。
cal。
add(Calendar。
DAY_OF_MONTH, +1;//取当前日期的后一天。
//通过格式化输出日期java.text.SimpleDateFormat format = new java.text。
SimpleDateFormat(”yyyy-MM—dd";System。
out.println("Today is:”+format。
format(Calendar.getInstance(.getTime (;System。
out.println("yesterday is:"+format.format(cal.getTime(;得到2007—12-25日期: Calendar calendar = new GregorianCalendar(2007,11, 25,0,0,0;Date date = calendar.getTime(;System。
out.println("2007 Christmasis:”+format.format(date;//java月份是从0—11,月份设置时要减1。
//GregorianCalendar构造方法参数依次为:年,月—1,日,时,分,秒。
java编写万年历(1900年以后)
int year=in.nextInt();
System.out.println("请输入你想查询的月份:");
int month=in.nextInt();
int sum=0;
for(int i=1900;i<year;i++){
}
for(int i=1;i<=day1;i++,wekday++){
System.out.print(i+"\t");
if((wekday+1)%7==0){
System.out.println();
}
}
}
}
if((i%4==0&&i%100!=0)||i%400==0){
sum+=366;
}else {
sum+=365;
}
}
for(int i=1;i<month;i++){
if(i==2){
if((year%4==0&&year%100!=0)||year%400==0){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day1=31;
break;
case 4:
case 6:
case 9:
case 11:
day1=30;
break;
java日历小程序
java日历小程序简介:Java日历小程序是一个基于Java语言开发的工具,用于显示和管理日期和时间。
它提供了一种简单而直观的方式来查看、创建和编辑日历事件,并提供了一些额外的功能,如提醒、重复事件和时间段的计算等。
本文将详细介绍Java日历小程序的功能、使用方法和设计原理。
功能:1. 显示日历:Java日历小程序可以显示当前月份的日历,并以日、周或月的形式展示。
用户可以通过界面上的上下翻页按钮切换月份,方便查看不同日期的日历。
2. 创建和编辑事件:用户可以通过Java日历小程序创建新的日历事件,并为每个事件指定标题、日期、时间和地点等详细信息。
程序还提供了事件编辑功能,允许用户修改已有事件的信息。
3. 提醒功能:Java日历小程序可以设置事件的提醒功能,提醒用户在指定的时间前提醒。
用户可以选择在事件开始前几分钟、几小时或几天提醒,并可以自定义提醒方式,如弹窗、声音或邮件通知等。
4. 重复事件:用户可以为事件设置重复功能,使事件在指定的日期间隔内重复发生。
可以选择每天、每周、每月或每年重复,并可以设置重复的结束日期。
5. 时间段计算:Java日历小程序可以计算两个日期之间的时间段,如计算两个事件之间的天数、小时数或分钟数等。
用户可以选择计算方式,并获取精确的时间段结果。
使用方法:1. 下载和安装:用户可以从官方网站上下载Java日历小程序的安装包,然后按照安装向导进行安装。
安装完成后,用户可以在桌面或开始菜单中找到程序的快捷方式。
2. 打开程序:双击程序的快捷方式,Java日历小程序将启动并显示当前月份的日历。
用户可以通过界面上的按钮和菜单来使用各种功能。
3. 创建事件:用户可以点击界面上的“添加事件”按钮,弹出一个对话框,输入事件的详细信息,如标题、日期、时间和地点等。
点击“确定”按钮后,事件将被创建并显示在日历上。
4. 编辑事件:用户可以在日历上点击已有的事件,弹出一个对话框,允许用户修改事件的信息。
java万年历设计报告
JAVA大作业目录作业要求 (3)项目概述 (3)具体功能介绍 (4)设计与实现 (4)作业要求:编写一个万年历程序,要求能够查询历史时间,能够添加、查看、删除备忘录。
项目概述这个项目是一个简单的Java万年历,可以实现所有年份的公历日期的查询,并且在相应的日期做备忘录,以及可以显示当前的日期以及时间。
使用的是Oracle数据库进行连接。
具体功能介绍:(1)万年历查询:点击图形界面中的上年、下年键用来调整要查询的年份,或者可以直接在上年下年按钮直接的文本框中输入年份(负数表示公元前),以回车结束;点击上月或者下月来调整要查询的月份,然后可以看到这个月的每一天所对应的星期。
(2)Clock功能:在万年历下面显示当前的年月日时分秒,相当于一个时钟的功能。
(3)记事本功能:可以任选某年某月的某一天,单击,在右侧会出现这一天的备忘录,如果存在,则显示某年某月某日有日志记载,是否想看,否则,则在文本框中显示无记录;然后可以编辑这一天的备忘录,编辑好了之后,点击保存日志,弹出对话框某年某月某日保存日志吗,点击保存,则日志被保存,反之未被保存;若想删除某日的日志,则单击这一天,然后点击右侧的删除日志,显示删除某年某月某日的日志吗,点击是,则日志被删除。
从文件中读取备忘录的内容,用数据库进行存储和删除操作。
设计与实现(需要附全部代码,GUI自动生成代码除外):1 类的设计(继承、多态、数据结构):核心类是Month,Year,NotePad,Clock,DBAccess,CalendarPad.(其中继承用粗体,接口用粗斜体,数据结构是哈希表,用粗下划线,多态用斜体+点点短线式下划线)2 Java IO (文件访问):用的是粗体+浪线3 JDBC (数据库访问):使用Oracle数据库连接,是直连(双下划线)数据库是:create table mynotes(mydate varchar2(50) primary key,note varchar2(100) not null);4 Socket + Multi-Thread:斜体(定义在Clock中的Thread t)5 GUI (用户界面):点下划线来表示GUI用户界面6 其他功能:(无)以下是全部代码(共六个.Java文件)//对月份的选择package javaapplication13;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Month extends Box implements ActionListener//ActionListener接口{int month;JTextField showMonth=null;JButton RMonth,NMonth;CalendarPad cal;public Month(CalendarPad c){super(BoxLayout.X_AXIS);this.cal=c;showMonth=new JTextField(2);month=c.getMonth();showMonth.setEditable(false);showMonth.setForeground(Color.blue);showMonth.setFont(new Font("TimesRomn",Font.BOLD,16));NMonth=new JButton("下月");RMonth=new JButton("上月");add(RMonth);add(showMonth);add(NMonth);RMonth.addActionListener(this);NMonth.addActionListener(this);showMonth.setText(""+month);}public void setMonth(int month){if(month<=12&&month>=1){this.month=month;}else{this.month=1;}showMonth.setText(""+month);}public int getMonth(){return month;}public void actionPerformed(ActionEvent e){if(e.getSource()==RMonth){if(month>=2){month=month-1;cal.setMonth(month);cal.setCal(cal.getYear(),month);}else if(month==1){month=12;cal.setMonth(month);cal.setCal(cal.getYear(),month);}showMonth.setText(""+month);}else if(e.getSource()==NMonth){if(month<12){month=month+1;cal.setMonth(month);cal.setCal(cal.getYear(),month);}else if(month==12){month=1;cal.setMonth(month);cal.setCal(cal.getYear(),month);}showMonth.setText(""+month);}}}//对年分的选择package javaapplication13;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Year extends Box implements ActionListener//ActionListener接口{int year;JTextField showYear=null;JButton NYear,RYear;CalendarPad cal;public Year(CalendarPad c){super(BoxLayout.X_AXIS);showYear=new JTextField(4);showYear.setForeground(Color.blue);showYear.setFont(new Font("TimesRomn",Font.BOLD,14));this.cal=c;year=cal.getYear();NYear=new JButton("下年");RYear=new JButton("上年");add(RYear);add(showYear);add(NYear);showYear.addActionListener(this);RYear.addActionListener(this);NYear.addActionListener(this);}public void setYear(int year){this.year=year;showYear.setText(""+year);}public int getYear(){return year;}public void actionPerformed(ActionEvent e){if(e.getSource()==RYear){year=year-1;showYear.setText(""+year);cal.setYear(year);cal.setCal(year,cal.getMonth());}else if(e.getSource()==NYear){year=year+1;showYear.setText(""+year);cal.setYear(year);cal.setCal(year,cal.getMonth());}else if(e.getSource()==showYear){try{year=Integer.parseInt(showYear.getText());showYear.setText(""+year);cal.setYear(year);cal.setCal(year,cal.getMonth());}catch(NumberFormatException ee){showYear.setText(""+year);cal.setYear(year);cal.setCal(year,cal.getMonth());}}}}//对备忘录的操作package javaapplication13;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.event.*;import java.io.*;public class NotePad extends JPanel implements ActionListener {JTextArea text;JButton save_log,del_log;Hashtable table;JLabel mes_label;int year,month,day;File file;CalendarPad calendar;public NotePad(CalendarPad calendar)//构造函数{this.calendar=calendar;Calendar now = Calendar.getInstance();int hour=now.get(Calendar.HOUR);int minute=now.get(Calendar.MINUTE);year=calendar.getYear();month=calendar.getMonth();day=calendar.getDay();;table=calendar.getHashtable();file=calendar.getFile();mes_label=new JLabel(""+year+"年"+month+"月"+day+"日"+" "+hour+":"+minute,JLabel.CENTER);mes_label.setFont(new Font("TimesRoman",Font.BOLD,16));mes_label.setForeground(Color.MAGENTA);text=new JTextArea(10,8);save_log=new JButton("保存日志") ;del_log=new JButton("删除日志") ;save_log.addActionListener(this);del_log.addActionListener(this);setLayout(new BorderLayout());JPanel pSouth=new JPanel();add(mes_label,BorderLayout.NORTH);pSouth.add(save_log);pSouth.add(del_log);add(pSouth,BorderLayout.SOUTH);add(new JScrollPane(text),BorderLayout.CENTER);}public void actionPerformed(ActionEvent e){if(e.getSource()==save_log){saveLog(year,month,day);}else if(e.getSource()==del_log){delLog(year,month,day);}}public void setYear(int year){this.year=year;}public int getYear(){return year;}public void setMonth(int month){this.month=month;}public int getMonth(){return month;}public void setDay(int day){this.day=day;}public int getDay(){return day;}public void setMesLabel(int year,int month,int day){mes_label.setText(""+year+"年"+month+"月"+day+"日");}public void setText(String s){text.setText(s);}public void getLog(int year,int month,int day){String key=""+year+""+month+""+day;try{FileInputStream inOne=new FileInputStream(file);ObjectInputStream inTwo=new ObjectInputStream(inOne);table=(Hashtable)inTwo.readObject();inOne.close();inTwo.close();}catch(Exception ee){}if(table.containsKey(key)){String m=""+year+"年"+month+"月"+day+"这一天有日志记载,想看吗?";int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);if(ok==JOptionPane.YES_OPTION){text.setText((String)table.get(key));}else{text.setText("");}}else{text.setText("无记录");}}public void saveLog(int year,int month,int day){String 日志内容=text.getText();String key=""+year+""+month+""+day;String m=""+year+"年"+month+"月"+day+"保存日志吗?";int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);if(ok==JOptionPane.YES_OPTION){try{FileInputStream inOne=new FileInputStream(file);ObjectInputStream inTwo=new ObjectInputStream(inOne);table=(Hashtable)inTwo.readObject();inOne.close();inTwo.close();table.put(key,日志内容);FileOutputStream out=new FileOutputStream(file);ObjectOutputStream objectOut=new ObjectOutputStream(out);objectOut.writeObject(table);objectOut.close();out.close();}catch(Exception ee){}}//向数据库中添加数据。
JAVA编写日历实训心得[修改版]
第一篇:JAVA编写日历实训心得班级:**** 姓名:*** 学号:****10010127 java实验报告实验7 日历一、实验目的1. 掌握swt 的容器概念及使用方法。
2. 掌握swt 中的工具栏与菜单的使用方法。
3. 掌握swt 中的表格组件的使用。
4. 掌握swt 各种对话框的使用方法。
二、实验内容:简单日历系统的设计与实现。
三、实验步骤:1. 简单日历系统的设计与实现1) 创建java 类。
在“包资源管理器”中用鼠标右击项目“practical”,选择快捷菜单中“新建”→“visual class”;在打开的“new java visual class ”窗口中,“源文件夹”的默认值是项目的文件夹,不需要更改,“包”栏中输入程序包的名称“ch7”。
“名称”栏中输入类名“su”,在“style ”列表框中选择“swt”→“shell”,选中“public static void main(string[] args) ”复选框,使之自动创建一个main 方法,单击“完成”按钮,一个含有gui 程序界面以及代码的编辑器窗口被打开。
2) 添加组件。
用鼠标右键单击gui 程序界面的任何位置,在快捷菜单中选择“set layout”→“filllayout”。
在“palette”面板中用鼠标单击“swt containers ”中的“viewform”,在程序界面sshell 中添加viewform 容器viewform 。
从pallete 工具栏中依次单击组件:toolbar 和table ,在viewform 容器添加一个工具栏toolbar 和一个表格table ,并在工具栏toolbar 中添加4 个工具栏按钮toolitem~toolitem3;在表格中添加7 个表列tablecolumn~tablecolumn6 。
更改4 个工具栏按钮的text 属性分别为“上一年”、“上个月”、“下个月”和“下一年”;更改7 个表列的text 属性分别为“日”~“六”,如图下所示。
java 年月日的枚举值
java 年月日的枚举值在Java中,可以使用Java 8中引入的java.time包中的枚举类型来表示年、月、日。
具体来说,可以使用Year、Month和DayOfWeek枚举类型来分别表示年、月和星期几。
1. Year枚举类型:Year枚举类型用于表示年份,它可以通过静态方法Year.now()获取当前的年份,也可以使用of()方法指定特定的年份。
例如:java.Year currentYear = Year.now();Year specificYear = Year.of(2022);2. Month枚举类型:Month枚举类型用于表示月份,它包括了1月到12月的枚举常量。
可以通过静态方法Month.valueOf("JANUARY")来获取特定月份的枚举常量,也可以直接使用Month.JANUARY这样的方式来表示月份。
例如:java.Month currentMonth = Month.JANUARY;Month specificMonth = Month.valueOf("FEBRUARY");3. DayOfWeek枚举类型:DayOfWeek枚举类型用于表示星期几,它包括了MONDAY到SUNDAY的枚举常量。
可以通过静态方法DayOfWeek.valueOf("MONDAY")来获取特定星期几的枚举常量,也可以直接使用DayOfWeek.MONDAY这样的方式来表示星期几。
例如:java.DayOfWeek currentDay = DayOfWeek.MONDAY;DayOfWeek specificDay = DayOfWeek.valueOf("TUESDAY");通过使用这些枚举类型,可以方便地表示和操作年、月、日的值,使得日期和时间的处理更加直观和简洁。
同时,Java 8中引入的新的日期时间API也提供了丰富的方法来对日期进行计算、格式化和解析,枚举类型的使用使得这些操作更加方便和安全。
java日历课程设计报告
软件学院课程设计报告书课程名称面向对象课程设计设计题目专业班级学号姓名指导教师年 1 月int selYear1=Integer.parseInt(calLis.Year.getSelectedItem());int selYear = selYear1- 1900;Date d1 = new Date(selYear,selMonth,1);int day = d1.getDay();calLis.setVal(d1,day,selMonth,selYear);}}class myAction implements ActionListener{CalenderCreator calAc;int newyear;public myAction(CalenderCreator ca{calAc=ca;}//为“关闭”按钮添加监听事件,当“关闭”按钮被点击时就执行这里的部分public void actionPerformed(ActionEvent e){String s=calAc.textfield.getText();// newyear=Integer.parseInt(s);System.out.println("Year upto::::::::"+s);calAc.setYear(s);TextField tf = (TextField)e.getSource();tf.removeActionListener(this);}}4.3用图片辅助认识程序的具体操作和执行图4-1:月份和年份的效果图图4-2:星期的效果图图4-3:日期的效果图图4-4:日历整体效果图4.4附注完整程序设计import java.awt.*;import java.awt.event.*;import java.util.*;public class CalenderCreator extends Frame{Button days[]=new Button[49];Choice Month=new Choice();Choice Year=new Choice();Label lmonth=new Label("MONTH");Label lyear=new Label("Year");Label ltext=new Label("YEAR UPTO");Panel p1,p2;GregorianCalendar gc=new GregorianCalendar();int totdays;TextField textfield=new TextField(2);public CalenderCreator(){setTitle("日历");setSize(400,400);。
java获取年月日的方法
java获取年月日的方法Java是一种面向对象的编程语言,广泛应用于软件开发领域。
在Java中,获取年月日是一项常见的操作,可以通过多种方法实现。
下面,我将为大家介绍一些常用的获取年月日的方法,并解释它们的使用方式。
一、使用Java内置的日期类Java提供了多个日期类,其中最常用的是`Date`和`Calendar`类。
这两个类都可以用于获取年月日,但它们的使用方式有所不同。
1. `Date`类`Date`类是`java.util`包中的一个类,可以表示一个特定的时间点。
要获取当前的年月日,可以使用`Date`类的`getYear()`、`getMonth()`和`getDate()`方法。
需要注意的是,`getYear()`方法返回的年份是相对于1900的偏移量,所以需要将其加上1900才能得到真实的年份。
```javaDate currentDate = new Date();int year = currentDate.getYear() + 1900;int month = currentDate.getMonth() + 1;int day = currentDate.getDate();System.out.println("当前日期:" + year + "年" + month + "月" + day + "日");```2. `Calendar`类`Calendar`类是`java.util`包中的一个抽象类,它提供了丰富的日期操作方法。
与`Date`类相比,`Calendar`类更常用,因为它可以处理更复杂的日期计算。
要获取当前的年月日,可以使用`Calendar`类的`get()`方法,并传入相应的`Calendar`字段参数,如`Calendar.YEAR`、`Calendar.MONTH`和`Calendar.DAY_OF_MONTH`。
java获取年月日的方法
Java获取年月日的方法在Java编程中,我们经常需要获取当前的年、月和日。
这些信息在许多应用程序中都是必需的,例如日历应用、日期处理和数据分析等。
本文将介绍几种获取年月日的方法,帮助你在Java中轻松地处理日期。
1. 使用Java.util.Date类Java.util.Date类是Java中处理日期和时间的基本类之一。
要获取当前的年、月和日,可以使用以下代码:import java.util.Date;import java.text.SimpleDateFormat;public class Main {public static void main(String[] args) {Date date = new Date();SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");SimpleDateFormat sdfDay = new SimpleDateFormat("dd");String year = sdfYear.format(date);String month = sdfMonth.format(date);String day = sdfDay.format(date);System.out.println("Year: " + year);System.out.println("Month: " + month);System.out.println("Day: " + day);}}在上面的代码中,我们使用了SimpleDateFormat类来格式化日期。
通过传入不同的格式化模式,我们可以获取所需的日期部分。
万年历程序
万年历程序简介万年历(Perpetual Calendar)是一个可以显示公历和农历的时间工具。
在这个文档中,我们将讨论如何编写一个简单的万年历程序,以及其实现原理和功能。
功能该万年历程序将具备以下功能:1.显示当前日期2.显示当前周数3.显示当前月份的日历4.显示指定年份和月份的日历实现原理为了实现万年历程序,我们需要使用一些基本的计算方法。
以下是一些相关的计算原理:判断某年是否是闰年判断某年是否是闰年可以使用以下公式:is_leap_year = (year % 4 == 0 and year % 100 ! = 0) or (year % 400 == 0)若上述公式的值为真,则表示该年份是闰年。
计算指定年份和月份的天数可以使用以下公式来计算指定年份和月份的天数:days_in_month = [31, # 1月28 + is_leap_year, # 2月31, # 3月30, # 4月31, # 5月30, # 6月31, # 7月31, # 8月30, # 9月31, # 10月30, # 11月31 # 12月]这样,我们就可以得到每个月份的天数。
判断某年某月的第一天是星期几我们可以使用Zeller’s Congruence算法来判断某年某月的第一天是星期几。
以下是该算法的公式:h = (day + 26*(month+1)//10 + k + k//4 + j//4 + 5*j) % 7其中,h是星期几(0代表星期六,1代表星期日,依次类推),day是月份的第一天的日期,month是月份(3代表三月,4代表四月,依次类推),k是该年的前两位数,j是该年的后两位数。
实现步骤以下是实现万年历程序的步骤:1.获取当前日期和时间2.使用相关公式判断当前年份是否是闰年3.显示当前日期和时间4.计算当前周数并显示5.获取当前月份的相关信息:年份、月份、天数、星期几6.显示当前月份的日历7.提供用户界面,允许用户输入指定的年份和月份8.使用相关算法计算指定年份和月份的日历9.显示指定年份和月份的日历示例代码以下是使用Python语言实现的一个简单的万年历程序:```python import datetime获取当前日期today = datetime.date.today() current_year = today.year current_month = today.month current_day = today.day判断当前年份是否是闰年is_leap_year = (current_year % 4 == 0 and current_year % 100 != 0) or (current_year % 400 == 0)显示当前日期和时间print(。
java万年历程序代码
package pack;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Calendar;import java.util.Date;public class rili extends JFrame implements ActionListener { JButton b_today, b_query;JLabel lb_Year, lb_Month;JButton b_week[] = new JButton[7];JButton b_day[][] = new JButton[6][7];Container thisContainer;JPanel pUp;JPanel pCenter;JPanel pCenter_week, pCenter_day;JComboBox year, month;public void init() {b_today = new JButton("Today");b_query = new JButton("Query");setTitle("日历");lb_Year = new JLabel("Year");lb_Month = new JLabel("Month");year = new JComboBox();month = new JComboBox();setDate();pUp = new JPanel();pUp.add(lb_Year);pUp.add(year);pUp.add(lb_Month);pUp.add(month);pUp.add(b_today);pUp.add(b_query);b_today.addActionListener(this);b_query.addActionListener(this);pCenter = new JPanel();pCenter_week = new JPanel();b_week[0] = new JButton("星期日");b_week[1] = new JButton("星期一");b_week[2] = new JButton("星期二");b_week[3] = new JButton("星期三");b_week[4] = new JButton("星期四");b_week[5] = new JButton("星期五");b_week[6] = new JButton("星期六");b_week[0].setSize(400, 200);b_week[1].setSize(400, 200);b_week[2].setSize(400, 200);b_week[3].setSize(400, 200);b_week[4].setSize(400, 200);b_week[5].setSize(400, 200);b_week[6].setSize(400, 200);for (int i = 0; i < 7; i++) {b_week[i].setEnabled(false);pCenter_week.add(b_week[i]);}pCenter_day = new JPanel();for (int cols = 0; cols < 6; cols++) {for (int rows = 0; rows < 7; rows++) {b_day[cols][rows] = new JButton("");b_day[cols][rows].setSize(400, 200);this.pCenter_day.add(b_day[cols][rows]);}}pCenter_day.setLayout(new GridLayout(6, 7));setDay(Integer.parseInt(this.year.getSelectedItem().toString()),Integer.parseInt(this.month.getSelectedItem().toString()));// setDay(2011,2);pCenter.setLayout(new BorderLayout());pCenter.add(pCenter_week, "North");pCenter.add(pCenter_day, "Center");thisContainer = this.getContentPane();thisContainer.setLayout(new BorderLayout());thisContainer.add(pUp, "North");thisContainer.add(pCenter, "Center");this.setVisible(true);this.setResizable(false);this.pack();}public void setDate() {int year, month, day, week;Calendar cal = Calendar.getInstance();year = cal.get(Calendar.YEAR);month = cal.get(Calendar.MONTH);day = cal.get(Calendar.DA TE);week = cal.get(Calendar.WEEK_OF_YEAR);int year_temp = year - 4;for (int i = 0; i < 10; i++) {this.year.addItem(year_temp);year_temp += 1;}this.year.setSelectedIndex(4);for (int n = 0; n < 12; n++) {this.month.addItem(n + 1);}this.month.setSelectedIndex(month);}public void setDay(int Year, int Month) {int count;Calendar c = Calendar.getInstance();c.clear();c.set(Year, Month-1, 1);count = c.getActualMaximum(Calendar.DAY_OF_MONTH); // 总天数System.out.print(count);int day = c.get(Calendar.DAY_OF_WEEK) - 1; // 0为星期天,6为星期六System.out.print(day);int i = 1 - day;for (int cols = 0; cols < 6; cols++) {for (int rows = 0; rows < 7; rows++) {String st = String.valueOf(i);b_day[cols][rows].setText(st);b_day[cols][rows].setEnabled(false);if (i > 0 && i <= count)b_day[cols][rows].setVisible(true);elseb_day[cols][rows].setVisible(false);i++;}}}public void actionPerformed(ActionEvent e) {if (e.getSource() == b_query) {this.setDay(Integer.parseInt(this.year.getSelectedItem().toString()), Integer.parseInt(this.month.getSelectedItem().toString()));}if (e.getSource() == b_today) {int year, month;Calendar cal = Calendar.getInstance();year = cal.get(Calendar.YEAR);month = cal.get(Calendar.MONTH)+1;this.setDay(year,month);}}public static void main(String[] args) {rili rl = new rili();rl.init();}}。
万年历的程序代码
万年历的程序代码一年没碰Pascal了,写起来还有点吃力,希望对99级的同学有点帮助. : 程序已编译通过;附2000年年历;: 不足之处请大家批评指出.: 只限参考,不可抄袭.: Program wnl(input,output);: label: again;: var: a:char;: i:integer;: ran,year,all_days,all_years:integer;: days_month,first_month:array[1..12]of integer;: procedure display;: var: i,j,k,l,m,n,c,d,mm,nn:integer;: begin: for j:=0 to 5 do: begin: mm:=1;: nn:=1;: k:=j*2+1;: l:=j*2+2;: m:=first_month[k];: n:=first_month[l];: if m=0 then m:=7;: if n=0 then n:=7;: for i:=1 to 21 do write( );: write(No.,k:2);: for i:=1 to 32 do write( );: writeln(No.,l:2);: for i:=1 to 8 do write( );: writeln(||mon|tur|wed|thr|fri|sat|sun|| ||mon|tur|wed|thr|: fri|sat|sun||);: write( ||);: for c:=1 to 7 do if c$#@60;m then write( |) else: begin: write(mm:3,|);: mm:=mm+1;: end;: write(| ||);: for c:=1 to 7 do if c$#@60;n then write( |) else: begin: write(nn:3,|);: nn:=nn+1;: end;: writeln(|);{the first line.}: for d:=1 to 5 do: begin: write( ||);: for c:=1 to 7 do if mm>days_month[k] then write( |) else: begin: write(mm:3,|);: mm:=mm+1;: end;: write(| ||);: for c:=1 to 7 do if nn>days_month[l] then write( |) else: begin: write(nn:3,|);: nn:=nn+1;: end;: writeln(|);: end;: readln;: end;: end;: begin{main}: days_month[1]:=31;: days_month[2]:=28;: days_month[3]:=31;: days_month[4]:=30;: days_month[5]:=31;: days_month[6]:=30;: days_month[7]:=31;: days_month[8 31;: days_month[9]:=30;: days_month[10]:=31;: days_month[11]:=30;: days_month[12]:=31;: writeln(This program may help you to make a calender of the year yo: u input.);: again: year:=0;: ran:=0;: for i:=1 to 12 do first_month[i]:=0;: writeln(Please input the year number:);: readln(year);: if (year mod 400 =0) or ((year mod 100 $#@60;>0) and (year mod 4=0)) then: ran:=1;: days_month[2]:=days_month[2]+ran;: all_years:=year-1;: all_days:=all_years+(all_years div 4)-(all_years div 100)+(all_years : div 400);: first_month[1]:=(all_days+1) mod 7;: for i:=2 to 12 do first_month[i]:=(first_month[i-1]+days_month[i-1]) : mod 7;: display;: writeln(Press enter to continute,Ctrl+C to quit.);: readln;: goto again;: end.{main}: 2000年年历:: This program may help you to make a calender of the year you input. : Please input the year number:2000: No. 1 No. 2: ||mon|tur|wed|thr|fri|sat|sun|| ||mon|tur|wed|thr|fri|sat|sun||: || | | | | | 1| 2|| || | 1| 2| 3| 4| 5| 6||: || 3| 4| 5| 6| 7| 8| 9|| || 7| 8| 9| 10| 11| 12| 13||: || 10| 11| 12| 13| 14| 15| 16|| || 14| 15| 16| 17| 18| 19| 20||: || 17| 18| 19| 20| 21| 22| 23|| || 21| 22| 23| 24| 25| 26| 27||: || 24| 25| 26| 27| 28| 29| 30|| || 28| 29| | | | | ||: || 31| | | | | | || || | | | | | | ||: No. 3 No. 4: ||mon|tur|wed|thr|fri|sat|sun|| ||mon|tur|wed|thr|fri|sat|sun||: || | | 1| 2| 3| 4| 5|| || | | | | | 1| 2||: || 6| 7| 8| 9| 10| 11| 12|| || 3| 4| 5| 6| 7| 8| 9||: || 13| 14| 15| 16| 17| 18| 19|| || 10| 11| 12| 13| 14| 15| 16||: || 20| 21| 22| 23| 24| 25| 26|| || 17| 18| 19| 20| 21| 22| 23||: || 27| 28| 29| 30| 31| | || || 24| 25| 26| 27| 28| 29| 30||: || | | | | | | || || | | | | | | ||: No. 5 No. 6: ||mon|tur|wed|thr|fri|sat|sun|| ||mon|tur|wed|thr|fri|sat|sun||: || 1| 2| 3| 4| 5| 6| 7|| || | | | 1| 2| 3| 4||: || 8| 9| 10| 11| 12| 13| 14|| || 5| 6| 7| 8| 9| 10| 11||: || 15| 16| 17| 18| 19| 20| 21|| || 12| 13| 14| 15| 16| 17| 18||: || 22| 23| 24| 25| 26| 27| 28|| || 19| 20| 21| 22| 23| 24| 25||: || 29| 30| 31| | | | || || 26| 27| 28| 29| 30| | ||: || | | | | | | || || | | | | | | ||: No. 7 No. 8: ||mon|tur|wed|thr|fri|sat|sun|| ||mon|tur|wed|thr|fri|sat|sun||: || | | | | | 1| 2|| || | 1| 2| 3| 4| 5| 6||: || 3| 4| 5| 6| 7| 8| 9|| || 7| 8| 9| 10| 11| 12| 13||: || 10| 11| 12| 13| 14| 15| 16|| || 14| 15| 16| 17| 18| 19| 20|| : || 17| 18| 19| 20| 21| 22| 23|| || 21| 22| 23| 24| 25| 26| 27|| : || 24| 25| 26| 27| 28| 29| 30|| || 28| 29| 30| 31| | | ||: || 31| | | | | | || || | | | | | | ||: No. 9 No.10: ||mon|tur|wed|thr|fri|sat|sun|| ||mon|tur|wed|thr|fri|sat|sun|| : || | | | | 1| 2| 3|| || | | | | | | 1||: || 4| 5| 6| 7| 8| 9| 10|| || 2| 3| 4| 5| 6| 7| 8||: || 11| 12| 13| 14| 15| 16| 17|| || 9| 10| 11| 12| 13| 14| 15||: || 18| 19| 20| 21| 22| 23| 24|| || 16| 17| 18| 19| 20| 21| 22|| : || 25| 26| 27| 28| 29| 30| || || 23| 24| 25| 26| 27| 28| 29||: || | | | | | | || || 30| 31| | | | | ||: No.11 No.12: ||mon|tur|wed|thr|fri|sat|sun|| ||mon|tur|wed|thr|fri|sat|sun|| : || | | 1| 2| 3| 4| 5|| || | | | | 1| 2| 3||: || 6| 7| 8| 9| 10| 11| 12|| || 4| 5| 6| 7| 8| 9| 10||: || 13| 14| 15| 16| 17| 18| 19|| || 11| 12| 13| 14| 15| 16| 17|| : || 20| 21| 22| 23| 24| 25| 26|| || 18| 19| 20| 21| 22| 23| 24|| : || 27| 28| 29| 30| | | || || 25| 26| 27| 28| 29| 30| 31||: || | | | | | | || || | | | | | | ||: Press enter to continute,Ctrl+C to quit.--。
万年历程序代码
/*****************************************************4字LED点阵屏+DS1302万年历电子钟C 程序*******************************************************DS1302 接线图Vcc2 CLK I/O /RST| | | |-------------------| 8 7 6 5 || DS1302 || || 1 2 3 4 |-------------------| | | |VCC1 GND1 脚接+5V 2,3脚32768HZ晶振4脚接地5脚接S51的P02 6脚接S51的P01 7接S51的P008脚接后备电源,可以接老计算机主板上的3.6V电池,也可以通过二级管隔离接一个大容量电解电容电压在2.5V以上即可维持595连级输出数据,138行驱动。
*///(本程序引用网上程序,有点BUG经过修改后可以使用,经过修改调整加上了温度显示功能,节日提醒功能和家人生日提醒功能。
//程序没有有效简化)07电气工程,付春平!//手动添加定时器2寄存器定义sfr T2CON = 0xC8;(special function register)sfr TL2 = 0xCC;sfr TH2 = 0xCD;sfr RCAP2L = 0xCA;sfr RCAP2H = 0xCB;sbit TF2 = T2CON^7;sbit EXF2 = T2CON^6;sbit RCLK = T2CON^5;sbit TCLK = T2CON^4;sbit EXEN2 = T2CON^3;sbit TR2 = T2CON^2;sbit C_T2 = T2CON^1;sbit CP_RL2= T2CON^0;//sbit ET2 =0xAD;#include<A T89x51.H>#include <intrins.h>#define uchar unsigned char#define uint unsigned intunsigned char irtime;//红外用全局变量bit irokk;unsigned char IRcord[2];unsigned char irdata[17];unsigned char data temp_data[2] = {0x00,0x00} ;void Ircordpro(void);uchar code hanzi[]; //汉字字模uchar code hanzi1[]; //汉字字模uchar code timer[18][16]; //0~9数字uchar code sw[]; //138驱动数据void Show_word(); //待机显示按三秒间隔分别显示年、月日、星期、时分秒。
万年历程序(不要嫌程序长,功能很齐全,而且对初学者很有帮助)
#include <REG51.H>#include <intrins.h>#define uint unsigned int#define uchar unsigned charvoid Stopwatch(); //声明秒表函数void Clock(); //声明闹钟函数void Output(); //声明输出函数void important(); //声明重要函数(相当于mian()函数)void TLimit(); //声明调节温度上下限函数sbit DS1302_CLK = P1^7; //实时时钟时钟线引脚sbit DS1302_IO = P1^6; //实时时钟数据线引脚sbit DS1302_RST = P1^5; //实时时钟复位线引脚sbit wireless_1 = P3^0; //温度过上限标志sbit wireless_2 = P3^1; //温度适中标志sbit wireless_3 = P3^2; //温度过下限标志sbit fan = P3^4; //开风扇标志sbit clock=P3^5; //开关闹钟sbit setclock = P3^6; //闹钟开启控制sbit stopwatch = P3^7; //秒表开启控制sbit ACC0 = ACC^0;sbit ACC7 = ACC^7;char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; //秒,分,时到日,月,年位闪的计数sbit Set = P2^0; //模式切换键sbit Up = P2^1; //加法按钮sbit Down = P2^2; //减法按钮sbit out = P2^3; //立刻跳出调整模式按钮sbit DQ = P1^0; //温度传送数据IO口char c3,c2=0;char c=0,LowerBonud=20,UpperLimit=30;char a1=0,a2=0,a3=0,a4=0;char k[8]={'0','0',':','0','0',':','0','0'};void InitTimer0(void)//产生10ms时间间隔{TMOD = (TMOD & 0XF0) | 0X01; //定时器0工作在模式1,16Bit定时器模式TH0 = (65536 - 8000)/256;TL0 = (65536 - 8000)%256;TR0 =1; ET0 =1;//开启定时器和中断EA = 1;}char done,count,temp,flag,up_flag,down_flag; //uchar temp_value; //温度值uchar TempBuffer[5],week_value[2],h[8]; //unsigned char NL[5];void show_time(); //液晶显示程序/***********1602液晶显示部分子程序****************///Port Definitions**********************************************************sbit LcdRs = P2^5;sbit LcdRw = P2^6;sbit LcdEn = P2^7;sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口//内部等待函数************************************************************************** unsigned char LCD_Wait(void){LcdRs=0;LcdRw=1; _nop_();LcdEn=1; _nop_();LcdEn=0;return DBPort;}//向LCD写入命令或数据************************************************************#define LCD_COMMAND 0 // Command#define LCD_DATA 1 // Data#define LCD_CLEAR_SCREEN 0x01 // 清屏#define LCD_HOMING 0x02 // 光标返回原点void LCD_Write(bit style, unsigned char input){LcdEn=0;LcdRs=style;LcdRw=0; _nop_();DBPort=input; _nop_();//注意顺序LcdEn=1; _nop_();//注意顺序LcdEn=0; _nop_();LCD_Wait();}//设置显示模式************************************************************#define LCD_SHOW 0x04 //显示开#define LCD_HIDE 0x00 //显示关#define LCD_CURSOR 0x02 //显示光标#define LCD_NO_CURSOR 0x00 //无光标#define LCD_FLASH 0x01 //光标闪动#define LCD_NO_FLASH 0x00 //光标不闪动void LCD_SetDisplay(unsigned char DisplayMode){LCD_Write(LCD_COMMAND, 0x08|DisplayMode);}//设置输入模式************************************************************ #define LCD_AC_UP 0x02#define LCD_AC_DOWN 0x00 // default#define LCD_MOVE 0x01 // 画面可平移#define LCD_NO_MOVE 0x00 //defaultvoid LCD_SetInput(unsigned char InputMode){LCD_Write(LCD_COMMAND, 0x04|InputMode);}//初始化LCD************************************************************ void LCD_Initial(){LcdEn=0;LCD_Write(LCD_COMMAND,0x38); //8位数据端口,2行显示,5*7点阵LCD_Write(LCD_COMMAND,0x38);LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //开启显示, 无光标LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC递增, 画面不动}//液晶字符输入的位置************************void GotoXY(unsigned char x, unsigned char y){if(y==0)LCD_Write(LCD_COMMAND,0x80|x);if(y==1)LCD_Write(LCD_COMMAND,0x80|(x-0x40));}//将字符输出到液晶显示void Print(unsigned char *str){while(*str!='\0')//while(*str!='\0'){LCD_Write(LCD_DATA,*str);str++;}}/***********DS1302时钟部分子程序******************/typedef struct SYSTEMTIME{unsigned char Second;unsigned char Minute;unsigned char Hour;unsigned char Week;unsigned char Day;unsigned char Month;unsigned char Year;unsigned char YearH;unsigned char YearL;unsigned char day;unsigned char month;unsigned char year;unsigned char DateString[11];unsigned char NLDateString[12];unsigned char TimeString[9];}SYSTEMTIME; //定义的时间类型SYSTEMTIME CurrentTime; //#define AM(X) X#define PM(X) (X+12) // 转成24小时制#define DS1302_SECOND 0x80 //时钟芯片的寄存器位置,存放时间#define DS1302_MINUTE 0x82#define DS1302_HOUR 0x84#define DS1302_WEEK 0x8A#define DS1302_DAY 0x86#define DS1302_MONTH 0x88#define DS1302_YEAR 0x8C#define DS1302_YEARH 0xC0void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数) {unsigned char i;ACC = d;for(i=8; i>0; i--){DS1302_IO = ACC0; //相当于汇编中的RRCDS1302_CLK = 1;DS1302_CLK = 0;ACC = ACC >> 1;}}unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数){unsigned char i;for(i=8; i>0; i--){ACC = ACC >>1; //相当于汇编中的RRCACC7 = DS1302_IO;DS1302_CLK = 1;DS1302_CLK = 0;}return(ACC);}void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据{DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(ucAddr); // 地址,命令DS1302InputByte(ucDa); // 写1Byte数据DS1302_CLK = 1;DS1302_RST = 0;}unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据{unsigned char ucData;DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(ucAddr|0x01); // 地址,命令ucData = DS1302OutputByte(); // 读1Byte数据DS1302_CLK = 1;DS1302_RST = 0;return(ucData);}void DS1302_GetTime(SYSTEMTIME *Time) //获取时钟芯片的时钟数据到自定义的结构型数组{unsigned char ReadV alue;ReadValue = Read1302(DS1302_SECOND);Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_MINUTE);Time->Minute = ((ReadV alue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_HOUR);Time->Hour = ((ReadV alue&0x70)>>4)*10 + (ReadV alue&0x0F);ReadValue = Read1302(DS1302_DAY);Time->Day = ((ReadV alue&0x70)>>4)*10 + (ReadV alue&0x0F);ReadValue = Read1302(DS1302_WEEK);Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadV alue&0x0F);ReadValue = Read1302(DS1302_MONTH);Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_YEAR);Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadV alue&0x0F);Time->YearH = 20;}void DateToStr(SYSTEMTIME *Time) //将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateString[]{ if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示,输出字符串为2007/07/22{Time->DateString[0] = Time->YearH/10 + '0';Time->DateString[1] = Time->YearH%10 + '0';Time->DateString[2] = Time->Year/10 + '0';Time->DateString[3] = Time->Year%10 + '0';}else{Time->DateString[0] = ' ';Time->DateString[1] = ' ';Time->DateString[2] = ' ';Time->DateString[3] = ' ';}Time->DateString[4] = '/';if(hide_month<2){Time->DateString[5] = Time->Month/10 + '0';Time->DateString[6] = Time->Month%10 + '0';}else{Time->DateString[5] = ' ';Time->DateString[6] = ' ';}Time->DateString[7] = '/';if(hide_day<2){Time->DateString[8] = Time->Day/10 + '0';Time->DateString[9] = Time->Day%10 + '0';}else{Time->DateString[8] = ' ';Time->DateString[9] = ' ';}if(hide_week<2){week_value[0] = Time->Week%10 + '0'; //星期的数据另外放到week_value[]数组里,跟年,月,日的分开存放,因为等一下要在最后显示}else{week_value[0] = ' ';}week_value[1] = '\0';Time->DateString[10] = '\0'; //字符串末尾加'\0' ,判断结束字符}void TimeToStr(SYSTEMTIME *Time) //将时,分,秒数据转换成液晶显示字符放到数组TimeString[];{ if(hide_hour<2){Time->TimeString[0] = Time->Hour/10 + '0';Time->TimeString[1] = Time->Hour%10 + '0';h[0]=Time->TimeString[0];h[1]=Time->TimeString[1];}else{Time->TimeString[0] = ' ';Time->TimeString[1] = ' ';}Time->TimeString[2] = ':';h[2]=Time->TimeString[2];if(hide_min<2){Time->TimeString[3] = Time->Minute/10 + '0';Time->TimeString[4] = Time->Minute%10 + '0';h[3]=Time->TimeString[3];h[4]=Time->TimeString[4];}else{Time->TimeString[3] = ' ';Time->TimeString[4] = ' ';}Time->TimeString[5] = ':';h[5]=Time->TimeString[5];if(hide_sec<2){Time->TimeString[6] = Time->Second/10 + '0';Time->TimeString[7] = Time->Second%10 + '0';h[6]=Time->TimeString[6];h[7]=Time->TimeString[7];}else{Time->TimeString[6] = ' ';Time->TimeString[7] = ' ';}Time->DateString[8] = '\0';if ((a1==h[4])&&(a2==h[3])&&(a3<=h[7])&&((a4==h[6])||(a4==h[6]-1))) {clock=0;c=10;}}void Initial_DS1302(void) //时钟芯片初始化{unsigned char Second=Read1302(DS1302_SECOND);if(Second&0x80) //判断时钟芯片是否关闭{Write1302(0x8e,0x00); //写入允许Write1302(0xC0,0x20);Write1302(0x8c,0x09); //以下写入初始化时间日期:2007/07/25.星期: 3. 时间: 23:59:55 Write1302(0x88,0x06);Write1302(0x86,0x05);Write1302(0x8a,0x07); //xingqiWrite1302(0x84,0x23);Write1302(0x82,0x59);Write1302(0x80,0x55);Write1302(0x8e,0x80); //禁止写入}}/***********ds18b20子程序*************************//***********ds18b20延迟子函数(晶振12MHz )*******/void delay_18B20(unsigned int i){while(i--);}/**********ds18b20初始化函数**********************/void Init_DS18B20(void){unsigned char x=0;DQ = 1; //DQ复位delay_18B20(8); //稍做延时DQ = 0; //单片机将DQ拉低delay_18B20(80); //精确延时大于480usDQ = 1; //拉高总线delay_18B20(14);x=DQ; //稍做延时后如果x=0则初始化成功x=1则初始化失败delay_18B20(20);}/***********ds18b20读一个字节**************/unsigned char ReadOneChar(void){uchar i=0;uchar dat = 0;for (i=8;i>0;i--){DQ = 0; // 给脉冲信号dat>>=1; //数据右移一位DQ = 1; // 给脉冲信号if(DQ)dat|=0x80; //按位或,取最高位delay_18B20(4);}return(dat);}/*************ds18b20写一个字节****************/void WriteOneChar(uchar dat){unsigned char i=0;for (i=8; i>0; i--){DQ = 0;DQ = dat&0x01; //取最低位delay_18B20(5);DQ = 1; //上升沿将数据送入dat>>=1;}}/**************读取ds18b20当前温度************/void ReadTemp(void){unsigned char a=0;unsigned char b=0;unsigned char t=0;Init_DS18B20();WriteOneChar(0xCC); // 跳过读序号列号的操作WriteOneChar(0x44); // 启动温度转换delay_18B20(100); // this message is wery importantInit_DS18B20();WriteOneChar(0xCC); //跳过读序号列号的操作WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器)前两个就是温度delay_18B20(100);a=ReadOneChar(); //读取温度值低位b=ReadOneChar(); //读取温度值高位temp_value=b<<4; //左移四位,扩大16倍,变为一个字节的高四位temp_value+=(a&0xf0)>>4;//取温度值的地位的高四位,右移四位后与温度的高四位相加得到温度值if (temp_value>=UpperLimit) {wireless_1 =0;fan=1;}else {wireless_1 =1;fan=0;}if ((temp_value>LowerBonud)&&(temp_value<UpperLimit)) wireless_2 =0;else wireless_2 =1;if (temp_value<=LowerBonud) wireless_3 =0;else wireless_3 =1;}void temp_to_str() //温度数据转换成液晶字符显示{TempBuffer[0]=temp_value/10+'0'; //十位TempBuffer[1]=temp_value%10+'0'; //个位TempBuffer[2]=0xdf; //温度符号TempBuffer[3]='C';TempBuffer[4]='\0';}void Delay1ms(unsigned int count){unsigned int i,j;for(i=0;i<count;i++)for(j=0;j<120;j++){if (!stopwatch) {c++;Stopwatch();}if (!setclock) {c2++;Clock();}if ((!out)&&(!Set)) {TLimit();}if (!out) clock=1; //关闭闹钟}}/*延时子程序*/void mdelay(uint delay){ uint i;for(;delay>0;delay--)for(i=0;i<32;i++); //1ms延时.}/*****************************阳历转换阴历***********************************/ code unsigned char year_code[597]={0x04,0xAe,0x53, //1901 00x0A,0x57,0x48, //1902 30x55,0x26,0xBd, //1903 60x0d,0x26,0x50, //1904 90x0d,0x95,0x44, //1905 120x46,0xAA,0xB9, //1906 150x05,0x6A,0x4d, //1907 180x09,0xAd,0x42, //1908 210x24,0xAe,0xB6, //19090x04,0xAe,0x4A, //19100x6A,0x4d,0xBe, //19110x0A,0x4d,0x52, //19120x0d,0x25,0x46, //19130x5d,0x52,0xBA, //19140x0B,0x54,0x4e, //19150x0d,0x6A,0x43, //19160x29,0x6d,0x37, //19170x09,0x5B,0x4B, //19180x74,0x9B,0xC1, //19190x04,0x97,0x54, //19200x0A,0x4B,0x48, //19210x5B,0x25,0xBC, //19220x06,0xA5,0x50, //19230x06,0xd4,0x45, //19240x4A,0xdA,0xB8, //19250x02,0xB6,0x4d, //19260x09,0x57,0x42, //19270x24,0x97,0xB7, //19280x04,0x97,0x4A, //19290x66,0x4B,0x3e, //19300x0d,0x4A,0x51, //19310x0e,0xA5,0x46, //19320x56,0xd4,0xBA, //19330x05,0xAd,0x4e, //19340x02,0xB6,0x44, //19350x39,0x37,0x38, //19360x09,0x2e,0x4B, //19370x0C,0x95,0x53, //1939 0x0d,0x4A,0x48, //1940 0x6d,0xA5,0x3B, //1941 0x0B,0x55,0x4f, //1942 0x05,0x6A,0x45, //1943 0x4A,0xAd,0xB9, //1944 0x02,0x5d,0x4d, //1945 0x09,0x2d,0x42, //1946 0x2C,0x95,0xB6, //1947 0x0A,0x95,0x4A, //1948 0x7B,0x4A,0xBd, //1949 0x06,0xCA,0x51, //1950 0x0B,0x55,0x46, //1951 0x55,0x5A,0xBB, //1952 0x04,0xdA,0x4e, //1953 0x0A,0x5B,0x43, //1954 0x35,0x2B,0xB8, //1955 0x05,0x2B,0x4C, //1956 0x8A,0x95,0x3f, //1957 0x0e,0x95,0x52, //1958 0x06,0xAA,0x48, //1959 0x7A,0xd5,0x3C, //1960 0x0A,0xB5,0x4f, //1961 0x04,0xB6,0x45, //1962 0x4A,0x57,0x39, //1963 0x0A,0x57,0x4d, //1964 0x05,0x26,0x42, //1965 0x3e,0x93,0x35, //1966 0x0d,0x95,0x49, //1967 0x75,0xAA,0xBe, //1968 0x05,0x6A,0x51, //1969 0x09,0x6d,0x46, //1970 0x54,0xAe,0xBB, //1971 0x04,0xAd,0x4f, //1972 0x0A,0x4d,0x43, //1973 0x4d,0x26,0xB7, //1974 0x0d,0x25,0x4B, //1975 0x8d,0x52,0xBf, //1976 0x0B,0x54,0x52, //1977 0x0B,0x6A,0x47, //1978 0x69,0x6d,0x3C, //1979 0x09,0x5B,0x50, //1980 0x04,0x9B,0x45, //19810x0A,0x4B,0x4d, //19830xAB,0x25,0xC2, //19840x06,0xA5,0x54, //19850x06,0xd4,0x49, //19860x6A,0xdA,0x3d, //19870x0A,0xB6,0x51, //19880x09,0x37,0x46, //19890x54,0x97,0xBB, //19900x04,0x97,0x4f, //19910x06,0x4B,0x44, //19920x36,0xA5,0x37, //19930x0e,0xA5,0x4A, //19940x86,0xB2,0xBf, //19950x05,0xAC,0x53, //19960x0A,0xB6,0x47, //19970x59,0x36,0xBC, //19980x09,0x2e,0x50, //1999 294 0x0C,0x96,0x45, //2000 297 0x4d,0x4A,0xB8, //20010x0d,0x4A,0x4C, //20020x0d,0xA5,0x41, //20030x25,0xAA,0xB6, //20040x05,0x6A,0x49, //20050x7A,0xAd,0xBd, //20060x02,0x5d,0x52, //20070x09,0x2d,0x47, //20080x5C,0x95,0xBA, //20090x0A,0x95,0x4e, //20100x0B,0x4A,0x43, //20110x4B,0x55,0x37, //20120x0A,0xd5,0x4A, //20130x95,0x5A,0xBf, //20140x04,0xBA,0x53, //20150x0A,0x5B,0x48, //20160x65,0x2B,0xBC, //20170x05,0x2B,0x50, //20180x0A,0x93,0x45, //20190x47,0x4A,0xB9, //20200x06,0xAA,0x4C, //20210x0A,0xd5,0x41, //20220x24,0xdA,0xB6, //20230x04,0xB6,0x4A, //20240x69,0x57,0x3d, //20250x0d,0x26,0x46, //2027 0x5e,0x93,0x3A, //2028 0x0d,0x53,0x4d, //2029 0x05,0xAA,0x43, //2030 0x36,0xB5,0x37, //2031 0x09,0x6d,0x4B, //2032 0xB4,0xAe,0xBf, //2033 0x04,0xAd,0x53, //2034 0x0A,0x4d,0x48, //2035 0x6d,0x25,0xBC, //2036 0x0d,0x25,0x4f, //2037 0x0d,0x52,0x44, //2038 0x5d,0xAA,0x38, //2039 0x0B,0x5A,0x4C, //2040 0x05,0x6d,0x41, //2041 0x24,0xAd,0xB6, //2042 0x04,0x9B,0x4A, //2043 0x7A,0x4B,0xBe, //2044 0x0A,0x4B,0x51, //2045 0x0A,0xA5,0x46, //2046 0x5B,0x52,0xBA, //2047 0x06,0xd2,0x4e, //2048 0x0A,0xdA,0x42, //2049 0x35,0x5B,0x37, //2050 0x09,0x37,0x4B, //2051 0x84,0x97,0xC1, //2052 0x04,0x97,0x53, //2053 0x06,0x4B,0x48, //2054 0x66,0xA5,0x3C, //2055 0x0e,0xA5,0x4f, //2056 0x06,0xB2,0x44, //2057 0x4A,0xB6,0x38, //2058 0x0A,0xAe,0x4C, //2059 0x09,0x2e,0x42, //2060 0x3C,0x97,0x35, //2061 0x0C,0x96,0x49, //2062 0x7d,0x4A,0xBd, //2063 0x0d,0x4A,0x51, //2064 0x0d,0xA5,0x45, //2065 0x55,0xAA,0xBA, //2066 0x05,0x6A,0x4e, //2067 0x0A,0x6d,0x43, //2068 0x45,0x2e,0xB7, //20690x8A,0x95,0xBf, //20710x0A,0x95,0x53, //20720x0B,0x4A,0x47, //20730x6B,0x55,0x3B, //20740x0A,0xd5,0x4f, //20750x05,0x5A,0x45, //20760x4A,0x5d,0x38, //20770x0A,0x5B,0x4C, //20780x05,0x2B,0x42, //20790x3A,0x93,0xB6, //20800x06,0x93,0x49, //20810x77,0x29,0xBd, //20820x06,0xAA,0x51, //20830x0A,0xd5,0x46, //20840x54,0xdA,0xBA, //20850x04,0xB6,0x4e, //20860x0A,0x57,0x43, //20870x45,0x27,0x38, //20880x0d,0x26,0x4A, //20890x8e,0x93,0x3e, //20900x0d,0x52,0x52, //20910x0d,0xAA,0x47, //20920x66,0xB5,0x3B, //20930x05,0x6d,0x4f, //20940x04,0xAe,0x45, //20950x4A,0x4e,0xB9, //20960x0A,0x4d,0x4C, //20970x0d,0x15,0x41, //20980x2d,0x92,0xB5, //2099};//月份数据表code unsigned char day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};code unsigned int day_code2[3]={0x111,0x130,0x14e};/**************************************************************************** * 子函数,用于读取数据表中农历月的大月或小月,如果该月为大返回1,为小返回0****************************************************************************/ bit GetMoonDay(unsigned char month_p,unsigned short table_addr){switch (month_p){case 1:if((year_code[table_addr]&0x08)==0) return(0);else return(1);case 2:if((year_code[table_addr]&0x04)==0) return(0);else return(1);case 3:if((year_code[table_addr]&0x02)==0) return(0);else return(1);case 4:if((year_code[table_addr]&0x01)==0) return(0);else return(1);case 5:if((year_code[table_addr+1]&0x80)==0) return(0);else return(1);case 6:if((year_code[table_addr+1]&0x40)==0) return(0);else return(1);case 7:if((year_code[table_addr+1]&0x20)==0) return(0);else return(1);case 8:if((year_code[table_addr+1]&0x10)==0) return(0);else return(1);case 9:if((year_code[table_addr+1]&0x08)==0) return(0);else return(1);case 10:if((year_code[table_addr+1]&0x04)==0) return(0);else return(1);case 11:if((year_code[table_addr+1]&0x02)==0) return(0);else return(1);case 12:if((year_code[table_addr+1]&0x01)==0) return(0);else return(1);case 13:if((year_code[table_addr+2]&0x80)==0) return(0);else return(1);}return(0);}/****************************************************************************** ***************************** 函数名称:GetChinaCalendar** 功能描述:公农历转换(只允许1901-2099年)** 输入: year 公历年** month 公历月** day 公历日** p 储存农历日期地址** 输出: 1 成功** 0 失败** 作者: Campo** 修改: 赖皮** 日期: 2007年02月06日**-------------------------------------------------------------------------------------------------------** 修改人:** 日期:**------------------------------------------------------------------------------------------------------******************************************************************************* *************************/unsigned char GetChinaCalendar(unsigned int year,unsigned char month,unsigned char day){unsigned char temp1,temp2,temp3,month_p,yearH,yearL;unsigned char flag_y;unsigned short temp4,table_addr;yearH=year/100; yearL=year%100;if((yearH!=19)&&(yearH!=20)) return(0);// 定位数据表地址if(yearH==20) table_addr=(yearL+100-1)*3;else table_addr=(yearL-1)*3;// 取当年春节所在的公历月份temp1=year_code[table_addr+2]&0x60;temp1>>=5;// 取当年春节所在的公历日temp2=year_code[table_addr+2]&31;// 计算当年春年离当年元旦的天数,春节只会在公历1月或2月if(temp1==1) t emp3=temp2-1;else temp3=temp2+31-1;// 计算公历日离当年元旦的天数if (month<10) temp4=day_code1[month-1]+day-1;else temp4=day_code2[month-10]+day-1;// 如果公历月大于2月并且该年的2月为闰月,天数加1if ((month>2)&&(yearL%4==0)) temp4++;// 判断公历日在春节前还是春节后if (temp4>=temp3){temp4-=temp3;month=1;month_p=1;flag_y=0;if(GetMoonDay(month_p,table_addr)==0) temp1=29; //小月29天else temp1=30; //大小30天// 从数据表中取该年的闰月月份,如为0则该年无闰月temp2=year_code[table_addr]/16;while(temp4>=temp1){temp4-=temp1;month_p++;if(month==temp2){NL[4]='R';flag_y=~flag_y;if(flag_y==0) {month++;NL[4]=' ';}}else {month++;NL[4]=' ';}if(GetMoonDay(month_p,table_addr)==0) temp1=29;else temp1=30;}day=temp4+1;}// 公历日在春节前使用下面代码进行运算else{temp3-=temp4;if (yearL==0){yearL=100-1;yearH=19;}else yearL--;table_addr-=3;month=12;temp2=year_code[table_addr]/16;if (temp2==0) {month_p=12;NL[4]=' ';}else {month_p=13;NL[4]='R';}flag_y=0;if(GetMoonDay(month_p,table_addr)==0) temp1=29;else temp1=30;while(temp3>temp1){temp3-=temp1;month_p--;if(flag_y==0) month--;if(month==temp2) flag_y=~flag_y;if(GetMoonDay(month_p,table_addr)==0) temp1=29;else temp1=30;}day=temp1-temp3+1;}/////////////////////////NL[0]=yearH;NL[1]=yearL;NL[2]=month;NL[3]=day;return(1);}void zhuan(SYSTEMTIME *Time){GetChinaCalendar(Time->YearH*100+Time->Year,Time->Month,Time->Day);Time->day = NL[3];Time->month =NL[2];//将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateString[]if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示,输出字符串为2007/07/22{Time->NLDateString[0] = NL[0]/10 + '0';Time->NLDateString[1] = NL[0]%10 + '0';Time->NLDateString[2] = NL[1]/10 + '0';Time->NLDateString[3] = NL[1]%10 + '0';}else{Time->NLDateString[0] = ' ';Time->NLDateString[1] = ' ';Time->NLDateString[2] = ' ';Time->NLDateString[3] = ' ';}Time->NLDateString[4] = '/';if(hide_month<2){Time->NLDateString[5] =NL[4] ;Time->NLDateString[6] = Time->month/10 + '0';Time->NLDateString[7] = Time->month%10 + '0';}else{Time->NLDateString[6] = ' ';Time->NLDateString[7] = ' ';}Time->NLDateString[8] = '/';if(hide_day<2){Time->NLDateString[9] = Time->day/10 + '0';Time->NLDateString[10] = Time->day%10 + '0';}else{Time->NLDateString[9] = ' ';Time->NLDateString[10] = ' ';}Time->NLDateString[11] = '\0'; //字符串末尾加'\0' ,判断结束字符//////////////////////}//显示天干生肖void lcd_digan(){temp = NL[1]%12;switch(temp){case 0: GotoXY(3,0); Print("Chen LongYear");//龙break;case 1: GotoXY(3,0); Print("Si She ear");//蛇break;case 2: GotoXY(3,0); Print("Wu Ma Year");//马break;case 3: GotoXY(3,0); Print("Wei YangYear"); //羊break;case 4: GotoXY(3,0); Print("Chen Hou Year"); //猴break;case 5: GotoXY(3,0); Print("You Ji Year"); //鸡break;case 6: GotoXY(3,0); Print("Xu Gou Year"); //狗break;case 7: GotoXY(3,0); Print("Hai Zhu Year"); //猪break;case 8: GotoXY(3,0); Print("Zi Shu Year"); //鼠break;case 9: GotoXY(3,0); Print("Chou Niu Year"); //牛break;case 10:GotoXY(3,0); Print("Yin Hu Year"); //虎break;case 11:GotoXY(3,0); Print("Mao Tu Year"); //兔break;}}//显示天支void lcd_tianzhi(){temp = NL[1]%10;switch(temp){case 0: GotoXY(0,0); Print("Gen");//庚break;case 1: GotoXY(0,0); Print("Xin");//辛break;case 2: GotoXY(0,0); Print("Ren");//壬break;case 3: GotoXY(0,0); Print("Gui"); //癸?break;case 4: GotoXY(0,0); Print("Jia"); //甲break;case 5: GotoXY(0,0); Print("Yi"); //乙break;case 6: GotoXY(0,0); Print("Bin"); //丙break;case 7: GotoXY(0,0); Print("Din"); //丁break;case 8: GotoXY(0,0); Print("Wu"); //戊break;case 9: GotoXY(0,0); Print("Ji");//己break;}}void outkey() //跳出调整模式,返回默认显示{ uchar Second;if(out==0)//if(out==0||wireless_1==1) //{ mdelay(4);count=0;hide_sec=0,hide_min=0,hide_hour=0,hide_day=0,hide_week=0,hide_month=0,hide_year=0;Second=Read1302(DS1302_SECOND);Write1302(0x8e,0x00); //写入允许Write1302(0x80,Second&0x7f);Write1302(0x8E,0x80); //禁止写入done=0;while(out==0);//while(wireless_1==1);}}////////////////////////////////////////////////////////////////////////////////////////////////////////////void Upkey()//升序按键{Up=1;if(Up==0)//if(Up==0||wireless_2==1){mdelay(4);switch(count){case 1:temp=Read1302(DS1302_SECOND); //读取秒数temp=temp+1; //秒数加1up_flag=1; //数据调整后更新标志if((temp&0x7f)>0x59) //超过59秒,清零temp=0;break;case 2:temp=Read1302(DS1302_MINUTE); //读取分数temp=temp+1; //分数加1up_flag=1;if(temp>0x59) //超过59分,清零temp=0;break;case 3:temp=Read1302(DS1302_HOUR); //读取小时数temp=temp+1; //小时数加1up_flag=1;if(temp>0x23) //超过23小时,清零temp=0;break;case 4:temp=Read1302(DS1302_WEEK); //读取星期数temp=temp+1; //星期数加1up_flag=1;if(temp>0x7)temp=1;break;case 5:temp=Read1302(DS1302_DAY); //读取日数temp=temp+1; //日数加1up_flag=1;if(temp>0x31)temp=1;break;case 6:temp=Read1302(DS1302_MONTH); //读取月数temp=temp+1; //月数加1up_flag=1;if(temp>0x12)temp=1;break;case 7:temp=Read1302(DS1302_YEAR); //读取年数temp=temp+1; //年数加1up_flag=1;if(temp>0x85)temp=0;break;default:break;}while(Up==0);//while(wireless_2==1);}}////////////////////////////////////////////////////////////////////////////////////////////////////////////void Downkey()//降序按键{Down=1;if(Down==0)//if(Down==0||wireless_3==1){mdelay(4);switch(count){case 1:temp=Read1302(DS1302_SECOND); //读取秒数temp=temp-1; //秒数减1down_flag=1; //数据调整后更新标志if(temp==0x7f) //小于0秒,返回59秒temp=0x59;break;case 2:temp=Read1302(DS1302_MINUTE); //读取分数temp=temp-1; //分数减1down_flag=1;if(temp==-1)temp=0x59; //小于0秒,返回59秒break;case 3:temp=Read1302(DS1302_HOUR); //读取小时数temp=temp-1; //小时数减1down_flag=1;if(temp==-1)temp=0x23;break;case 4:temp=Read1302(DS1302_WEEK); //读取星期数temp=temp-1; //星期数减1down_flag=1;if(temp==0)temp=0x7;;break;case 5:temp=Read1302(DS1302_DAY); //读取日数temp=temp-1; //日数减1down_flag=1;if(temp==0)temp=31;break;case 6:temp=Read1302(DS1302_MONTH); //读取月数temp=temp-1; //月数减1down_flag=1;if(temp==0)temp=12;break;case 7:temp=Read1302(DS1302_YEAR); //读取年数temp=temp-1; //年数减1down_flag=1;if(temp==-1)temp=0x85;break;default:break;}while(Down==0);//while(wireless_3==1);}}void Setkey()//模式选择按键{Set=1;if(Set==0)//if(Set==0||wireless_4==1){mdelay(4);count=count+1; //Setkey按一次,count就加1done=1; //进入调整模式while(Set==0);//while(wireless_4==1);}}void keydone()//按键功能执行{ uchar Second;if(flag==0) //关闭时钟,停止计时{ Write1302(0x8e,0x00); //写入允许temp=Read1302(0x80);Write1302(0x80,temp|0x80);Write1302(0x8e,0x80); //禁止写入flag=1;。
JAVA万年历系统课程设计报告附源码
2013-2014学年第二学期《面向对象程序设计》课程设计报告题目:万年历系统专业:计算机科学与技术班级:姓名:学号:指导教师:成绩:计算机与信息工程系2014年6月6日目录1设计内容及要求设计内容万年历,实际上就是记录一定时间范围内(比如100年或更多)的具体阳历或阴历的日期的年历,方便有需要的人查询使用。
在我设计的万年历中主要有:(1)使用图形用户界面来查询用用户所需的日期信息,符合日常软件使用规范。
(2)按月份查询,实现了显示查询1901~2100年某月所有日期的阴阳历对照。
(3)并且添加了重大节日与星座等信息,界面采用日常的星期与月份结合的格式,方便查看与使用。
(4)编写万年历的课程设计,是为了使同学们更加了解高级程序设计语言的结构,掌握基本的程序设计过程和技巧,掌握基本的分析问题和利用计算机求解问题的能力,具备初步的高级语言程序设计能力。
为后续各门计算机课程的学习和毕业设计打下坚实基础。
设计任务及具体要求利用JAVA语言编写的万年历系统采用了多种JAVA语句来实现多种功能。
用户可以通过本程序的applet运行界面来查找一整年某月的农历和阳历,可以查找用户所想了解的某一天具体为星期几,并且可以看到一些重大节日的具体时间。
要求:满足设计万年历系统的目的,即当用户查询年份与月份时,系统就要将这一年的某一月份的阴历与阳历全部显示出来,并且附带这一月份的重大节日。
当用户随意改动年份或月份时系统自动显示与星期对应的日期。
用户点击日期时系统会显示具体星座信息。
2概要设计程序设计思路(1)总天数的算法:首先用if语句判断定义年到输入年之间每一年是否为闰年,是闰年,该年的总天数为366,否则,为365。
然后判断输入的年是否为定义年,若是,令总天数S=1,否则,用累加法计算出定义年到输入年之间的总天数,再把输入年的一月到要输出的月份之间的天数累加起来,若该月是闰年中的月份并且该月还大于二月,再使总天数加1,否则,不加,既算出从定义年一月一日到输出年的该月一日的总天数。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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程序结束。