首页 > 编程知识 正文

java万年历,java万年历课程设计

时间:2023-12-29 13:16:31 阅读:329689 作者:ZXBY

本文目录一览:

如何用java语言 写出一个万年历呢? 要求自己输入年份 自动出现月 日 以及对应的星期

如果要月历,只要把月份循环那里修改下,直接调用月历方法既可

import java.text.DateFormatSymbols;

import java.util.Calendar;

import javax.swing.JOptionPane;

public class YearCalendar {

public static void main(String[] args) {

final String title = getCalTitle();

String input = JOptionPane.showInputDialog("Please input year");

try{

if(!input.trim().matches("^\d{4}$")){

throw new NumberFormatException();

}

int year = Integer.parseInt(input.trim());

System.out.println("------- Calendar For Year " + year + " ----------------");

String[] monthTitles = new DateFormatSymbols().getMonths();

for(int month = Calendar.JANUARY; month = Calendar.DECEMBER; month++){

System.out.println("t********** " + monthTitles[month] + " *********");

System.out.println(title);

generateMonthlyCalendar(year, month);

System.out.println("nn");

}

}catch(NumberFormatException nbFmtExp){

JOptionPane.showMessageDialog(null, "Error data foramt! Date should be 4 digits only format yyyy");

System.exit(0);

}

}

private static String getCalTitle() {

StringBuffer sb = new StringBuffer();

String[] ary = new DateFormatSymbols().getShortWeekdays();

for(int i = Calendar.SUNDAY; i = Calendar.SATURDAY; i++){

sb.append(ary[i]+ "t");

}

return sb.toString();

}

private static void generateMonthlyCalendar(int year, int month) {

Calendar cal = Calendar.getInstance();

cal.set(Calendar.YEAR, year);

cal.set(Calendar.MONTH, month);

cal.set(Calendar.DATE, 1);

int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

int i = 0;

for(i = Calendar.SUNDAY; i cal.get(Calendar.DAY_OF_WEEK); i++){

System.out.print(" t");

}

while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY){

System.out.print(cal.get(Calendar.DATE) + "t");

cal.add(Calendar.DATE, 1);

}

int weekDay = Calendar.SATURDAY;

int day = cal.get(Calendar.DATE);

while(day = maxDay){

if(weekDay == Calendar.SATURDAY){

System.out.println();

weekDay = Calendar.SUNDAY;

}else{

weekDay++;

}

System.out.print(day++ + "t");

}

}

}

--------------------------------JDK 1.5结果

------- Calendar For Year 2011 ----------------

********** January *********

Sun Mon Tue Wed Thu Fri Sat

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31

********** February *********

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28

********** March *********

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

********** April *********

Sun Mon Tue Wed Thu Fri Sat

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

********** May *********

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

********** June *********

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30

********** July *********

Sun Mon Tue Wed Thu Fri Sat

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

********** August *********

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

********** September *********

Sun Mon Tue Wed Thu Fri Sat

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30

********** October *********

Sun Mon Tue Wed Thu Fri Sat

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31

********** November *********

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30

********** December *********

Sun Mon Tue Wed Thu Fri Sat

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

JAVA万年历

//日历使用的类

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import java.util.Calendar;

import javax.swing.JApplet;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JSpinner;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.ListSelectionModel;

import javax.swing.Timer;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.TableCellRenderer;

import javax.swing.table.TableModel;

//日历

public class MyCalendar extends JApplet {

public static final String WEEK_SUN = "SUN";

public static final String WEEK_MON = "MON";

public static final String WEEK_TUE = "TUE";

public static final String WEEK_WED = "WED";

public static final String WEEK_THU = "THU";

public static final String WEEK_FRI = "FRI";

public static final String WEEK_SAT = "SAT";

public static final Color background = Color.yellow;

public static final Color foreground = Color.black;

public static final Color headerBackground = Color.blue;

public static final Color headerForeground = Color.white;

public static final Color selectedBackground = Color.blue;

public static final Color selectedForeground = Color.white;

private JPanel cPane;

private JLabel yearsLabel;

private JSpinner yearsSpinner;

private JLabel monthsLabel;

private JComboBox monthsComboBox;

private JTable daysTable;

private AbstractTableModel daysModel;

private Calendar calendar;

int delay = 1000;

public MyCalendar() {

cPane = (JPanel) getContentPane();

}

public void init() {

setSize(350, 300);

cPane.setLayout(new BorderLayout());

calendar = Calendar.getInstance();

yearsLabel = new JLabel("Year: ");

yearsSpinner = new JSpinner();

yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));

yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));

yearsSpinner.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent changeEvent) {

int day = calendar.get(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, 1);

calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue())

.intValue());

int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

calendar

.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day);

updateView();

}

});

JPanel yearMonthPanel = new JPanel();

cPane.add(yearMonthPanel, BorderLayout.NORTH);

yearMonthPanel.setLayout(new BorderLayout());

yearMonthPanel.add(new JPanel(), BorderLayout.CENTER);

JPanel yearPanel = new JPanel();

yearMonthPanel.add(yearPanel, BorderLayout.WEST);

yearPanel.setLayout(new BorderLayout());

yearPanel.add(yearsLabel, BorderLayout.WEST);

yearPanel.add(yearsSpinner, BorderLayout.CENTER);

monthsLabel = new JLabel("Month: ");

monthsComboBox = new JComboBox();

for (int i = 1; i = 12; i++) {

monthsComboBox.addItem(new Integer(i));

}

monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));

monthsComboBox.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent actionEvent) {

int day = calendar.get(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, 1);

calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());

int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

calendar

.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day);

updateView();

}

});

JPanel monthPanel = new JPanel();

yearMonthPanel.add(monthPanel, BorderLayout.EAST);

monthPanel.setLayout(new BorderLayout());

monthPanel.add(monthsLabel, BorderLayout.WEST);

monthPanel.add(monthsComboBox, BorderLayout.CENTER);

daysModel = new AbstractTableModel() {

public int getRowCount() {

return 9;

}

public int getColumnCount() {

return 7;

}

public Object getValueAt(int row, int column) {

if (row == 0) {

return getHeader(column);

}

row--;

Calendar calendar = (Calendar) MyCalendar.this.calendar.clone();

calendar.set(Calendar.DAY_OF_MONTH, 1);

int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;

int index = row * 7 + column;

int dayIndex = index - moreDayCount + 1;

if (index moreDayCount || dayIndex dayCount) {

return null;

} else {

return new Integer(dayIndex);

}

}

};

daysTable = new CalendarTable(daysModel, calendar);

daysTable.setCellSelectionEnabled(true);

daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

daysTable.setDefaultRenderer(daysTable.getColumnClass(0),

new TableCellRenderer() {

public Component getTableCellRendererComponent(

JTable table, Object value, boolean isSelected,

boolean hasFocus, int row, int column) {

String text = (value == null) ? "" : value.toString();

JLabel cell = new JLabel(text);

cell.setOpaque(true);

if (row == 0) {

cell.setForeground(headerForeground);

cell.setBackground(headerBackground);

} else {

if (isSelected) {

cell.setForeground(selectedForeground);

cell.setBackground(selectedBackground);

} else {

cell.setForeground(foreground);

cell.setBackground(background);

}

}

return cell;

}

});

updateView();

cPane.add(daysTable, BorderLayout.CENTER);

// 窗体添加事件监听,监听秒表的触发

ActionListener taskPerformer = new ActionListener() {

public void actionPerformed(ActionEvent evt) {

repaint();

}

};

new Timer(delay, taskPerformer).start();

}

public void paint(Graphics g) {

int hh, mm, ss;

Calendar now;

String st;

// 获取时间

now = Calendar.getInstance();

hh = now.get(Calendar.HOUR_OF_DAY);// 小时

mm = now.get(Calendar.MINUTE);// 分钟

ss = now.get(Calendar.SECOND);// 秒

g.setColor(Color.WHITE);

g.fillRect(5, 250, 150, 30);// 填充的矩形

g.setColor(Color.BLACK);

if (hh 10)

st = "0" + hh;

else

st = "" + hh;

if (mm 10)

st = st + ":0" + mm;

else

st = st + ":" + mm;

if (ss 10)

st = st + ":0" + ss;

else

st = st + ":" + ss;

{

g.setFont(new Font("华文楷体", Font.BOLD, 16));

g.drawString("系统时间:" + st, 10, 270);

}

}

public static String getHeader(int index) {

switch (index) {

case 0:

return WEEK_SUN;

case 1:

return WEEK_MON;

case 2:

return WEEK_TUE;

case 3:

return WEEK_WED;

case 4:

return WEEK_THU;

case 5:

return WEEK_FRI;

case 6:

return WEEK_SAT;

default:

return null;

}

}

public void updateView() {

daysModel.fireTableDataChanged();

daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),

calendar.get(Calendar.WEEK_OF_MONTH));

daysTable.setColumnSelectionInterval(

calendar.get(Calendar.DAY_OF_WEEK) - 1, calendar

.get(Calendar.DAY_OF_WEEK) - 1);

daysTable.setColumnSelectionInterval(0,0);

}

public static class CalendarTable extends JTable {

private Calendar calendar;

public CalendarTable(TableModel model, Calendar calendar) {

super(model);

this.calendar = calendar;

}

public void changeSelection(int row, int column, boolean toggle,

boolean extend) {

super.changeSelection(row, column, toggle, extend);

if (row == 0) {

return;

}

Object obj = getValueAt(row, column);

if (obj != null) {

calendar.set(Calendar.DAY_OF_MONTH, ((Integer) obj).intValue());

}

}

}

public static void main(String[] args) {

JFrame frame = new JFrame("简易时间日历");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyCalendar myCalendar = new MyCalendar();

myCalendar.init();

frame.getContentPane().add(myCalendar);

frame.setLocation(330, 80);

frame.setSize(360, 212);

frame.setVisible(true);

}

// 滚动字

public static class RollbyJFrame extends JFrame implements ActionListener,

FocusListener, javax.swing.event.ChangeListener {

private JTextField text;

private JSpinner spinner;

private Timer timer;

private JButton button;

public void focusGained(FocusEvent e) // 获得焦点时

{

if (e.getSource() == text) {

timer.stop();

}

}

public void focusLost(FocusEvent e) // 失去焦点时

{

if (e.getSource() == text) {

timer.restart();

}

}

public void stateChanged(ChangeEvent e) {

if (e.getSource() == spinner) {

timer.setDelay(new Integer("" + spinner.getValue())); // 设置延时的时间间隔

}

}

public void actionPerformed(ActionEvent e) // 定时器定时执行事件

{

if (e.getSource() == button)

;

else {

String temp = text.getText();

temp = temp.substring(1) + temp.substring(0, 1);

text.setText(temp);

}

}

public void buttondown(ActionEvent e) // 单击事件

{

if (e.getSource() == button) {

}

;

}

}

}

JAVA万年历代码

/*

题目:输出任意年份任意月份的日历表(公元后)

思路:

    1.已知1年1月1日是星期日,1 % 7 = 1 对应的是星期日,2 % 7 = 2 对应的是星期一,以此类推;

    2.计算当年以前所有天数+当年当月1号之前所有天数;

      a.年份分平年闰年,平年365天,闰年366天;

      b.闰年的判断方法year % 400 == 0 || (year % 100 != 0  year % 4 == 0)若为真,则为闰年否则为平年;

      c.定义平年/闰年数组,包含各月天数;

      d.遍历数组求和,计算当年当月前总天数;

      e.当年以前所有天数+当年当月前总天数+1即为1年1月1日到当年当月1日的总天数;

    3.总天数对7取模,根据结果判断当月1号是星期几,输出空白区域;

    4.输出当月日历表,逢星期六换行

*/

import java.util.Scanner;

class FindMonthList {

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入年份:");

        int year = sc.nextInt();            //年份

        if (year  1) {                        //判断非法输入年份

            System.out.println("输入错误!");

            return;

        }

        System.out.println("请输入月份:");

        int month = sc.nextInt();            //月份

        if (month  1 || month  12) {        //判断非法输入月份

            System.out.println("输入错误!");

            return;

        }

        //输出表头

        System.out.println("-------" + year + " 年 " + month + " 月 " + "-------");

        System.out.println();

        System.out.println("日  一  二  三  四  五  六");

        //计算当前年份以前所有天数beforeYearTotalDay;每4年一个闰年,闰年366天,平年365天

        int beforeYearTotalDay = ((year - 1) / 4 * 366) + (year-1 - ((year - 1) / 4)) * 365;

        int[] arrLeapYear = {0,31,29,31,30,31,30,31,31,30,31,30,31};    //闰年各月天数    int数组

        int[] arrNormalYear = {0,31,28,31,30,31,30,31,31,30,31,30,31};    //平年各月天数    int数组

        int beforeMonthTotalDay = 0;                                    //定义本年当月之前月份的总天数

        if (year % 400 == 0 || (year % 100 != 0  year % 4 == 0)) {    //判断当前年份是否是闰年

            for (int i = 0 ; i  month ; i ++ ) {    //for循环计算当月之前总天数

                //计算当前月份之前的所有天数

                beforeMonthTotalDay = beforeMonthTotalDay + arrLeapYear[i];

            }

            //判断当月1日是星期几

            int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;

            int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日

            for (int i = 0 ; i  (week - 1 + 7) % 7 ; i ++) {    //如果写成i  (week-1)会出现i-1的情况

                System.out.print("    ");//输出开头空白

            }

            for (int i = 1 ;i = arrLeapYear[month] ;i ++ ) {    //for循环输出各月天数

                System.out.print(i + "  ");

                if (i  10 ) {        //小于10的数补一个空格,以便打印整齐

                    System.out.print(" ");

                }

                if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrLeapYear[month]) {//每逢星期六/尾数换行

                    System.out.println();

                }

            }

        } else {        //不是闰年就是平年

            for (int i = 0 ; i  month ; i ++ ) {    //for循环计算出当月之前月份总天数

                beforeMonthTotalDay = beforeMonthTotalDay + arrNormalYear[i];

            }

            //判断当月1日是星期几

            int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;

            int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日

            for (int i = 0 ; i  (week - 1 + 7) % 7 ; i ++) {    //如果写成i  (week-1)会出现i-1的情况

                System.out.print("    ");//输出开头空白

            }

            for (int i = 1 ;i = arrNormalYear[month] ;i ++ ) {//for循环输出各月天数

                System.out.print(i + "  ");

                if (i  10 ) {            //小于10的数补一个空格,以便打印整齐

                    System.out.print(" ");

                }

                if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrNormalYear[month]) {//每逢星期六/尾数换行

                    System.out.println();

                }

            }

        }

    }

}

显示效果:

java编写万年历程序

希望我的回答能得到分~呵呵

/**

* @(#)AidyCalender.java

*

*

* @author

* @version 1.00 2008/7/19

*/

import java.awt.*;

import java.awt.event.*;

import java.lang.StringBuffer;

import javax.swing.*;

import java.util.*;

import javax.swing.Timer;

import javax.swing.border.*;

public class AidyCalender extends JFrame implements ActionListener,ItemListener{

Date date = new Date();

private GregorianCalendar gregorianCalendar = new GregorianCalendar();

//定义中英文字符数组存储星期信息,用于转换显示

private String[] stringWeekEn = new String[] { "SUN", "MON", "TUE", "WED",

"THU", "FRI", "SAT" };

private String[] stringWeekCn = new String[] { "星期日", "星期一", "星期二", "星期三",

"星期四", "星期五", "星期六" };

//定义存储月份的信息数组,用于转换显示方示

private String[] stringMonthEn = new String[] { "Jan", "Feb", "Mar", "Apr",

"May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };

private String[] stringMonthCn = {"一月","二月","三月","四月","五月","六月",

"七月","八月","九月","十月","十一月","十二月"};

private String[] sysNowTime = new String[6];//sysNowTime 用于存储系统时间的变量

private String[] sysRunTime = new String[6];

private JLabel []labelWeek = new JLabel[7];

private JLabel []labelDay = new JLabel[42];

private JLabel labelTime = new JLabel();

private JPanel panel1 = new JPanel();

private JPanel panel2 = new JPanel();

private JPanel panel3 = new JPanel();

private JComboBox combo1 = new JComboBox();

private JComboBox combo2 = new JComboBox();

private JButton buttonToday = new JButton();

private Border border = BorderFactory.createRaisedBevelBorder();

private Border border1 = BorderFactory.createLineBorder(Color.cyan,3);

public AidyCalender(String title) {

super(title);

for (int y = 1900; y 2101; y++) {

combo1.addItem(" " + new Integer(y).toString()+"年");

}

for (int m = 0;m12;m++){

combo2.addItem(" "+stringMonthCn[m]);

}

buttonToday.setText("今 天");

setLayout(new FlowLayout());

add(panel1);

add(panel2);

add(panel3);

panel1.setLayout(new GridLayout(1,3,10,0));

panel1.add(combo1);

combo1.addItemListener(this);

panel1.add(combo2);

combo2.addItemListener(this);

panel1.add(buttonToday);

buttonToday.addActionListener(this);

labelTime.setFont(new Font("宋体",Font.PLAIN,16));

labelTime.setForeground(Color.MAGENTA);

panel1.add(labelTime);

Timer time = new Timer(1000,new TimerListener());

time.addActionListener(new TimerListener());

//time.setRepeats(true);

time.start();

//labelTime.addAncestorListener(new TimerListener());

panel2.setLayout(new GridLayout(7,7,0,10));

panel2.setBackground(Color.white);

for(int i=0;i7;i++){

labelWeek[i] = new JLabel();

labelWeek[i].setHorizontalAlignment(0);

if(i==0||i==6){

labelWeek[i].setBackground(Color.blue);

labelWeek[i].setForeground(Color.RED);

labelWeek[i].setFont(new Font("黑体",Font.BOLD,14));

}

else{

labelWeek[i].setForeground(Color.BLACK);

labelWeek[i].setFont(new Font("新宋体",Font.PLAIN,14));

}

labelWeek[i].setText(stringWeekCn[i]);

panel2.add(labelWeek[i]);

}

for(int i= 0;i42;i++){

labelDay[i] = new JLabel();

labelDay[i].setHorizontalAlignment(0);

labelDay[i].setText("");

panel2.add(labelDay[i]);

}

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

setSize(300,300);

setBounds(250, 200, 400, 360);

setVisible(true);

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

getSysDate();

setNowDate();

}

public void actionPerformed(ActionEvent ae){

if(ae.getSource()==buttonToday){

setNowDate();

}

}

public void itemStateChanged(ItemEvent aa){

setChangeDate();

}

public int turnWeek(String week){

int i;

for(i=0;i7;i++)

if(week.equalsIgnoreCase(stringWeekEn[i]))

break;

return i;

}

public int turnMonth(String month){

/**

*int turnMonth(String month)

*@month 系统日期中的月,诸如JanFeb

*@return int

*返回一个整数值,用于寻找stringMonthCn[]数组中对应的中文月份

*/

int i;

for(i=0;i12;i++)

if(month.equalsIgnoreCase(stringMonthEn[i]))

break;

return i;

}

/**

*setNowDate()

*设置当前系统日期

*/

public void setNowDate(){

setSysDate(getNowYear(),getNowMonth());

getSysRunDate();

setDateNull();

combo1.setSelectedIndex(getShowYear() - 1900);

combo2.setSelectedIndex(getShowMonth());

setDays(getMonthDays(getNowYear(),getNowMonth()),getInitWeek(sysRunTime[0]),getNowDay());

//labelTime.setText(sysNowTime[3]);

//labelTime.setHorizontalAlignment(0);

}

/**Integer getShowYear()

*获取组合框中应该显示的年份

*/

public void setSysDate(int year,int month){

gregorianCalendar.set(year,month,1);

}

public void setDateNull(){

for(int i=0;i42;i++){

labelDay[i].setText("");

}

}

public void setChangeDate(){

setSysDate(getComboYear(),getComboMonth());

getSysRunDate();

setDateNull();

setDays(getMonthDays(getComboYear(),getComboMonth()),getInitWeek(sysRunTime[0]),-1);

}

public int getMonthDays(int year, int month) {

/**

*返回所选年月的天数,因为数组中的数值从0开始,所以35810分别代表46911几个小月.

*而1代表2月,经过是否为闰年判断,选择返回28或29天.

*其余月份为大月,返回31天.

**/

switch (month) {

case 3:

case 5:

case 8:

case 10:

return 30;//小月返回30天

case 1:

if (gregorianCalendar.isLeapYear(year)) {

//isLeapYear(year)确定当前纪元中的指定年份是否为闰年。

return 29;

} else {

return 28;

}//闰年的二月返回29天,平年返回28天

default:

return 31;

//大月返回31天

}

}

/**

*int getComboYear()

*获取组合框中的年份

*/

public void getSysDate(){

date = gregorianCalendar.getTime();

sysNowTime = (date.toString()).split(" ");

}

public void getSysRunDate(){

date = gregorianCalendar.getTime();

sysRunTime = (date.toString()).split(" ");

}

public int getComboYear(){

return combo1.getSelectedIndex()+1900;

}

/**

*int getComboMonth()

*获取月组合框中的整数值,

*/

public int getComboMonth(){

return combo2.getSelectedIndex();

}

public int getInitWeek(String initWeek){

/**

*getWeekNow(String initWeek)

*@para nowWeek 系统日期中的星期

*返回当月中的1号是从星期几开始

*/

int nowWeek = 0 ;

for(int i = 0;i7;i++){

if(initWeek.equalsIgnoreCase(stringWeekEn[i])){

nowWeek = i;

break;

}

}

return nowWeek;

}

public int getNowYear(){

return Integer.parseInt(sysNowTime[5]);

}

public int getNowMonth(){

int nowMonth=0;

for(int i=0;i12;i++){

if(sysNowTime[1].equalsIgnoreCase(stringMonthEn[i]));

nowMonth=i;

break;

}

return nowMonth;

}

public int getNowDay(){

return Integer.parseInt(sysNowTime[2]);

}

public Integer getShowYear(){

return Integer.parseInt(sysNowTime[5]);

}

public Integer getShowMonth(){

/**

*Integer getShowMonth()

*获取在组给框中显示的中文格式月份:如七月八月等

*/

return turnMonth(sysNowTime[1]);

}

public void setDays(int monthDays,int initWeek,int day){

/**

*void setDays(int monthDays,int initWeek,int day)

*@para monthDays 本月天数

*@para initWeek 初始星期

*@para day 今天日

*设置月历

*/

setDateNull();

for(int i=initWeek;iinitWeek+monthDays+1;i++){

if((i-initWeek+1)==day){

labelDay[i].setBorder(border1);

labelDay[i].setForeground(Color.BLUE);

labelDay[i].setFont(new Font("黑体",Font.BOLD,20));

}else if((i%7==0)||(i%7==6))

labelDay[i].setForeground(Color.RED);

else{

labelDay[i].setForeground(Color.BLACK);

}

labelDay[i].setText(String.valueOf(i-initWeek+1));

}

for(int i=initWeek+monthDays;i42;i++)

labelDay[i].setText("");

}

class TimerListener implements ActionListener{

//AdapterDemo var=new AdapterDemo("万年历程序--Aidy");

public void actionPerformed(ActionEvent e) {

GregorianCalendar g = new GregorianCalendar();

String clock = new String((g.getTime().toString().split(" "))[3]);

labelTime.setText(clock);

}

}

public static void main(String args[])

{

try{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exception e){

throw new RuntimeException(e);

}

AidyCalender var=new AidyCalender("万年历程序--question_bai");

}

}

java万年历源代码是多少?

package org.java.test;x0dx0ax0dx0aimport java.util.Scanner;x0dx0apublic class CalendarTest{x0dx0apublic static void main(String[] args) {x0dx0aSystem.out.println("欢 迎 使 用 万 年 历");x0dx0aScanner input = new Scanner(System.in);x0dx0aSystem.out.print("n请选择年份: ");x0dx0aint year = input.nextInt();x0dx0aSystem.out.print("n请选择月份: ");x0dx0aint month = input.nextInt();x0dx0aSystem.out.println();x0dx0aint days = 0; // 存储当月的天数x0dx0aboolean isRn;x0dx0a/* 判断是否是闰年 */x0dx0aif (year % 4 == 0 !(year % 100 == 0) || year % 400 == 0) { // 判断是否为闰年x0dx0aisRn = true; // 闰年x0dx0a} else {x0dx0aisRn = false;// 平年x0dx0a}x0dx0a/* 计算输入的年份之前的天数 */x0dx0aint totalDays = 0;x0dx0afor (int i = 1900; i

回答于 2022-11-16

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。