编写一个程序,用于显示特定年份中某个月的日历,月和年应从命令行参数接受,如果用户没有输入月和年,则应显示当前月和年的日历。
1 将月份名称和每个月的天数 储存在数组中
2 获取特定月份名称和最大天数
3 创建 GregorianCalendar类的实例
给我一点提示吧!!

解决方案 »

  1.   

    int year=2004;
    int month=10;
    Calendar c = GregorianCalendar.getInstance();
    c.set(Calendar.YEAR, year);//2004年
    c.set(Calendar.MONTH, month-1);//10月
    c.set(Calendar.DAY_OF_MONTH, 1);//1日
    System.out.println(c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月");
    System.out.println("日 一 二 三 四 五 六");
    int[][] day = new int[6][7];

    while(c.get(Calendar.MONTH)+1==month){
    int i=c.get(Calendar.WEEK_OF_MONTH)-1;
    int j=c.get(Calendar.DAY_OF_WEEK)-1;
    day[i][j] = c.get(Calendar.DAY_OF_MONTH);
    c.add(Calendar.DATE, 1);
    }
    for(int i=0;i<6;i++){
    for(int j=0;j<7;j++){
    String out = day[i][j]+"";
    if(out.equals("0"))out=" ";
    if(out.length()==1)out=" "+out;
    System.out.print(out+" ");
    } System.out.println();
    }
      

  2.   

    不知道楼主搞这么复杂的日历做什么?
    Java可以做一个日历控件,和delphi、VC中的日历控件相当。
    难道楼主要做更复杂功能的么?
      

  3.   

    shan1119(大天使,卐~解!) ( ) 信誉:101 这东西你也想的出来?
      

  4.   

    这里还有点
    http://community.csdn.net/Expert/topic/5753/5753747.xml?temp=.2584803
      

  5.   

    说一下啊
    我开始学Java的时候也做过这个题目
    当时真的觉得好难
    我看过北大青鸟的书
    第一学期的Java书上就有这个题目
    所以我才问了一下lz是不是北大青鸟的
      

  6.   

    我是北大青鸟的
    就是 谁能讲讲while后面那一段
    循环长度等于1和那些加一减一艾
      

  7.   

    package cs;import java.util.Vector;import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import javax.swing.JComboBox;public class MainFrame extends JFrame
    {
        private JComboBox cboYear;
        private JComboBox cboMonth;
        private JButton btnShow;
        private JTextArea txaDisplay;
        
        public MainFrame()
        {
            super("万年历");
            
            txaDisplay = new JTextArea();
            txaDisplay.setBackground(Color.BLACK);
            txaDisplay.setForeground(Color.GREEN);
            txaDisplay.setTabSize(4); //?
            txaDisplay.setEditable(false);
            
            Container me = this.getContentPane();
            me.setLayout(new BorderLayout());
            me.add(new NorthPanel(), BorderLayout.NORTH);
            me.add(new JScrollPane(txaDisplay), BorderLayout.CENTER); //经典
            
            txaDisplay.setText((new CalendarInfo()).toString());
            
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(305, 215);
            this.setResizable(false);
            this.setVisible(true);
        }
        
        private class NorthPanel extends JPanel implements ActionListener
        {
            public NorthPanel()
            {
                super(new FlowLayout());
                
                btnShow = new JButton("显示");
                btnShow.setToolTipText("点击显示日历...");
                btnShow.addActionListener(this);
                
                this.add(cboYear_initialize());
                this.add(new JLabel("年"));
                this.add(cboMonth_initialize());
                this.add(new JLabel("月"));
                this.add(btnShow);
            }
            
            public void actionPerformed(ActionEvent ae)
            {
                int year = ((Integer)(cboYear.getSelectedItem())).intValue();
                int month = ((Integer)(cboMonth.getSelectedItem())).intValue();
                try
                {
                    CalendarInfo ci = new CalendarInfo(year, month);
                    txaDisplay.setText(ci.toString());
                }
                catch (DateException de)
                {
                    txaDisplay.setText(de.getMessage());
                }
            }
            
            private JComboBox cboYear_initialize()
            {
                Vector<Integer> vecYear = new Vector<Integer>();
                for (int i = 1900; i <= 2100; i++)
                {
                    vecYear.add(new Integer(i));   
                }
                cboYear = new JComboBox(vecYear);
                int nowYear = CalendarInfo.getNowYear();
                cboYear.setSelectedItem(new Integer(nowYear));
                cboYear.setToolTipText("请选择年份");
                return (cboYear);
            }
            
            private JComboBox cboMonth_initialize()
            {
                Vector<Integer> vecMonth = new Vector<Integer>();
                for (int i = 1; i <= 12; i++)
                {
                    vecMonth.add(new Integer(i));   
                }
                cboMonth = new JComboBox(vecMonth);
                int nowMonth = CalendarInfo.getNowMonth();
                cboMonth.setSelectedItem(new Integer(nowMonth));
                cboMonth.setToolTipText("请选择月份");
                return (cboMonth);
            }
        } 
    }
      

  8.   

    package cs;import java.util.Calendar;public class CalendarInfo
    {
        private static Calendar calNow;
        
        private int mYear;
        private int mMonth;
        private int mDaysOfMonth;
        private int mFirstWeek;
        
        static
        {
            calNow = Calendar.getInstance();    
        }
        
        public static int getNowYear()
        {
            return (calNow.get(Calendar.YEAR));
        }
        
        public static int getNowMonth()
        {
            return (calNow.get(Calendar.MONTH) + 1);
        }
        
        public CalendarInfo()
        {
            mYear = getNowYear();
            mMonth = getNowMonth();
            mDaysOfMonth = getDaysOfMonth(mYear, mMonth);
            mFirstWeek = getFirstOfWeek(mYear, mMonth);
        }
        
        public CalendarInfo(int year, int month) throws DateException
        {
            mYear = year;
            if (month < 1 || month > 12)
            {
                throw (new DateException());
            }
            mMonth = month;
            mDaysOfMonth = getDaysOfMonth(mYear, mMonth);
            mFirstWeek = getFirstOfWeek(mYear, mMonth);
        }
        
        private int isLeapYear(int year)
        {
            return (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 1 : 0);
        }
        
        private int getDaysOfMonth(int year, int month)
        {
            int[][] days = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                            {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
            return (days[isLeapYear(year)][month]);
        }
        
        private int getFirstOfWeek(int year, int month)
        {
            Calendar cal = Calendar.getInstance();
            cal.set(year, month - 1, 1);
            return (cal.get(Calendar.DAY_OF_WEEK) - 1);
        }
        
        public String toString()
        {
            String str = "\t\t\t" + mYear + "年" + mMonth + "月\n";
            str += "日\t一\t二\t三\t四\t五\t六\n";
            
            int i;
            for (i = 1; i <= mFirstWeek; i++)
            {
                str += " \t";
            }
            for (int j = 1; j <= mDaysOfMonth; j++, i++)
            {
                str += (j + "\t");
                if (i % 7 == 0)
                {
                    StringBuffer sb = new StringBuffer(str);
                    sb.setCharAt(sb.length() - 1, '\n');
                    str = sb.toString();
                }
            }
            return (str);
        }
    }
      

  9.   

    package cs;public class DateException extends Exception
    {
        public DateException()
        {
            super("日期数据不合法!");
        }
        
        public DateException(String message)
        {
            super(message);
        }
    }