| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 948 人关注过本帖
标题:求助!
只看楼主 加入收藏
tntchenmingye
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-12-10
收藏
 问题点数:0 回复次数:5 
求助!
编写程序,提示用户输入年号和该年的第一天的星期,在显示器上显示该年的月历,例如2005年,2005年1月1日为星期六,则显示为
         January   2005
 ---------------------------------------------
 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
                 
                 
                 
        December  2005
   --------------------------------------------



请给个思路,需要每个月都分开写吗?还有怎样用循环使日期在星期六之后换行?谢谢

[[it] 本帖最后由 tntchenmingye 于 2008-3-19 20:23 编辑 [/it]]
搜索更多相关主题的帖子: Sun 年号 显示器 Tue 
2008-03-19 20:22
谁与争疯
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:海南省
等 级:版主
威 望:191
帖 子:15071
专家分:17513
注 册:2007-4-22
收藏
得分:0 
使用java.util.GorgreianCalendar这个类
它是一个日历类吧,简单地是这样说。
你查一查API,便知道他的使用方法。

论坛是我家灌水靠大家
2008-03-19 20:31
Alistair001
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-2-24
收藏
得分:0 
这是一段在《JAVA核心技术》中的代码,你看看吧
import java.util.*;

public class CalendarTest
{  
   public static void main(String[] args)
   {  
      // construct d as current date
      GregorianCalendar d = new GregorianCalendar();

      int today = d.get(Calendar.DAY_OF_MONTH);
      int month = d.get(Calendar.MONTH);
      
     int year = d.get(Calendar.YEAR);
      System.out.println(year+"年"+month+"月"+today+"日");

      // set d to start date of the month
      d.set(Calendar.DAY_OF_MONTH, 1);

      int weekday = d.get(Calendar.DAY_OF_WEEK);

      // print heading
      System.out.println("Sun Mon Tue Wed Thu Fri Sat");

      // indent first line of calendar
      for (int i = Calendar.SUNDAY; i < weekday; i++ )
         System.out.print("    ");

      do
      {  
         // print day
         int day = d.get(Calendar.DAY_OF_MONTH);
         if (day < 10) System.out.print(" ");
         System.out.print(day);

         // mark current day with *
         if (day == today)
            System.out.print("* ");
         else
            System.out.print("  ");

         // start a new line after every Saturday
         if (weekday == Calendar.SATURDAY)
            System.out.println();

         // advance d to the next day
         d.add(Calendar.DAY_OF_MONTH, 1);
         weekday = d.get(Calendar.DAY_OF_WEEK);
      }
      while (d.get(Calendar.MONTH) == month);
      // the loop exits when d is day 1 of the next month

      // print final end of line if necessary
      if (weekday != Calendar.SUNDAY)
         System.out.println();
   }
}
2008-03-19 21:45
谁与争疯
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:海南省
等 级:版主
威 望:191
帖 子:15071
专家分:17513
注 册:2007-4-22
收藏
得分:0 
哈哈,楼上的代码我也打过。只不过后来我改了之后就出错了!
我以前在main里改的!

论坛是我家灌水靠大家
2008-03-19 21:56
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
我在用标签改呢...不怎么想改了..呵呵..

学习需要安静。。海盗要重新来过。。
2008-03-19 22:57
wu1011
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2007-11-18
收藏
得分:0 
我写一个 看看 呵呵
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class MyDate extends JFrame{
   Calendar now;
   String temp;
  
   int day_month[] = {31,29,31,30,31,30,31,31,30,31,30,31};
  
   int first = 0;
   JComboBox jcb_month;
   JTextField year_jtf;
   JPanel jpanel,jpanel_date;
   JTextArea jta;
   JLabel year_jlabel,month_jlabel;
   MyDate(){
      now = Calendar.getInstance();
      jpanel = new JPanel();
      year_jlabel = new JLabel("年");
      year_jtf = new JTextField("2008");
      jcb_month = new JComboBox(new String[]{"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"});
      month_jlabel = new JLabel("月");
      jpanel.add(year_jtf);
      jpanel.add(year_jlabel);
      jpanel.add(jcb_month);
      jpanel.add(month_jlabel);

      jpanel_date = new JPanel();
      jta = new JTextArea();
      jpanel_date.add(jta);

      getContentPane().add(jpanel,BorderLayout.NORTH);
      getContentPane().add(jpanel_date,BorderLayout.CENTER);

      pack();
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      int now_year = now.get(Calendar.YEAR);
      int now_month = now.get(Calendar.MONTH);
      jcb_month.setSelectedIndex(now_month);
      now.set(Calendar.DAY_OF_MONTH,1);
      int now_day_of_week = now.get(Calendar.DAY_OF_WEEK);
      int row_count = 0;
      temp = "日      "+"一      " + "二     "+"三      "+"四      "+"五      "+"六       "+"\n";
       for(int i=1;i<now_day_of_week;i++){
             temp = temp + "          ";
       }
        for(int j=1;j<=day_month[now_month];j++){
             if( j < 10)
                temp = temp + j + "       ";
             else
                 temp = temp + j +"     ";
             row_count ++;
             if(row_count + now_day_of_week == 8 || row_count == 7){
                temp = temp + "\n";
                row_count= 0;
                now_day_of_week = 0;
             }
         }
         jta.append(temp);
         temp = "";
         
          ItemHandler handler = new ItemHandler();
          jcb_month.addItemListener(handler);
      
  }
   
    class ItemHandler implements ItemListener{
      public void itemStateChanged(ItemEvent e){
         temp = "";
         jta.setText("");
         int row_count = 0;
            
         temp = "日      "+"一      " + "二     "+"三      "+"四      "+"五      "+"六      "+"\n";
         int selected_month = jcb_month.getSelectedIndex();
         int get_year = Integer.parseInt(year_jtf.getText().trim());
         now.set(Calendar.MONTH, selected_month);
         now.set(Calendar.YEAR, get_year);
         now.set(Calendar.DAY_OF_MONTH,1);

         int day_of_week = now.get(Calendar.DAY_OF_WEEK);
        
         for(int i=1;i<day_of_week;i++){
             temp = temp + "          ";
         }
         for(int j=1;j<=day_month[selected_month];j++){
             if( j < 10)
                temp = temp + j + "       ";
             else
                 temp = temp + j +"     ";
             row_count ++;
             if(row_count + day_of_week == 8 || row_count == 7){
                temp = temp + "\n";
                row_count= 0;
                day_of_week = 0;
             }
         }
         jta.append(temp);
         temp = "";
      
      }
    }
    public static void main(String args[]){
       new MyDate();
    
    }
 }
2008-03-20 22:19
快速回复:求助!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.066209 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved