AidyCalender(java代码编写万年历)
用java实现简单的万年历输出的代码
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程序结束。
JAVA课程设计 万年历 源代码
测试用例设计:根据 需求文档和功能描述, 设计出能够覆盖所有 功能的测试用例
测试工具:使用JUnit 等测试框架进行单元 测试,使用Selenium 等工具进行UI测试
测试结果分析:根 据测试结果,分析 代码存在的问题, 并进行修改和优化
集成测试:验证各个模块之间的接口是否正确,数据传输是否正常 性能测试:测试系统的响应时间、吞吐量、资源利用率等性能指标
提醒功能:用户可以设置提醒功能,在节日或假期到来之前,系统会自动提醒用户。
删除事件:用户可以删除不 再需要的事件
编辑事件:用户可以对已添加 的事件进行编辑,如修改事件 名称、时间等
添加事件:用户可以在万年历 中添加新的事件,如生日、纪 念日等
查询事件:用户可以查询特定 日期或时间段内的事件,如查
界面显示:万年历界面将显示年、 月、日、星期等信息,用户可以通 过点击相应的按钮来切换日期。
添加标题
添加标题
添加标题
添加标题
系统响应:当用户输入日期后,系统 将根据输入的日期显示相应的万年历 信息,包括年、月、日、星期等信息。
用户操作:用户可以通过点击相应 的按钮来切换日期,系统将根据用 户的操作显示相应的万年历信息。
添加标题
界面设计:简洁明了,易于阅读
添加标题
添加标题
交互性:用户可以选择查看不同日 期的日历信息
功能描述:在万年历中,用户可以选择标注节日和假期,以便于查看和提醒。
节日标注:用户可以在万年历中设置自己喜欢的节日,如春节、中秋节等,系统会自动 标注这些节日。
假期标注:用户可以在万年历中设置自己的假期,如年假、病假等,系统会自动标注这 些假期。
,a click to unlimited possibilities
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) { }}。
java万年历源代码
java万年历源代码第一个类:chaxun.javapackage wannianli;import java.util.*;public class chaxun {public static void main(String[] args) {Scanner input =new Scanner(System.in);String answer="y";for(;answer.equals("y");){week cn=new week();cn.weekDay();//调用方法System.out.print("\是否继续?");answer=input.next();}}}第二个类:tianshu.javapackage wannianli;import java.util.*;public class tianshu {int totalDay;//总共的天数int yueTian;//每月的天数public void jsts(){int days=0;//输入月份到当年的天数System.out.println("*************************************欢迎使用万年历*************************************");Scanner input=new Scanner(System.in);System.out.print("请输入年份:");//从键盘输入年份int year=input.nextInt();System.out.print("请输入月份:");//从键盘输入月份int yue=input.nextInt();/**判断每月的天数*/for(int index=1;index<=yue;index++){if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==1 2){//满足闰年的条件yueTian=31;}elseif(yue==2&&((year%4==0)&&(!(year%100==0))||(year%400== 0))){yueTian=29;}elseif(yue==2&&(!((year%4==0)&&(!(year%100==0))||(year%400= =0)))){yueTian=28;}else if(yue==4||yue==6||yue==9||yue==11){yueTian=30;}else{System.out.print("输入的月份不正确");}if(index<=yue){days=days+yueTian;}}/**判断是否是闰年*/for(int i=100;i<year;i++){< bdsfid="116" p=""></year;i++){<>if ((year%4==0)&&(!(year%100==0))||(year%400==0)){//满足闰年的条件totalDay=totalDay+366;}else{totalDay=totalDay+365;}}totalDay=totalDay+days;//System.out.println(totalDay+"天");//return totalDay+yueTian;}}第三个类:week.jvapackage wannianli;public class week {public void weekDay() {int monDay;//星期几tianshu cn=new tianshu();cn.jsts();int week=1+cn.totalDay%7;//System.out.println(""+week);if (week==7){// 求当月第一天monDay=0;// 周日}else{monDay=week;}/* 输出日历*/System.out.println("星期日\星期一\星期二\星期三\星期四\星期五\星期六");for(int nullNo=0;nullNo<monday;nullno++){< bdsfid="145"p=""></monday;nullno++){<>System.out.print("\");// 输出空格}for(int i=1;i<=cn.yueTian;i++){System.out.print(i+"\");//输出每月的号数if((cn.totalDay + i - 1) % 7 == 5){// 如果当天为周六,输出换行System.out.println();}}} }。
用Java程序编写万年历程序的设计报告
用Java程序编写万年历程序的设计报告学习中心(点):泾阳学习中心专业:计算机科学与技术层次:专升本姓名:李永固批次: 112目录一、设计分析 (1)1、需求分析 (1)2、功能设计 (1)3、概要设计 (1)3.1程序设计思路 (1)3.2程序运行界面 (2)二、程序结构 (3)三、各模块的功能及程序说明 (4)1、初始化组件 (4)2、初始化数据 (4)3、绘制程序界面 (5)四、源程序 (6)五、操作方法 (14)六、试验结果 (14)七、设计体会 (16)用Java程序编写万年历程序的设计报告一、设计分析1、需求分析本程序的要求为:1.使用图形用户界面;2.实现日期与星期的查询。
2、功能设计本程序要构建的万年历程序,其功能有以下几个方面:(1)通过网页形式运行,实现图形界面。
(2)能以月历形式显示日期与星期。
(3)支持用户自己输入年份,并提供月份的下拉形式菜单来选择月份。
(4)通过点击“更新”来刷新日历。
3、概要设计3.1程序设计思路1、总天数的算法:首先用if语句判断定义年到输入年之间每一年是否为闰年,是闰年,该年的总天数为366,否则,为365。
然后判断输入的年是否为定义年,若是,令总天数S=1,否则,用累加法计算出定义年到输入年之间的总天数,再把输入年的一月到要输出的月份之间的天数累加起来,若该月是闰年中的月份并且该月还大于二月,再使总天数加1,否则,不加,既算出从定义年一月一日到输出年的该月一日的总天数。
2、输出月份第一天为星期几的算法:使总天数除以7取余加2得几既为星期几,若是7,则为星期日。
3、算出输出月份第一天为星期几的算法:算出输出月份第一天为星期几后,把该日期以前的位置用空格补上,并总该日起一次输出天数直到月底,该月中的天数加上该月一日为星期几的数字再除以7得0换行,即可完整的输出该月的日历。
4、如果年份小于1582年则程序不予判断。
3.2程序运行界面二、程序结构流程图:三、各模块的功能及程序说明1、初始化组件import java.applet.Applet;import java.awt.*;import java.util.*;2、初始化数据public class CalendarApplet extends Applet{static final int TOP = 70; //顶端距离static final int CELLWIDTH=50,CELLHEIGHT = 30; //单元格尺寸static final int MARGIN = 3; //边界距离static final int FEBRUARY = 1;TextField tfYear = new TextField("2004", 5); //显示年份的文本域Choice monthChoice = new Choice(); //月份选择下拉框Button btUpdate = new Button("更新"); //更新按钮GregorianCalendar calendar=new GregorianCalendar(); //日历对象Font smallFont = new Font("TimesRoman", Font.PLAIN, 15); //显示小字体Font bigFont = new Font("TimesRoman", Font.BOLD, 50); //显示大字体String days[] = {"星期日", "星期一", "星期二", "星期三","星期四", "星期五", "星期六"};String months[] = {"一月", "二月", "三月", "四月","五月", "六月", "七月", "八月", "九月","十月", "十一月", "十二月"};int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //每个月的天数int searchMonth,searchYear; //查询的年份及月份3、绘制程序界面public void init(){setBackground(Color.white); //设置背景颜色searchMonth = calendar.get(Calendar.MONTH); //得到系统年份searchYear = calendar.get(Calendar.YEAR); //得到系统月份add(new Label(" 年:")); //增加组件到ApplettfYear.setText(String.valueOf(searchYear)); //设置文本域文字add(tfYear);add(new Label(" 月:"));monthChoice.setFont(smallFont); //设置月份选择下拉框的显示字体for (int i = 0; i < 12; i++) {monthChoice.addItem(months[i]); //增加下拉框选项}monthChoice.select(searchMonth); //设置下拉框当前选择项add(monthChoice);add(btUpdate);int componentCount=this.getComponentCount(); //得到Applet中的组件数量for (int i=0;i<componentCount;i++){getComponent(i).setFont(smallFont); //设置所有组件的显示字体}}四、源程序import java.applet.Applet;import java.awt.*;import java.util.*;public class CalendarApplet extends Applet{static final int TOP = 70; //顶端距离static final int CELLWIDTH=50,CELLHEIGHT = 30; //单元格尺寸static final int MARGIN = 3; //边界距离static final int FEBRUARY = 1;TextField tfYear = new TextField("2004", 5); //显示年份的文本域Choice monthChoice = new Choice(); //月份选择下拉框Button btUpdate = new Button("更新"); //更新按钮GregorianCalendar calendar=new GregorianCalendar(); //日历对象Font smallFont = new Font("TimesRoman", Font.PLAIN, 15); //显示小字体Font bigFont = new Font("TimesRoman", Font.BOLD, 50); //显示大字体String days[] = {"星期日", "星期一", "星期二", "星期三","星期四", "星期五", "星期六"};String months[] = {"一月", "二月", "三月", "四月","五月", "六月", "七月", "八月", "九月","十月", "十一月", "十二月"};int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //每个月的天数int searchMonth,searchYear; //查询的年份及月份public void init(){setBackground(Color.white); //设置背景颜色searchMonth = calendar.get(Calendar.MONTH); //得到系统年份searchYear = calendar.get(Calendar.YEAR); //得到系统月份add(new Label(" 年:")); //增加组件到ApplettfYear.setText(String.valueOf(searchYear)); //设置文本域文字add(tfYear);add(new Label(" 月:"));monthChoice.setFont(smallFont); //设置月份选择下拉框的显示字体for (int i = 0; i < 12; i++) {monthChoice.addItem(months[i]); //增加下拉框选项}monthChoice.select(searchMonth); //设置下拉框当前选择项add(monthChoice);add(btUpdate);int componentCount=this.getComponentCount(); //得到Applet中的组件数量for (int i=0;i<componentCount;i++){getComponent(i).setFont(smallFont); //设置所有组件的显示字体}}public void paint(Graphics g){FontMetrics fontMetric; //显示字体的FontMetrics对象int fontAscent;int dayPos;int totalWidth, totalHeight; //总的宽度,高度int numRows; //行数int xNum, yNum; //水平和垂直方向单元格数量int numDays;String dayStr; //显示天数字符串int margin;g.setColor(Color.lightGray); //设置当前颜色g.setFont(bigFont); //设置当前使用字体g.drawString(searchYear+"年",60,TOP+70); //绘制字符串g.drawString((searchMonth+1)+"月",200,TOP+130);g.setColor(Color.black);g.setFont(smallFont);fontMetric = g.getFontMetrics(); //获取变量初值fontAscent = fontMetric.getAscent();dayPos = TOP + fontAscent / 2;totalWidth = 7 * CELLWIDTH; //得到总的表格宽度for (int i = 0; i < 7; i++) {g.drawString(days[i], (CELLWIDTH-fontMetric.stringWidth(days[i]))/2 + i*CELLWIDTH,dayPos-20); //绘制表格标题栏}numRows = getNumberRows(searchYear, searchMonth); //计算需要的行的数量totalHeight = numRows * CELLHEIGHT; //得到总的表格高度for (int i = 0; i <= totalWidth; i += CELLWIDTH) {g.drawLine(i, TOP , i, TOP+ totalHeight); //绘制表格线}for (int i = 0, j = TOP ; i <= numRows; i++, j += CELLHEIGHT) {g.drawLine(0, j, totalWidth, j); //绘制表格线}xNum = (getFirstDayOfMonth(searchYear, searchMonth) + 1) * CELLWIDTH - MARGIN;yNum = TOP + MARGIN + fontAscent;numDays = daysInMonth[searchMonth] + ((calendar.isLeapYear(searchYear) && (searchMonth == FEBRUARY)) ? 1 : 0);for (int day = 1; day <= numDays; day++) {dayStr = Integer.toString(day);g.drawString(dayStr, xNum - fontMetric.stringWidth(dayStr), yNum); //绘制字符串xNum += CELLWIDTH;if (xNum > totalWidth) {xNum = CELLWIDTH - MARGIN;yNum += CELLHEIGHT;}}}public boolean action(Event e, Object o){int searchYearInt;if (e.target==btUpdate){searchMonth = monthChoice.getSelectedIndex(); //得到查询月份searchYearInt = Integer.parseInt(tfYear.getText(), 10); //得到查询年份if (searchYearInt > 1581) {searchYear = searchYearInt;}repaint(); //重绘屏幕return true;}return false;}private int getNumberRows(int year, int month) { //得到行数量int firstDay;int numCells;if (year < 1582) { //年份小于1582年,则返回-1return (-1);}if ((month < 0) || (month > 11)) {return (-1);}firstDay = getFirstDayOfMonth(year, month); //计算月份的第一天if ((month == FEBRUARY) && (firstDay == 0) && !calendar.isLeapYear(year)) { return 4;}numCells = firstDay + daysInMonth[month];if ((month == FEBRUARY) && (calendar.isLeapYear(year))) {numCells++;}return ((numCells <= 35) ? 5 : 6); //返回行数}private int getFirstDayOfMonth(int year, int month) { //得到每月的第一天int firstDay;int i;if (year < 1582) { //年份小于1582年,返回-1return (-1);}if ((month < 0) || (month > 11)) { //月份数错误,返回-1return (-1);}firstDay = getFirstDayOfYear(year); //得到每年的第一天for (i = 0; i < month; i++) {firstDay += daysInMonth[i]; //计算每月的第一天}if ((month > FEBRUARY) && calendar.isLeapYear(year)) {firstDay++;}return (firstDay % 7);}private int getFirstDayOfYear(int year){ //计算每年的第一天int leapYears;int hundreds;int fourHundreds;int first;if (year < 1582) { //如果年份小于1582年return (-1); //返回-1}leapYears = (year - 1581) / 4;hundreds = (year - 1501) / 100;leapYears -= hundreds;fourHundreds = (year - 1201) / 400;leapYears += fourHundreds;first=5 + (year - 1582) + leapYears % 7; //得到每年第一天return first;}}五、操作方法1、出提示语句,并接受用户输入的年、月。
JAVA控制台万年历代码
days += 31;
yearDay = 31;
} else if (month == 2) {
if (years % 4 == 0 && years % 100 != 0
+ work[No] + "】\t编号:No." + No);
No++;
continue;
} else {
No++;
continue;
}
}
No = 0; //输入菜单
System.out.print("\n\n当月日期:" + years + "年" + monthsDay + "月");
prox(); //上月
}else if(answer.equals("d") || answer.equals("D")|| answer.equals("2")){
ultimo(); //下月
}else if(answer.equals("w") || answer.equals("W")|| answer.equals("3")) {
|| month == 11) {
days += 30;
yearDay = 30;
}
}
if (monthsDay == 1 || monthsDay == 3 || monthsDay == 5
用Java程序编写万年历程序的设计报告
用Java程序编写万年历程序的设计报告一、引言本文档是针对使用Java程序编写万年历程序的设计报告,旨在详细介绍该程序的设计思路和实现细节。
二、需求分析1.背景介绍在现代社会中,人们经常需要查看特定日期的日历信息。
为了方便用户直观地查看任意年月的日历信息,设计一个万年历程序,可以满足用户的需求。
2.功能需求万年历程序具备以下功能:●显示指定年月的日历信息●支持用户输入任意年月●能够高亮当前日期●具备简洁易读的界面设计3.性能需求●用户界面响应迅速,无明显卡顿●日历信息准确无误,支持闰年判断三、概要设计1.系统架构万年历程序采用MVC(Model.View.Controller)架构,将数据模型、用户界面和控制逻辑分离。
模型层:负责处理日期计算和数据存储视图层:负责展示日历信息,接收用户输入控制器层:负责协调模型层和视图层的交互2.关键模块●日历模块:负责日期计算,并提供接口给视图层调用●用户界面模块:提供用户交互界面,并接收用户输入●控制器模块:负责协调日历模块和用户界面模块四、详细设计1.日历模块设计●定义日期对象:包括年、月、日等属性●实现日期计算功能:如计算指定年月的第一天是星期几,判断某年是否为闰年等●提供接口给其他模块调用:如获取指定年月的日历信息2.用户界面设计●设计主界面:包括输入年月的文本框和显示日历信息的面板●设计按钮事件:响应用户输入的年月,调用日历模块计算并显示日历信息3.控制器设计●建立模型与视图之间的联系:将用户输入传递给日历模块,并将计算后的日历信息传递给视图层进行显示五、测试计划1.单元测试●对日历模块进行单元测试,测试日期计算功能的准确性●对用户界面模块进行单元测试,测试界面的交互和显示是否正常2.集成测试●测试控制器模块与日历模块、用户界面模块之间的功能协调是否正常六、附录1.附件●无2.法律名词及注释●无。
Java使用Calendar计算时间的示例代码
Java使⽤Calendar计算时间的⽰例代码Java实例⼤全@Testpublic void test4(){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");/** 获取15天后是多少号?** add(int field,int n)* 为当前calendar指定时间分量上累加给定值* 若传⼊的是负数,则是累减*/Calendar calendar = Calendar.getInstance();calendar.add(Calendar.DAY_OF_YEAR, 15); //给当前时间加15天//System.out.println(calendar.getTime());System.out.println(sdf.format(calendar.getTime()));/** 三个⽉零⼗天后的那周的星期三是⼏号?*/Calendar cal = Calendar.getInstance();cal.add(Calendar.MONTH, 3); //给当前时间加3个⽉cal.add(Calendar.DAY_OF_YEAR, 10); //给当前时间加10天//得出三个⽉⼗天后的⽇期,然后将⽇期设为当周星期三cal.set(Calendar.DAY_OF_WEEK, 4); //1是周天,所以4是周三//System.out.println(cal.getTime());System.out.println(sdf.format(cal.getTime()));/** 获取某个时间分量所允许的最⼤值* 获取今年最⼤天数*/Calendar cal1 = Calendar.getInstance();int days = cal1.getActualMaximum(Calendar.DAY_OF_YEAR);System.out.println("今年最⼤天数为:"+days+"天");/** ⼀周的第⼀天是星期⽇* 获取当前是星期⼏*/Calendar cal2 = Calendar.getInstance();int w = cal2.get(Calendar.DAY_OF_WEEK);System.out.println("今天是星期:"+(w==1?7:w-1));}结果:/** 获取15天后是多少号?*/2019-10-29 14:25:59/** 三个⽉零⼗天后的那周的星期三是⼏号?*/2020-01-22 14:25:59/** 获取今年最⼤天数*/今年最⼤天数为:365天/** 获取当前是星期⼏*/今天是星期:1例⼦:/***** 传⼊具体⽇期,返回具体⽇期增加⼀个⽉。
java编写的万年历
System.exit(0);
}
});
setTitle("万年历 06专升本一班 06303103 冯占魁");
setSize(550,450); //////////////////////////////////////////////////定义万年历方框的大小为550*450
p1.add(textfield); /////////////////////////////////////////////////输入年份
p1.add(lmonth);
p1.add(Month);
Month.add("一月");
Month.add("二月");
Month.add("三月");
days[6].setLabel("Sat");
if ( myday>0)
{
int blank= myday;
for( ;blank>0;blank--,count++)
{
days[count].setLabel("");
}
}
for(int i=1;i<=mytotdays; i++,count++)
Year.add("1900");
Year.addItemListener(new myLis(this));
for(int i=0;i<49;i++)
{
days[i]=new Button("");
java万年历源代码(可运行)
private JScrollPane sp = new JScrollPane(table);
private JButton bLastYear = new JButton("上一年");
p3.add(jsp, BorderLayout.CENTER);
p3.add(p2, BorderLayout.SOUTH);
p3.add(ld, BorderLayout.NORTH);
private JTextArea jta = new JTextArea(); //jta--JTextArea
private JScrollPane jsp = new JScrollPane(jta);
private JLabel l = new JLabel("年份文本框中可直接键入要查找的年份,以提高查询效率");
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
//import java.sql.Date;
cld.set(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1])-1, 0);
showCalendar(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1]), cld);
java万年历界面版
import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Date;import java.util.Calendar;import javax.swing.*;public class calendertest {public static void main(String[] args) {JFrame.setDefaultLookAndFeelDecorated(true);new MainFrame();}}class MainFrame extends JFrame {JPanel panel = new JPanel(new BorderLayout());JPanel panel1 = new JPanel();JPanel panel2 = new JPanel(new GridLayout(7, 7));JLabel[] label = new JLabel[49];JLabel y_label = new JLabel("年份");JLabel m_label = new JLabel("月份");JTextField T1=new JTextField(6);JTextField T2=new JTextField(6);JButton JB1=new JButton("转到");int re_year, re_month;int x_size, y_size;String year_num;Calendar now = Calendar.getInstance(); // 实例化CalendarMainFrame() {super("万年历");setSize(300, 350);x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());setLocation((x_size - 300) / 2, (y_size - 350) / 2);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);panel1.add(y_label);panel1.add(T1);panel1.add(m_label);panel1.add(T2);panel1.add(JB1);for (int i = 0; i < 49; i++) {label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中panel2.add(label[i]);}panel.add(panel1, BorderLayout.NORTH);panel.add(panel2, BorderLayout.CENTER);panel.setBackground(Color.white);panel1.setBackground(Color.white);panel2.setBackground(Color.white);Init();JB1.addActionListener(new ClockAction());setContentPane(panel);setVisible(true);setResizable(false);}public void Init() {int year, month_num, first_day_num;String log[] = { "日", "一", "二", "三", "四", "五", "六" };for (int i = 0; i < 7; i++) {label[i].setText(log[i]);}for (int i = 0; i < 49; i = i + 7) {label[i].setForeground(Color.red); // 将星期日的日期设置为红色}for (int i = 6; i < 49; i = i + 7) {label[i].setForeground(Color.green);// 将星期六的日期设置为绿色}month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份first_day_num = use(year, month_num);Resetday(first_day_num, year, month_num);}public int use(int reyear, int remonth) {int week_num;now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期return week_num;}class ClockAction implements ActionListener {public void actionPerformed(ActionEvent arg0) {int c_year, c_month, c_week;c_year = Integer.parseInt(T1.getText().toString()); // 得到当前所选年份c_month = Integer.parseInt(T2.getText().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11c_week = use(c_year, c_month); // 调用函数use,得到星期几Resetday(c_week, c_year, c_month); // 调用函数Resetday}}public void Resetday(int week_log, int year_log, int month_log) {int month_day_score; // 存储月份的天数int count;month_day_score = 0;count = 1;Date date = new Date(year_log, month_log + 1, 1); // nowCalendar cal = Calendar.getInstance();cal.setTime(date);cal.add(Calendar.MONTH, -1); // 前个月month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天for (int i = 7; i < 49; i++) { // 初始化标签label[i].setText("");}week_log = week_log + 6; // 将星期数加6,使显示正确month_day_score = month_day_score + week_log;for (int i = week_log; i < month_day_score; i++, count++) {label[i].setText(count + "");}}}/*class MainFrame extends JFrame {private static final long serialVersionUID = 1L;JPanel panel = new JPanel(new BorderLayout());JPanel panel1 = new JPanel();JPanel panel2 = new JPanel(new GridLayout(7, 7));JPanel panel3 = new JPanel();JLabel[] label = new JLabel[49];JLabel y_label = new JLabel("年份");JLabel m_label = new JLabel("月份");JComboBox com1 = new JComboBox();JComboBox com2 = new JComboBox();int re_year, re_month;int x_size, y_size;String year_num;Calendar now = Calendar.getInstance(); // 实例化CalendarMainFrame() {super("万年历");setSize(300, 350);x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());setLocation((x_size - 300) / 2, (y_size - 350) / 2);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);panel1.add(y_label);panel1.add(com1);panel1.add(m_label);panel1.add(com2);for (int i = 0; i < 49; i++) {label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中panel2.add(label[i]);}// panel3.add(new Clock(this));panel.add(panel1, BorderLayout.NORTH);panel.add(panel2, BorderLayout.CENTER);//panel.add(panel3, BorderLayout.SOUTH);panel.setBackground(Color.white);panel1.setBackground(Color.white);panel2.setBackground(Color.white);panel3.setBackground(Color.white);Init();com1.addActionListener(new ClockAction());com2.addActionListener(new ClockAction());setContentPane(panel);setVisible(true);setResizable(false);}class ClockAction implements ActionListener {public void actionPerformed(ActionEvent arg0) {int c_year, c_month, c_week;c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11c_week = use(c_year, c_month); // 调用函数use,得到星期几Resetday(c_week, c_year, c_month); // 调用函数Resetday}}public void Init() {int year, month_num, first_day_num;String log[] = { "日", "一", "二", "三", "四", "五", "六" };for (int i = 0; i < 7; i++) {label[i].setText(log[i]);}for (int i = 0; i < 49; i = i + 7) {label[i].setForeground(Color.red); // 将星期日的日期设置为红色}for (int i = 6; i < 49; i = i + 7) {label[i].setForeground(Color.green);// 将星期六的日期设置为绿色}for (int i = 1900; i < 2099; i++) {com1.addItem("" + i);}for (int i = 1; i < 13; i++) {com2.addItem("" + i);}month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份com1.setSelectedIndex(year - 1900); // 设置下拉列表显示为当前年com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月first_day_num = use(year, month_num);Resetday(first_day_num, year, month_num);}public int use(int reyear, int remonth) {int week_num;now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期return week_num;}@SuppressWarnings("deprecation")public void Resetday(int week_log, int year_log, int month_log) {int month_day_score; // 存储月份的天数int count;month_day_score = 0;count = 1;Date date = new Date(year_log, month_log + 1, 1); // nowCalendar cal = Calendar.getInstance();cal.setTime(date);cal.add(Calendar.MONTH, -1); // 前个月month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天for (int i = 7; i < 49; i++) { // 初始化标签label[i].setText("");}week_log = week_log + 6; // 将星期数加6,使显示正确month_day_score = month_day_score + week_log;for (int i = week_log; i < month_day_score; i++, count++) {label[i].setText(count + "");}}}*/。
用Java程序编写万年历程序的设计报告
用Java程序编写万年历程序的设计报告一、引言万年历是一种常见的工具,用于显示特定日期的星期几、农历日期、节气等信息。
本设计报告旨在介绍使用Java编写万年历程序的设计过程和实现细节。
二、需求分析1. 显示当前日期的星期几和农历日期;2. 支持用户输入指定日期,显示该日期的星期几和农历日期;3. 显示当月的日历,并标记当天;4. 支持用户选择不同年份和月份,显示相应的日历;5. 显示当天的节气。
三、设计思路1. 日期计算:使用Java提供的日期和时间类,如`java.util.Calendar`,来进行日期计算和格式化;2. 农历计算:根据农历算法,计算指定日期的农历日期和节气;3. 用户界面:使用Java图形用户界面(GUI)库,如`javax.swing`,来实现用户界面;4. 数据展示:使用文本框、标签等组件来展示日期、星期几、农历日期和节气。
四、程序实现1. 创建Java项目,并导入所需的库;2. 创建主界面窗口,设置布局和组件;3. 添加日期选择器,用于选择年份和月份;4. 添加按钮,用于触发日期查询和切换月份;5. 根据用户选择的日期,计算并显示星期几、农历日期和节气;6. 根据用户选择的年份和月份,计算并显示当月的日历;7. 标记当天的日期;8. 添加事件监听器,处理用户的操作。
五、代码示例```java// 导入所需的库import java.util.Calendar;import java.util.Date;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class CalendarProgram extends JFrame {private JLabel dateLabel;private JLabel weekdayLabel;private JLabel lunarLabel;private JLabel festivalLabel;private JPanel calendarPanel;public CalendarProgram() {// 设置窗口标题和大小setTitle("万年历程序");setSize(800, 600);// 创建主界面布局和组件JPanel mainPanel = new JPanel(); dateLabel = new JLabel();weekdayLabel = new JLabel(); lunarLabel = new JLabel();festivalLabel = new JLabel();calendarPanel = new JPanel();// 将组件添加到主界面布局中 mainPanel.add(dateLabel);mainPanel.add(weekdayLabel); mainPanel.add(lunarLabel);mainPanel.add(festivalLabel);mainPanel.add(calendarPanel); // 设置主界面布局和显示setContentPane(mainPanel);setVisible(true);}public void updateCalendar(Date selectedDate) {// 根据选择的日期更新界面显示Calendar calendar = Calendar.getInstance();calendar.setTime(selectedDate);// 计算并显示星期几、农历日期和节气int weekday = calendar.get(Calendar.DAY_OF_WEEK); String lunarDate = calculateLunarDate(calendar);String festival = calculateFestival(calendar);dateLabel.setText("日期:" + selectedDate.toString()); weekdayLabel.setText("星期几:" + weekday);lunarLabel.setText("农历日期:" + lunarDate);festivalLabel.setText("节气:" + festival);// 计算并显示当月的日历int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);int daysInMonth = calculateDaysInMonth(year, month); displayCalendar(year, month, daysInMonth);}private String calculateLunarDate(Calendar calendar) {// 根据农历算法计算农历日期// 实现略}private String calculateFestival(Calendar calendar) {// 根据日期计算节气// 实现略}private int calculateDaysInMonth(int year, int month) {// 计算指定年份和月份的天数// 实现略}private void displayCalendar(int year, int month, int daysInMonth) { // 显示当月的日历// 实现略}public static void main(String[] args) {CalendarProgram program = new CalendarProgram();program.updateCalendar(new Date());}}```六、总结通过使用Java编写万年历程序,我们可以实现日期计算、农历计算和节气计算等功能,并通过图形界面展示给用户。
java万年历的编写
作者:胡楠启java万年历的编写,以1年1月1日为星期一为标记。
在命令行窗口编译,运行代码:import java.util.Scanner;public class Text4{static int age,mouth;public static void main(String[] args){Scanner sc=new Scanner(System.in);System.out.println("由于程序员水平不行,请输入大于1年的年份");System.out.println("请输入年");age=sc.nextInt();System.out.println("请输入月");mouth=sc.nextInt();int mouse[]=new int[12];mouse[0]=31;//初始化数组。
默认为平年mouse[1]=28;mouse[2]=31;mouse[3]=30;mouse[4]=31;mouse[5]=30;mouse[6]=31;mouse[7]=31;mouse[8]=30;mouse[9]=31;mouse[10]=30;mouse[11]=31;if(age%4==0&&(age%100!=0||age%400==0)){mouse[1]=29;}System.out.println("星期天"+"\t"+"星期一"+"\t"+"星期二"+"\t"+"星期三"+"\t"+"星期四"+"\t"+"星期五"+"\t"+"星期六");int d=FirstMouth(mouth);//D表示当前月份的第一天是星期几 //下面的switch是为了输出界面包括空格和数字等switch(d){case 1:for(int i=0;i<1;i++){System.out.print(" "+"\t");//先输出每个月前面星期的空格 }for(int j=1;j<=mouse[mouth-1];j++){System.out.print(j+"\t");if((j+1)%7==0)System.out.println();}break;case 2:for(int i=0;i<2;i++){System.out.print(" "+"\t");}for(int j=1;j<=mouse[mouth-1];j++){System.out.print(j+"\t");if((j+2)%7==0)System.out.println();}break;case 3:for(int i=0;i<3;i++){System.out.print(" "+"\t");}for(int j=1;j<=mouse[mouth-1];j++){System.out.print(j+"\t");if((j+3)%7==0)System.out.println();}break;case 4:for(int i=0;i<4;i++){System.out.print(" "+"\t");}for(int j=1;j<=mouse[mouth-1];j++){ System.out.print(j+"\t");if((j+4)%7==0)System.out.println(); }break;case 5:for(int i=0;i<5;i++){System.out.print(" "+"\t");}for(int j=1;j<=mouse[mouth-1];j++){ System.out.print(j+"\t");if((j+5)%7==0)System.out.println(); }break;case 6:for(int i=0;i<6;i++){System.out.print(" "+"\t");}for(int j=1;j<=mouse[mouth-1];j++){ System.out.print(j+"\t");if((j+6)%7==0)System.out.println(); }break;case 0:for(int j=1;j<=mouse[mouth-1];j++){ System.out.print(j+"\t");if((j+0)%7==0)System.out.println(); }break;}}//计算每年的第一天是星期几,传入的参数是年份,1年1月1日是星期天把输入年份月份之之前到1年1月1日static int FirstDay(int x){int j=0;int sum=(x-1)*365;//sum表示输入的年之前总共有多少天默认全是平年for(int i=1;i<x;i++){if(i%4==0 && (i%100!=0 || i%400==0)){sum+=1;}}return sum;//返回总天数}//返回当前月份第一天是星期几static int FirstMouth(int y){int x=FirstDay(age);int day=0;int mouse[]=new int[12];mouse[0]=31;//初始化数组。
简单的java万年历代码
private Calendar a; //日历功能
private GridLayout MyGridLayout = new GridLayout(2, 1, 0, 5); //设计容器
private JPanel row1 = new JPanel();
private JPanel row2 = new JPanel();
private JTextField JTMonth=new JTextField(10); //月份显示文本框
private JTextField JTYear=new JTextField(10); //年份显示文本框
private JButton JBAddYear=new JButton(">>"); //增加年份按钮
private JLabel[] numbers = new JLabel[42];
//private JLabel MyJLabel1=new JLabel("设计:王 时间:2014年12月");
private JLabel MyJLabel2=new JLabel("日 一 二 三 四 五 六");
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
s = Integer.toString(MyYear); // 将年份转换为字符串类型
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();}}。
Java万年历Calendar
谨以此文献给同在学习中的学友们此代码理论上包括所有年份及月份;包含容错功能,在输入不正确的情况下,提示重新输入;代码实现关键:1,利用input.hasNextInt()判断用户输入的年份和月份是否符合实际,若不符,则提示重新输入;2,用(year%400==0||(year%4==0&&year%100!=0))判断输入的年份是否为闰年,然后根据月份给出需要加减的天数;3,使用if(year<=0){year++;}来包括公元前的情况;(实际情况中无公元0年,即公元0年为公元1年,也称为公元元年);4,以2012年为基础,进行加减计算(2012年1月1号为星期日);5,每月的第一天为星期几用天数除以7的余数求的;2012年以前的情况用yv=(7-dayAll%7)%7求得;6,本代码不包括显示农历和节日;理论上包括所有年份及月份,但由于地球的自转速度逐渐降低,而公转速度则相对更加稳定,所以上述的系统经过更长的周期也会发生微小的误差。
据计算,每8000年会有一天的误差,所以英国的天文学家John Herschel提议公元4000为平年,以后类推12000年,20000年亦为平年。
但此提议从未被正式采纳。
原因是到了4000年,地球自转的精确速度并非现在可以预测,所以届时参照真实数据方可做出判断。
因此,在长远的将来,针对闰年的微小调整应该不是由预定的系统决定,而是随时不定性的。
本人觉得对初学者很有用,若有疑问及完善建议,欢迎联系本人QQ:58824075好了,具体代码如下:import java.util.*;public class WanNianLi {public static void main(String[] args) {Scanner input=new Scanner(System.in);int dayAll=0,day=0,yv,i,j=0;int year,month;boolean a=true,b=true;System.out.print("请输入需要查询的年份:");do{if(input.hasNextInt()){year=input.nextInt();a=false;System.out.print("请输入需要查询年份的月份:");do{if(input.hasNextInt()){month=input.nextInt();if(month>0&&month<=12){if(year<=0){year++;}if(year%400==0||(year%4==0&&year%100!=0)){ switch (month){case 1:day=31;dayAll=0;break;case 2:day=29;dayAll=31;break;case 3:day=31;dayAll=60;break;case 4:day=30;dayAll=91;break;case 5:day=31;dayAll=121;break;case 6:day=30;dayAll=152;break;case 7:day=31;dayAll=182;break;case 8:day=31;dayAll=213;break;case 9:day=30;dayAll=244;break;case 10:day=31;dayAll=274;break;case 11:day=30;dayAll=305;break;case 12:day=31;dayAll=335;break;}}else{switch (month){case 1:day=31;dayAll=0;break;case 2:day=28;dayAll=31;break;case 3:day=31;dayAll=59;break;case 4:day=30;dayAll=90;break;case 5:day=31;dayAll=120;break;case 6:day=30;dayAll=151;break;case 7:day=31;dayAll=181;break;case 8:day=31;dayAll=212;break;case 9:day=30;dayAll=243;break;case 10:day=31;dayAll=273;break;case 11:day=30;dayAll=304;break;case 12:day=31;dayAll=334;break;}}if(year>=2012){for(i=2012;i<year;i++){if(i%400==0||(i%4==0&&i%100!=0)){dayAll=dayAll+366;}else{dayAll=dayAll+365;}}yv=dayAll%7;for(i=2011;i>=year;i--){if(i%400==0||(i%4==0&&i%100!=0)){j=j+366;}else{j=j+365;}}dayAll=j-dayAll;yv=(7-dayAll%7)%7;}if(year<=0){System.out.println("\n\t\t"+(year-1)+"年\t\t"+month+"月\n");}else{System.out.println("\n\t\t"+year+"年\t\t"+month+"月\n");}System.out.println("日\t一\t二\t三\t四\t五\t六\n");for(i=1;i<=yv;i++){System.out.print("\t");}for(i=1;i<=day;i++){System.out.print(i+"\t");if((i+yv)%7==0){System.out.print("\n\n");}}}else{System.out.print("输入月份无效,请重新输入需要查询的月份:");}}else{String y=input.next();System.out.print("输入无效,请重新输入需要查询的月份:");}}while(b);}else{String x=input.next();System.out.print("输入无效,请重新输入需要查询的年份:");}}while(a);}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
labelWeek[i].setText(stringWeekCn[i]);
panel2.add(labelWeek[i]);
}
for(int i= 0;i<42;i++){
}
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
import java.awt.*;
import java.awt.event.*;
import ng.StringBuffer;
import javax.swing.*;
import java.util.*;
import javax.swing.Timer;
import javax.swing.border.*;
"星期四", "星期五", "星期六" };
//定义存储月份的信息数组,用于转换显示方示
private String[] stringMonthEn = new String[] { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };
//time.setRepeats(true);
time.start();
//labelTime.addAncestorListener(new TimerListener());
*/
int i;
for(i=0;i<12;i++)
if(month.equalsIgnoreCase(stringMonthEn[i]))
break;
return i;
}
/**
*setNowDate()
getSysDate();
setNowDate();
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==buttonToday){
}
public int turnMonth(String month){
/**
*int turnMonth(String month)
*@month 系统日期中的月,诸如Jan\Feb
*@return int
*返回一个整数值,用于寻找stringMonthCn[]数组中对应的中文月份 ;
setBounds(250, 200, 400, 360);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
labelDay[i] = new JLabel();
labelDay[i].setHorizontalAlignment(0);
labelDay[i].setText("");
panel2.add(labelDay[i]);
super(title);
for (int y = 1900; y < 2101; y++) {
combo1.addItem(" " + new Integer(y).toString()+"年");
}
for (int m = 0;m<12;m++){
panel2.setLayout(new GridLayout(7,7,0,10));
panel2.setBackground(Color.white);
for(int i=0;i<7;i++){
labelWeek[i] = new JLabel();
labelWeek[i].setHorizontalAlignment(0);
if(i==0||i==6){
labelWeek[i].setBackground(Color.blue);
labelWeek[i].setForeground(Color.RED);
panel1.add(combo2);
combo2.addItemListener(this);
panel1.add(buttonToday);
buttonToday.addActionListener(this);
labelTime.setFont(new Font("宋体",Font.PLAIN,16));
public int turnWeek(String week){
int i;
for(i=0;i<7;i++)
if(week.equalsIgnoreCase(stringWeekEn[i]))
break;
return i;
labelTime.setForeground(Color.MAGENTA);
panel1.add(labelTime);
Timer time = new Timer(1000,new TimerListener());
time.addActionListener(new TimerListener());
add(panel2);
add(panel3);
panel1.setLayout(new GridLayout(1,3,10,0));
panel1.add(combo1);
combo1.addItemListener(this);
private String[] sysRunTime = new String[6];
private JLabel []labelWeek = new JLabel[7];
private JLabel []labelDay = new JLabel[42];
private JComboBox combo1 = new JComboBox();
private JComboBox combo2 = new JComboBox();
private JButton buttonToday = new JButton();
//定义中英文字符数组存储星期信息,用于转换显示
private String[] stringWeekEn = new String[] { "SUN", "MON", "TUE", "WED",
"THU", "FRI", "SAT" };
private String[] stringWeekCn = new String[] { "星期日", "星期一", "星期二", "星期三",
combo2.addItem(" "+stringMonthCn[m]);
}
buttonToday.setText("今 天");
setLayout(new FlowLayout());
add(panel1);
private String[] stringMonthCn = {"一月","二月","三月","四月","五月","六月",
"七月","八月","九月","十月","十一月","十二月"};
private String[] sysNowTime = new String[6];//sysNowTime 用于存储系统时间的变量
private JLabel labelTime = new JLabel();
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private Border border = BorderFactory.createRaisedBevelBorder();
private Border border1 = BorderFactory.createLineBorder(Color.cyan,3);
public AidyCalender(String title) {
setNowDate();
}
}
public void itemStateChanged(ItemEvent aa){
setChangeDate();
}
labelWeek[i].setFont(new Font("黑体",Font.BOLD,14));
}
else{
labelWeek[i].setForeground(Color.BLACK);
labelWeek[i].setFont(new Font("新宋体",Font.PLAIN,14));