万年历java

合集下载

JAVA《万年历系统》课程设计报告附源码

JAVA《万年历系统》课程设计报告附源码

2013-2014学年第二学期《面向对象程序设计》课程设计报告题目:万年历系统专业:计算机科学与技术班级:姓名:学号:指导教师:成绩:计算机与信息工程系2014年6月6日目录1 设计内容及要求 (1)1.1设计内容 (1)1.2设计任务及具体要求 (1)2 概要设计 (2)2.1程序设计思路 (2)2.2 总体程序框图 (3)3 设计过程或程序代码 (4)3.1各个模块的程序流程图及运行界面 (4)3.2对关键代码加以分析说明 (7)4 设计结果与分析 (13)4.1程序调试的结果 (13)4.2程序设计分析 (13)5 小结 (14)致谢 (15)参考文献 (16)源程序 (17)1 设计内容及要求1.1设计内容万年历,实际上就是记录一定时间范围内(比如100年或更多)的具体阳历或阴历的日期的年历,方便有需要的人查询使用。

在我设计的万年历中主要有:(1)使用图形用户界面来查询用用户所需的日期信息,符合日常软件使用规范。

(2)按月份查询,实现了显示查询1901~2100年某月所有日期的阴阳历对照。

(3)并且添加了重大节日与星座等信息,界面采用日常的星期与月份结合的格式,方便查看与使用。

(4)编写万年历的课程设计,是为了使同学们更加了解高级程序设计语言的结构,掌握基本的程序设计过程和技巧,掌握基本的分析问题和利用计算机求解问题的能力,具备初步的高级语言程序设计能力。

为后续各门计算机课程的学习和毕业设计打下坚实基础。

1.2设计任务及具体要求利用JAVA语言编写的万年历系统采用了多种JAVA语句来实现多种功能。

用户可以通过本程序的applet运行界面来查找一整年某月的农历和阳历,可以查找用户所想了解的某一天具体为星期几,并且可以看到一些重大节日的具体时间。

要求:满足设计万年历系统的目的,即当用户查询年份与月份时,系统就要将这一年的某一月份的阴历与阳历全部显示出来,并且附带这一月份的重大节日。

当用户随意改动年份或月份时系统自动显示与星期对应的日期。

java控制台输出万年历

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万年历源代码,可显示公历、农历、系统时间、国际时间

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编写万年历(1900年以后)

java编写万年历(1900年以后)
System.out.println("请输入你想查询的年份:");
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语言开发的工具,用于显示和管理日期和时间。

它提供了一种简单而直观的方式来查看、创建和编辑日历事件,并提供了一些额外的功能,如提醒、重复事件和时间段的计算等。

本文将详细介绍Java日历小程序的功能、使用方法和设计原理。

功能:1. 显示日历:Java日历小程序可以显示当前月份的日历,并以日、周或月的形式展示。

用户可以通过界面上的上下翻页按钮切换月份,方便查看不同日期的日历。

2. 创建和编辑事件:用户可以通过Java日历小程序创建新的日历事件,并为每个事件指定标题、日期、时间和地点等详细信息。

程序还提供了事件编辑功能,允许用户修改已有事件的信息。

3. 提醒功能:Java日历小程序可以设置事件的提醒功能,提醒用户在指定的时间前提醒。

用户可以选择在事件开始前几分钟、几小时或几天提醒,并可以自定义提醒方式,如弹窗、声音或邮件通知等。

4. 重复事件:用户可以为事件设置重复功能,使事件在指定的日期间隔内重复发生。

可以选择每天、每周、每月或每年重复,并可以设置重复的结束日期。

5. 时间段计算:Java日历小程序可以计算两个日期之间的时间段,如计算两个事件之间的天数、小时数或分钟数等。

用户可以选择计算方式,并获取精确的时间段结果。

使用方法:1. 下载和安装:用户可以从官方网站上下载Java日历小程序的安装包,然后按照安装向导进行安装。

安装完成后,用户可以在桌面或开始菜单中找到程序的快捷方式。

2. 打开程序:双击程序的快捷方式,Java日历小程序将启动并显示当前月份的日历。

用户可以通过界面上的按钮和菜单来使用各种功能。

3. 创建事件:用户可以点击界面上的“添加事件”按钮,弹出一个对话框,输入事件的详细信息,如标题、日期、时间和地点等。

点击“确定”按钮后,事件将被创建并显示在日历上。

4. 编辑事件:用户可以在日历上点击已有的事件,弹出一个对话框,允许用户修改事件的信息。

Java万年历

Java万年历

J a v a万年历一项目概述:这个项目是一个简单的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 其他功能:(无)以下是全部代码(共六个.J a v a文件)//对月份的选择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){}}//向数据库中添加数据。

万年历课程设计说明书

万年历课程设计说明书

*******************实践教学*******************兰州理工大学计算机与通信学院2012年秋季学期Java 课程设计题目:万年历的设计专业班级:姓名:学号:指导教师:成绩:目录摘要 (2)序言 (3)正文 (4)一、系统分析与设计 (4)二、详细设计 (5)2.1 类Calendars (5)2.2 类Time (5)2.3 类CalTabel (5)三、系统测试 (6)四、软件使用说明书 (9)设计总结 (10)参考文献 (11)致谢 (12)附录1(源代码) (13)摘要根据万年历的需要,设计一个万年历的程序,实现在此万年历表中有关日期和时间的基本操作:显示当时系统的日期和时间;显示当日的年和月;通过查询显示指定年、月的日历;设置北京时间,以显示北京和格林威治的时间并以设定的时间开始计时。

设置时间和查询日历通过设计的窗体执行和显示。

关键词:万年历;图形界面;java技术序言1996年Java第一次发布就引起了人们的极大兴趣。

关注Java的人士不仅限于计算机出版界,还有诸如《纽约时报》、《华盛顿邮报》、《商业周刊》这样的主流媒体。

Java时第一种也是唯一一种在National Public Radio上占用了十分钟时间进行介绍的程序设计语言,并且还得到了$100 000 000的风险投资基金。

这些基金全部用来支持这种特别的计算机语言开发的产品。

Java语言在Internet舞台上以及亮相便名声大噪,其原因在于它将有望成为连接用户与信息的万能胶,而不论这些信息来源于Web服务器、数据库、信息提供商,还是任何其他渠道,Java固有的可靠性与安全性不仅令Java程序员放心,也令使用Java程序的用户放心。

而此次将用Java设计一个万年历表,我们更加了解Java面向对象的程序设计语言。

正文一、系统分析与设计1、系统的要求(1)定义一个时间类Time,能提供时、分、秒组成的时间,并提供时间增加的方法(按秒);(2)定义一个日期类Date,记录年、月、日,并提供日期增加的方法(按天);(3)由类Time和日期类Date,派生出DateTime类,除具有年、月、日、时、分、秒外,还增加国名和与格林威治时间的差;(4)输入年、月、日、时、分、秒,在屏幕上模拟显示一电子计时器,不断输出下一秒的时间和日期,以及格林威治的日期和时间。

java 年月日的枚举值

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日历课程设计报告

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.
public enum Year {。

YEAR_2022,。

YEAR_2023,。

// 其他年份。

}。

接着,我们可以创建一个枚举类型来表示月份,如下所示:
java.
public enum Month {。

JANUARY,。

FEBRUARY,。

MARCH,。

// 其他月份。

}。

最后,我们可以创建一个枚举类型来表示日期,如下所示: java.
public enum Day {。

DAY_1,。

DAY_2,。

// 其他日期。

}。

通过这样的枚举类型,我们可以在程序中很方便地表示和操作年、月、日。

例如,我们可以使用这些枚举类型来构建日期对象,进行日期的比较和计算等操作。

另外,Java也提供了`java.time`包,其中包含了丰富的日期和时间操作类,可以更方便地处理日期时间相关的操作。

总的来说,使用枚举类型来表示年、月、日可以使代码更加清晰易懂,同时也有利于程序的可维护性和扩展性。

希望这些信息能够帮助到你。

java获取年月日的方法

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类来格式化日期。

通过传入不同的格式化模式,我们可以获取所需的日期部分。

java万年历程序代码

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 年月日正则表达式

java 年月日正则表达式

java 年月日正则表达式
在Java中,可以使用正则表达式来验证日期格式。

以下是一个示例正则表达式,用于匹配年月日格式:
```java
String regex = "^(\\d{4})-(\\d{2})-(\\d{2})$";
```
这个正则表达式的含义是:
`^` 表示字符串的开头
`(\\d{4})` 表示匹配4位数字,即年份
`-` 表示匹配连字符
`(\\d{2})` 表示匹配2位数字,即月份
`-` 表示匹配连字符
`(\\d{2})` 表示匹配2位数字,即日期
`$` 表示字符串的结尾
你可以使用这个正则表达式来验证一个字符串是否符合年月日格式。

例如:
```java
String date = "";
String regex = "^(\\d{4})-(\\d{2})-(\\d{2})$";
boolean isValid = (regex);
```
在这个示例中,`isValid` 变量将为 `true` 如果 `date` 字符串符合年月日格式,否则为 `false`。

java localdate常用方法

java localdate常用方法

java localdate常用方法Java LocalDate是Java 8中的一个日期类,用于表示不带时区的日期。

它提供了一系列常用的方法来操作和处理日期。

本文将介绍LocalDate类的常用方法,帮助读者更好地理解和使用该类。

1. 获取当前日期在Java中,我们可以使用LocalDate.now()方法获取当前日期。

下面是示例代码:LocalDate currentDate = LocalDate.now();System.out.println(currentDate);以上代码将输出当前的日期,格式类似于"2022-07-15"。

2. 构造指定日期除了获取当前日期,我们还可以使用LocalDate.of()方法构造一个指定日期的LocalDate对象。

示例代码如下:LocalDate date = LocalDate.of(2022, 7, 15);System.out.println(date);以上代码将输出指定的日期"2022-07-15"。

3. 获取年、月、日信息在LocalDate类中,我们可以使用getYear()、getMonthValue()和getDayOfMonth()等方法分别获取当前日期的年、月和日信息。

示例代码如下:LocalDate currentDate = LocalDate.now();int year = currentDate.getYear();int month = currentDate.getMonthValue();int day = currentDate.getDayOfMonth();System.out.println("Year: " + year);System.out.println("Month: " + month);System.out.println("Day: " + day);以上代码将输出当前日期的年、月和日信息。

java calendar用法

java calendar用法

java calendar用法Java Calendar用法Java Calendar是Java中的一个日期和时间处理类,它提供了一系列处理日期和时间的方法。

在Java中,Calendar类是一个抽象类,不能直接实例化,需要通过getInstance()方法获取一个Calendar对象。

一、获取Calendar对象Calendar calendar = Calendar.getInstance();二、获取日期和时间信息1. 获取年份信息int year = calendar.get(Calendar.YEAR);2. 获取月份信息int month = calendar.get(Calendar.MONTH) + 1;注意:月份从0开始计数,所以需要加1。

3. 获取日期信息int day = calendar.get(Calendar.DAY_OF_MONTH);4. 获取星期几信息int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); 注意:星期天为1,星期六为7。

5. 获取小时信息int hour = calendar.get(Calendar.HOUR_OF_DAY);6. 获取分钟信息int minute = calendar.get(Calendar.MINUTE);7. 获取秒钟信息int second = calendar.get(Calendar.SECOND);8. 获取毫秒信息int millisecond = calendar.get(LISECOND);三、设置日期和时间信息1. 设置年份信息calendar.set(Calendar.YEAR, 2022);2. 设置月份信息(月份从0开始计数)calendar.set(Calendar.MONTH, 0);3. 设置日期信息calendar.set(Calendar.DAY_OF_MONTH, 1);4. 设置小时信息(24小时制)calendar.set(Calendar.HOUR_OF_DAY, 12);5. 设置分钟信息calendar.set(Calendar.MINUTE, 30);6. 设置秒钟信息calendar.set(Calendar.SECOND, 0);7. 设置毫秒信息calendar.set(LISECOND, 0);四、日期和时间的计算1. 年份的加减calendar.add(Calendar.YEAR, 1); // 加一年calendar.add(Calendar.YEAR, -1); // 减一年2. 月份的加减calendar.add(Calendar.MONTH, 1); // 加一个月calendar.add(Calendar.MONTH, -1); // 减一个月3. 日期的加减calendar.add(Calendar.DAY_OF_MONTH, 1); // 加一天calendar.add(Calendar.DAY_OF_MONTH, -1); // 减一天4. 小时的加减calendar.add(Calendar.HOUR_OF_DAY, 1); // 加一小时calendar.add(Calendar.HOUR_OF_DAY, -1); // 减一小时5. 分钟的加减calendar.add(Calendar.MINUTE, 10); // 加十分钟calendar.add(Calendar.MINUTE, -10); // 减十分钟6. 秒钟的加减calendar.add(Calendar.SECOND, 30); // 加三十秒钟calendar.add(Calendar.SECOND, -30); // 减三十秒钟7. 毫秒的加减calendar.add(LISECOND, 500); // 加五百毫秒calendar.add(LISECOND, -500); // 减五百毫秒五、日期和时间格式化输出可以使用SimpleDateFormat类将日期和时间格式化输出。

java获取年份的方法

java获取年份的方法

java获取年份的方法Java provides several ways to get the current year. One of the most commonly used methods is to use the Calendar class. Java提供了几种获取当前年份的方法。

其中最常用的方法之一是使用Calendar类。

The Calendar class has a method called get() which allows us to retrieve the value of a specific field, such as the year. We can retrieve the current year by creating an instance of the Calendar class and calling the get() method with the field. Calendar类有一个叫做get()的方法,允许我们检索特定字段的值,比如年份。

我们可以通过创建Calendar类的实例,然后使用字段调用get()方法来检索当前年份。

Another way to get the current year in Java is by using the Year class from the package, which was introduced in Java 8. The Year class has a static method called now() which returns the current year. We can simply call () to get the current year. 在Java中获取当前年份的另一种方法是使用包中引入的Year类,该类在Java 8中被引入。

Year类有一个叫做now()的静态方法,它返回当前年份。

项目一制作万年历

项目一制作万年历

日历界面
以周为单位显示日期,标 注节假日和农历日期Biblioteka 提 供日期选择功能。设置界面
允许用户设置提醒功能、 选择主题和语言等个性化 设置。
数据库设计
01
用户表
存储用户信息,包括用户名、密码、 邮箱等字段。
提醒表
存储提醒信息,包括提醒内容、时 间、日期等字段。
03
02
日历表
存储日历数据,包括日期、星期、 节假日等字段。
安全测试与优化
测试系统安全性
检查系统是否存在安全漏洞,如SQL注入、跨站脚本 攻击等。
测试用户数据保护
验证系统是否能够保护用户数据不被非法获取或篡改。
优化系统安全措施
根据安全测试结果,对系统进行相应的安全加固和优 化。
07
项目总结与展望
项目总结
功能实现
万年历项目已成功实现了基本功能,包括公历、农历、节气、生肖等信息的展示,以及通过输入 年份和月份来查询指定日期对应的农历和公历日期。
后端部分
使用Node.js和Express.js搭建 服务器,处理前端的请求,与 数据库进行交互。
数据库部分
使用MongoDB和Mongoose 进行数据存储和操作。
前后端交互
通过API进行前后端交互,前后 端分离,提高应用的扩展性和 可维护性。
04
系统设计
系统架构设计
1 2 3
前端架构
使用HTML、CSS和JavaScript构建用户界面, 利用前端框架如Bootstrap进行布局和样式设计。
后端技术选型
Node.js
MongoDB
使用非阻塞I/O模型,使得其轻量且高 效,适用于构建高效能、可扩展的网 络应用。

java calendar set方法

java calendar set方法

java calendar set方法Java Calendar类中的set方法是用来设置日期和时间的。

在使用set方法之前,我们首先需要创建一个Calendar对象,并通过该对象调用set方法来设置日期和时间。

set方法有多个重载的版本,用于设置不同的日期和时间字段。

以下是set方法的几种常见用法:1. 设置年份:使用set方法设置年份,可以使用Calendar类中定义的常量来表示具体的年份。

例如,要将年份设置为2021年,可以使用以下代码:```javaCalendar calendar = Calendar.getInstance();calendar.set(Calendar.YEAR, 2021);```2. 设置月份:使用set方法设置月份时,月份的范围是从0到11,其中0表示一月,11表示十二月。

例如,要将月份设置为五月,可以使用以下代码:```javacalendar.set(Calendar.MONTH, Calendar.MAY);```3. 设置日期:使用set方法设置日期时,日期的范围是从1到31。

例如,要将日期设置为15号,可以使用以下代码:```javacalendar.set(Calendar.DAY_OF_MONTH, 15);```4. 设置小时:使用set方法设置小时时,小时的范围是从0到23。

例如,要将小时设置为下午3点,可以使用以下代码:```javacalendar.set(Calendar.HOUR_OF_DAY, 15);```5. 设置分钟:使用set方法设置分钟时,分钟的范围是从0到59。

例如,要将分钟设置为30分,可以使用以下代码:```javacalendar.set(Calendar.MINUTE, 30);```6. 设置秒钟:使用set方法设置秒钟时,秒钟的范围是从0到59。

例如,要将秒钟设置为45秒,可以使用以下代码:```javacalendar.set(Calendar.SECOND, 45);```7. 设置毫秒:使用set方法设置毫秒时,毫秒的范围是从0到999。

万年历程序(不要嫌程序长,功能很齐全,而且对初学者很有帮助)

万年历程序(不要嫌程序长,功能很齐全,而且对初学者很有帮助)

#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;。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

package clock;/** *//*** MainFrame.java* Summary 万年历主类* Created on* @author* remark*/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.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;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 = 1; i < 10000; 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 - 1); // 设置下拉列表显示为当前年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 + "");}}public static void main(String[] args) {JFrame.setDefaultLookAndFeelDecorated(true);new MainFrame();}万年历二/** *//*** MainFrame.java* Summary 万年历主类* Created on* @author* remark*/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.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;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 = 1; i < 10000; 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 - 1); // 设置下拉列表显示为当前年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 + "");}}public static void main(String[] args) {JFrame.setDefaultLookAndFeelDecorated(true);new MainFrame();}}万年历3以下是两个类,请楼主分别存成两个java文件:其中MainFrame.java是显示日历程序,Clock.java是日历计算程序。

相关文档
最新文档