java万年历制作

合集下载

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) { }}。

JavaSwing日历控件

JavaSwing日历控件

Java swing日历控件,界面精美,使用方便,能用于个人作品首先,声明一点,这个控件是我从一套据说价值九百多美元的swing类包中提取出来,并加以修改的,所以:
一,你不能把该控件用于任何商业产品中。

二,该控件没有源代码。

(DatePicker.java除外,这个是我加进去的)
三,该控件可以用于你的个人作品中。

截图如下:
使用之前请把datepicker.jar导入classpath中
使用方法如下:
DatePicker datepick = new DatePicker();
或者
datepick = new DatePicker(date,DefaultFormat,font,dimension);//自定义参数值
//各参数含义如下
//参数1 设置该控件的初始值(可选)
//参数2 设置日期的格式(可选)
//参数3 设置该控件的字体(可选)
//参数3 设置该控件的大小(可选)
//该控件继承自JComponent ,它继承了Jcomponent的属性
datepick.setTimePanleVisible(true);//设置时钟面板可见
atepick.setHightlightdays(hilightDays, Color.red);//设置一个月份中需要高亮显示的日子
datepick.setDisableddays(disabledDays);//设置一个月份中不需要的日子,呈灰色显示。

java日历小程序

java日历小程序

java日历小程序简介:Java日历小程序是一个基于Java语言开发的工具,用于显示和管理日期和时间。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

日历源代码——java

日历源代码——java

日历源代码——java//import java.sql.Date;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Locale;public class GregorianCalendar日历 {public static void main(String[] args) { //设置不同地区Locale.setDefault();//创建当前日历对象GregorianCalendar now = new GregorianCalendar();//从当前时期对象是取出时间日期对象//编辑错误:Type mismatch: cannot convert from java.util.Date to java.sql.DateDate date = now.getTime();//将时间日期对象按字符形式打印System.out.println(date.toString());//重新将时间对象设置到日期对象中now.setTime(date);//从当前日期对象是取出当前月份、日期int today =now.get(Calendar.DAY_OF_MONTH);int month = now.get(Calendar.MONTH);//获取本月开始日期now.set(Calendar.DAY_OF_MONTH, 1);//获取本月开始日期在一周中的编号int week = now.get(Calendar.DAY_OF_WEEK);//打印日历头并换行设置当前月中第一天的开始位置System.out.println("星期日星期一星期二星期三星期四星期五星期六");//设置当前月中第一天的开始位置for( int i = Calendar.SUNDAY; i < week; i++){ System.out.print(" ");//按规格打印当前月的日期数字while(now.get(Calendar.MONTH) ==month){//取出当前日期int day =now.get(Calendar.DAY_OF_MONTH);//设置日期数字小于10与不小于10两种情况的打印规格if(day < 10){//设置当前日期的表现形式if(day == today)System.out.print(" <" + day + "> ");elseSystem.out.print(" " + day + " ");}else{//设置当前日期的表现形式if(day == today)System.out.print(" <" + day + "> ");elseSystem.out.print(" " + day + " ");}//设置什么时候换行if(week == Calendar.SATURDAY)System.out.println();//设置日期与星期几为下一天now.add(Calendar.DAY_OF_MONTH, 1);week = now.get(Calendar.DAY_OF_WEEK);}}}}。

万年历课程设计说明书

万年历课程设计说明书

*******************实践教学*******************兰州理工大学计算机与通信学院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)输入年、月、日、时、分、秒,在屏幕上模拟显示一电子计时器,不断输出下一秒的时间和日期,以及格林威治的日期和时间。

毕业设计基于javagui的万年历系统的设计与实现[管理资料]

毕业设计基于javagui的万年历系统的设计与实现[管理资料]

目录1 需求分析 (3)2 总体设计 (3)设计的总体思想与算法描述 (4)模块结构图 (5)用户查询主界面设计模块 (5)用户输入查询日历模块 (5)农历与公历日期信息转换模块 (6)相应日期节日信息显示模块 (7)查询主界面效果更换模块 (7)各模块的功能与处理流程描述 (8)用户查询主界面设计模块功能和处理流程 (8)用户输入查询日历模块 (9)公历和农历日期信息转换模块 (9)相应日期节日信息显示模块 (9)查询主界面效果更换模块 (10)界面设计 (10)万年历初始化界面 (10)特定的日期查询界面 (11)特定的日期节日显示界面(如春节、元宵节) (12)特定的农历和公历日期信息转换界面 (13)查询主界面换肤功能展示界面 (14)3 各功能模块程序设计 (16)查询主界面布局设计部分截取代码 (16)公历日期求解算法程序截取代码 (18)农历与公历转换算法程序截取代码 (21)节日求解算法程序实现截取代码 (26)4 小结 (28)5 参考文献 (29)题目:基于Java GUI 的万年历系统的设计与实现开发平台:eclipse1 需求分析本系统所描述的万年历系统主要是基于Java GUI 来实现的。

其主要功能是致力于为用户提供方便快捷、准确的日历时间信息,以便用户能够迅速地查找出自己所需的相关日历信息。

本万年历系统主要的功能和任务是任意选择一段时间内的某个年份月份进行查询,对相关的日历信息进行公历和农历的换算显示,显示相应日期的节日信息等。

功能描述:(1)日历查询实现:主要通过主界面顶端的两个下拉列表框来选择相应待查询的年份和月份以实现具体的月份的日历查询。

(2)公历和农历的时间换算:当把鼠标指针指向日历表里的某个具体日期数字上时,会自动显示相关的农历年份、月份、和生肖的相关信息。

(3)节日信息的显示:当某个日期为某个特定的节日时,相应的日期数字所在表格会显示该节日信息,包括了阳历和农历节日。

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

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

一、设计内容和要求1、简单万年历的基本要求本程序的要求为:1.使用图形用户界面;2.本程序能够实现日期与星期的查询。

2、需要实现的主要功能(1). 能够显示实现1800-2100年的日期,包括公历、农历、星期、二十四节气。

(2). 能够按照月份格式显示,或按照星期格式显示。

(3). 能够显示系统时间。

(4). 能够设置闹钟。

(5). 能够查询若干国际大城市(分布在不同时区)的时间。

二、系统分析与概要设计1. 总天数的算法:首先用if语句判断定义年到输入年之间每一年是否为闰年,是闰年,该年的总天数为366,否则,为365。

然后判断输入的年是否为定义年,若是,令总天数S=1,否则,用累加法计算出定义年到输入年之间的总天数,再把输入年的一月到要输出的月份之间的天数累加起来,若该月是闰年中的月份并且该月还大于二月,再使总天数加1,否则,不加,既算出从定义年一月一日到输出年的该月一日的总天数。

2. 输出月份第一天为星期几的算法:使总天数除以7取余加2得几既为星期几,若是7,则为星期日。

3. 算出输出月份第一天为星期几的算法:算出输出月份第一天为星期几后,把该日期以前的位置用空格补上,并总该日起一次输出天数直到月底,该月中的天数加上该月一日为星期几的数字再除以7得0换行,即可完整的输出该月的日历。

4. 如果年份小于1900年则程序不予判断5.公历与农历的换算。

公历年对应的农历数据,每年三字节,格式第一字节BIT7-4 位表示闰月月份,值为0 为无闰月,算法:日期+年份+所过闰年数+月较正数之和除7 的余数就是星期但如果是在闰年又不到3 月份上述之和要减一天再除7星期数为0。

6、系统时间的设置。

直接从系统得到时间,设置字体,背景,显示框架等等,绘图就好了。

三、详细设计和编码开始程序初始化获取当前系统时间年份等数据计算,绘制屏幕是否输入年份或选择月份提交年月变更数据结束总流程图公历转农历流程图图3时间调整程序流程图2、类的定义及关系本次程序设计共用了 class Main Frame extends JFrame, class Clock extends Canvas implements Runnable和class Lunar三个大类,用于绘制万年历,显示万年历的系统时间和完成农历与公历的换算。

这些类中又运用了许多函数,来完成月份的输出,背景的设置,特定日期的标注等等。

引用的类有:import java.ait.*;import java.ait.event.Accentuation;import java.ait.event.Action Listener;import java.ait.event.Mouse Event;import java.ait.event.McAlister;import java.io.Buffered;import java.io.Buffered;import java.io.File;import java.io.Nonmainstream//import java.ql.Date;import java.text.Date Format;import java.text.Simplehearted;import java.til.Calendar;import java.til.Date;import Java.swing.Button;import Java.swing.Dialog;import Java.swing.Frame;import Java.swing.J Label;import Java.swing.Optionee;import Java.swing.J Panel;import Java.swing.Enscroll;import Java.swing.Getable;import Java.swing.Tartarean;import Java.swing.Leftfield;import Java.swing.table.Democratifiable;import java.text.*;import java.ait.*;import Java.swing.*;import java.ait.event.*;import Java.swing.border.*;import java.til.*;import Java.swing.*;3、运行效果4、源代码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();JButton but1=new JButton("上个月");JButton but2=new JButton("下个月");int re_year, re_month;int x_size, y_size;String year_num;Calendar now = Calendar.getInstance(); // 实例化CalendarMainFrame() { super("万年历");setSize(600, 700);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(but1);panel1.add(y_label);panel1.add(com1);panel1.add(m_label);panel1.add(com2);panel1.add(but2);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.gray);Init();but1.addActionListener(new AnAction());but2.addActionListener(new AnAction());com1.addActionListener(new ClockAction());com2.addActionListener(new ClockAction());setContentPane(panel);setVisible(true);setResizable(false);}class AnAction implements ActionListener{public void actionPerformed(ActionEvent e){int c_year, c_month,c_week;c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11c_year = Integer.parseInt(com1.getSelectedItem().toString())-1; // 得到当前所选年份if(e.getSource()==but1){if(c_month==0){c_year=c_year-1;c_month=11;}elsec_month=c_month-1;}if(e.getSource()==but2){if(c_month==11){ c_year=c_year+1;c_month=0; }elsec_month=c_month+1;}com1.setSelectedIndex(c_year);com2.setSelectedIndex(c_month);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}}class NextAction implements ActionListener{public void actionPerformed(ActionEvent arg0){int c_year, c_month,c_week;c_month =Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份if(c_month==11){c_year=c_year+1;c_month=0;}c_week = use(c_year, c_month); // 调用函数use,得到星期几 Resetday(c_week, c_year, c_month); // 调用函数Resetday }}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.blue); // 将星期日的日期设置为红色}for (int i = 6; i < 49; i = i + 7) {label[i].setForeground(Color.blue);// 将星期六的日期设置为hong色}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;Lunar lunar;int month_day;String[] LunarDate=new String[49];month_day_score = 0;count = 1;for (int i = 1; i < 49; i ++) {for(int j=0;j<49;j=j+7){if(i!=j&&i!=j+6)label[i].setForeground(Color.black);}}Date date = new Date(year_log, month_log + 1, 1); // now MONTH 是从0开始的, 对于一月第几天来说,DAY_OF_MONTH第一天就是1. 对于一年第几个月来说,MONTH一月份是0,二月份是1...Calendar cal = Calendar.getInstance();cal.setTime(date);cal.add(Calendar.MONTH, -1); // 前个月month_day_scor=cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天month_day=month_day_score;for (int i = 7; i < 49; i++) { // 初始化标签label[i].setText("");}week_log = week_log + 6; // 将星期数加6,使显示正确month_day_score = month_day_score + week_log;lunar=new Lunar();for(int i=0;i<month_day;i++){LunarDate[i]=lunar.getLunarDate( year_log, month_log+1, i+1);}for (int i = week_log; i < month_day_score; i++, count++) { if(month_log==9&&count==1){label[i].setText(count +"国庆" );label[i].setForeground(Color.red);}else if(month_log==0&&count==1){label[i].setText(count +"元旦" );label[i].setForeground(Color.red);}else if(month_log==11&&count==24){label[i].setText(count +"平安夜" );label[i].setForeground(Color.red);}else if(month_log==11&&count==25){label[i].setText(count +"圣诞" );label[i].setForeground(Color.red);}else if(month_log==1&&count==14){label[i].setText(count +"情人节" );label[i].setForeground(Color.red);}else if(month_log==4&&count==1){label[i].setText(count +"劳动节" );label[i].setForeground(Color.red);}else if(month_log==10&&count==20){label[i].setText(count +"闺蜜生日" );label[i].setForeground(Color.red);}else if(month_log==1&&count==25){label[i].setText(count +"自己生日" );label[i].setForeground(Color.red);}else if(month_log==6&&count==7){label[i].setText(count +"爸爸生日" );label[i].setForeground(Color.red);}else if(month_log==0&&count==12){label[i].setText(count +"妈妈生日" );label[i].setForeground(Color.red);}else if(LunarDate[i-week_log].equals("春节")||LunarDate[i-week_log].equals("元宵")||LunarDate[i-week_log].equals("端午")||LunarDate[i-week_log].equals("中秋")){label[i].setText(count +LunarDate[i-week_log] );label[i].setForeground(Color.red);}elselabel[i].setText(count +LunarDate[i-week_log] );}}public static void main(String[] args) {JFrame.setDefaultLookAndFeelDecorated(true);new MainFrame();}}class Clock extends Canvas implements Runnable{private static final long serialVersionUID = 3660124045489727166L; MainFrame mf;Thread t;String time;public Clock(MainFrame mf){this.mf=mf;setSize(280,40);setBackground(Color.white);t=new Thread(this); //实例化线程t.start(); //调用线程}public void run(){while(true){try{Thread.sleep(1000); //休眠1秒钟}catch(InterruptedException e){System.out.println("异常");}this.repaint(100);}}public void paint(Graphics g){Font f=new Font("宋体",Font.BOLD,16);SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型Calendar now=Calendar.getInstance();time=SDF.format(now.getTime()); //得到当前日期和时间g.setFont(f);g.setColor(Color.black);g.drawString(time,45,25);}}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));}public static String getChinaDayString(int day) {String chineseTen[] = {"初", "十", "廿", "卅"};int n = day % 10 == 0 ? 9 : day % 10 - 1;if (day > 30)return "";if (day == 10)return "初十";elsereturn chineseTen[day / 10] + chineseNumber[n];}* 传出y年m月d日对应的农历.* yearCyl3:农历年与1864的相差数 ?* monCyl4:从1900年1月31日以来,闰月数* dayCyl5:与1900年1月31日相差的天数,再加40 ?public String getLunarDate(int year_log, int month_log, int day_log) {//@SuppressWarnings("unused")int yearCyl, monCyl, dayCyl;int leapMonth = 0;String nowadays;Date baseDate = null;Date nowaday=null;try {baseDate = chineseDateFormat.parse("1900年1月31日");} catch (ParseException e) {e.printStackTrace(); //To change body of catch statement use Options | File Templates.}nowadays=year_log+"年"+month_log+"月"+day_log+"日";try {nowaday = chineseDateFormat.parse(nowadays);} catch (ParseException e) {e.printStackTrace(); //To change body of catch statement use Options | File Templates.}//求出和1900年1月31日相差的天数int offset = (int) ((nowaday.getTime() - baseDate.getTime()) / 86400000L);dayCyl = offset + 40;monCyl = 14;//用offset减去每农历年的天数// 计算当天是农历第几天//i最终结果是农历的年份//offset是当年的第几天int iYear, daysOfYear = 0;for (iYear = 1900; iYear < 10000 && offset > 0; iYear++) {daysOfYear = yearDays(iYear);offset -= daysOfYear;monCyl += 12;}if (offset < 0) {offset += daysOfYear;iYear--;monCyl -= 12;}//农历年份year = iYear;yearCyl = iYear - 1864;leapMonth = leapMonth(iYear); //闰哪个月,1-12leap = false;//用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天int iMonth, daysOfMonth = 0;for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {//闰月if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) { --iMonth;leap = true;daysOfMonth = leapDays(year);} elsedaysOfMonth = monthDays(year, iMonth);offset -= daysOfMonth;//解除闰月if (leap && iMonth == (leapMonth + 1)) leap = false;if (!leap) monCyl++;}//offset为0时,并且刚才计算的月份是闰月,要校正if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) { if (leap) {leap = false;} else {leap = true;--iMonth;--monCyl;}}//offset小于0时,也要校正if (offset < 0) {offset += daysOfMonth;--iMonth;--monCyl;}month = iMonth;day = offset + 1;if(((month)==1)&&day==1){return "春节";}else if(((month)==1)&&day==15){ return "元宵"; }else if(((month)==5)&&day==5)return "端午";else if(((month)==8)&&day==15)return "中秋";else if(day==1)return chineseNumber[month - 1] + "月";elsereturn getChinaDayString(day);}public String toString() {if(chineseNumber[month - 1]=="一"&&getChinaDayString(day)=="初一")return "农历"+year + "年";else if(getChinaDayString(day)=="初一")return chineseNumber[month - 1] + "月";elsereturn getChinaDayString(day);//return year + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] + "月" + getChinaDayString(day);}}四、测试结果五、小结经过短暂的一周的Java程序设计,使我又学到了很多的知识,尤其是运用Java知识去解决实际问题的能力又得到了很大的提高。

相关文档
最新文档