编写一个程序,用于显示特定年份中某个月的日历,月和年应从命令行参数接受,如果用户没有输入月和年,则应显示当前月和年的日历。
1 将月份名称和每个月的天数 储存在数组中
2 获取特定月份名称和最大天数
3 创建 GregorianCalendar类的实例
给我一点提示吧!!
1 将月份名称和每个月的天数 储存在数组中
2 获取特定月份名称和最大天数
3 创建 GregorianCalendar类的实例
给我一点提示吧!!
解决方案 »
- java问题Can't Find Database Driver.
- For each 循环
- 请教 一个socket.getOutputStream 如何发送给多个客户端。
- 请问个CSS的基本问题
- HashMap<String,Integer>的getKey返回的居然不是对Integer引用,为什么?
- 请问各位高手,java有没有类似codeproject的网站
- 时间的'上午''下午'是什么格式的??
- 如何比较两个文件的内容,并且打印出两个文件中内容有差别的行呢?
- 北京公司招聘!
- 请问,那里能下载JBuilder5企业版?
- 求大侠帮我给这个c++程序改成java程序 (100分重谢)
- 多线程的问题
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();
}
Java可以做一个日历控件,和delphi、VC中的日历控件相当。
难道楼主要做更复杂功能的么?
http://community.csdn.net/Expert/topic/5753/5753747.xml?temp=.2584803
我开始学Java的时候也做过这个题目
当时真的觉得好难
我看过北大青鸟的书
第一学期的Java书上就有这个题目
所以我才问了一下lz是不是北大青鸟的
就是 谁能讲讲while后面那一段
循环长度等于1和那些加一减一艾
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);
}
}
}
{
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);
}
}
{
public DateException()
{
super("日期数据不合法!");
}
public DateException(String message)
{
super(message);
}
}