这里有一个可以计算出当天日期是星期几的例子,你可以参考一下
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
/*
<applet codebase="." code="Timetest" width="400" height="200">
</applet>
*/
public class Timetest extends JApplet implements ActionListener
{
JComboBox JcmbYear, JcmbMonth, JcmbDay;
JLabel JlblYear, JlblMonth, JlblDay, JlblResult;
JButton JbtnOK;
Calendar calendar;
int w = 0;
String str;
public void init()
{
Container contentPane = getContentPane();//得到内容板
contentPane.setLayout(new BorderLayout());//设置布局
calendar = Calendar.getInstance();//获得系统当前日期
//三个下拉列表
JcmbYear = new JComboBox();
JcmbMonth = new JComboBox();
JcmbDay = new JComboBox();
//四个标签
JlblYear = new JLabel("年");
JlblMonth = new JLabel("月");
JlblDay = new JLabel("日");
JlblResult = new JLabel("计算结果");
//一个按钮
JbtnOK = new JButton("计算星期");
//二个面板
JPanel JpTop = new JPanel();
JPanel JpRs = new JPanel();
//增加下拉列表项
for (int i=1990; i<=2050; i++)
JcmbYear.addItem(new Integer(i));
for (int i=1; i<=12; i++)
JcmbMonth.addItem(new Integer(i));
for (int i=1; i<=31; i++)
JcmbDay.addItem(new Integer(i));
//设置下拉列表项为当前日期
JcmbYear.setSelectedItem(new Integer(calendar.get(Calendar.YEAR)));
//Month 值以 0 开始。 例如,0 代表一月。
JcmbMonth.setSelectedItem(new Integer(calendar.get(Calendar.MONTH) + 1));
JcmbDay.setSelectedItem(new Integer(calendar.get(Calendar.DATE)));
//上面的面板布局
JpTop.add(JcmbYear);
JpTop.add(JlblYear);
JpTop.add(JcmbMonth);
JpTop.add(JlblMonth);
JpTop.add(JcmbDay);
JpTop.add(JlblDay);
JpTop.add(JbtnOK);
//中部面板布局
JpRs.add(JlblResult);
//将面板增加到小应用程序
contentPane.add(JpTop, BorderLayout.NORTH);
contentPane.add(JpRs, BorderLayout.CENTER);
//注册监听器
JbtnOK.addActionListener(this);
}
//判断时间是否合法,传进年份,月份,返回当月的天数
public int isLeap(int y, int m)
{
//记得两个变量得初始化
boolean b = false;
int count = 0;
if (y%4 == 0 || (y%400 == 0 && y%100 != 0))
b = true;
switch (m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
count = 31;
break;
case 4:
case 6:
case 9:
case 11:
count = 30;
break;
case 2:
if(b)
count = 29;
else
count = 28;
break;
}
return count;
}
public void actionPerformed(ActionEvent e)
{
//获得年月日的整型值
int y = Integer.parseInt(JcmbYear.getSelectedItem().toString());
int m = Integer.parseInt(JcmbMonth.getSelectedItem().toString());
int d = Integer.parseInt(JcmbDay.getSelectedItem().toString());
//调用函数判断时间的合法性
if (d > isLeap(y, m))//如果不合法
{
JcmbMonth.setSelectedIndex(JcmbMonth.getSelectedIndex() + 1);//月份进一
JcmbDay.setSelectedIndex(0);//天数归一
}
//将下拉列表项写入时间对象
calendar.set(Calendar.YEAR, Integer.parseInt(JcmbYear.getSelectedItem().toString()));
//Month 值以 0 开始。 例如,0 代表一月。
calendar.set(Calendar.MONTH, Integer.parseInt(JcmbMonth.getSelectedItem().toString()) - 1);
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(JcmbDay.getSelectedItem().toString()));
//获取星期几
w = calendar.get(Calendar.DAY_OF_WEEK);
//组合字符串
str = JcmbYear.getSelectedItem().toString() + JlblYear.getText();
str += JcmbMonth.getSelectedItem().toString() + JlblMonth.getText();
str += JcmbDay.getSelectedItem().toString() + JlblDay.getText();
switch (w)
{
case 1:
str += "是星期日";
break;
case 2:
str += "是星期一";
break;
case 3:
str += "是星期二";
break;
case 4:
str += "是星期三";
break;
case 5:
str += "是星期四";
break;
case 6:
str += "是星期五";
break;
case 7:
str += "是星期六";
break;
}
JlblResult.setText(str);
}
}