但得到的结果不对,还有如果选择月份对应的日期不存在自动修改
例如:4月31日应该自动显示5月1日
请高手帮忙!谢了
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Calendar;
class fram extends JFrame implements ActionListener{
JComboBox cbxYear=new JComboBox();
JComboBox cbxMonth=new JComboBox();
JComboBox cbxDay=new JComboBox();
JLabel lbl1=new JLabel("年");
JLabel lbl2=new JLabel("月");
JLabel lbl3=new JLabel("日");
JLabel lbl4=new JLabel("");
JButton btn=new JButton("计算星期");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
String y,d;
int m;
int z;
Calendar calendar=Calendar.getInstance();
fram(){
Container c=this.getContentPane();
for (int i = 1990; i<=2050; i++){
cbxYear.addItem(new Integer(i));
}
for (int i = 0; i<12; i++){
cbxMonth.addItem(new Integer(i));
}
for (int i = 1; i<=31; i++){
cbxDay.addItem(new Integer(i));
}
cbxYear.setSelectedItem(new Integer(calendar.get(Calendar.YEAR)));
cbxMonth.setSelectedItem(new Integer(calendar.get(Calendar.MONTH)));
cbxDay.setSelectedItem(new Integer(calendar.get(Calendar.DAY_OF_MONTH)));
c.add(p1,BorderLayout.NORTH);
c.add(p2,BorderLayout.CENTER);
p1.add(cbxYear);
p1.add(lbl1);
p1.add(cbxMonth);
p1.add(lbl2);
p1.add(cbxDay);
p1.add(lbl3);
p1.add(btn);
p2.add(lbl4);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
y=String.valueOf(cbxYear.getSelectedItem());
m=cbxMonth.getSelectedIndex()+1;
d=String.valueOf(cbxDay.getSelectedItem());
calendar.set(Calendar.YEAR,Integer.parseInt(y));
calendar.set(Calendar.MONTH,m);
calendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(d));
lbl4.setText(y+"年"+m+"月"+d+"日是星期"+calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
public static void main(String agrs[]){
fram f=new fram();
f.setSize(400,200);
f.show();
}
}
----------------解决方案--------------------------------------------------------
仔细看看Calendar类就可以了
----------------解决方案--------------------------------------------------------
希望楼上斑竹能说清楚点 我看了很久 但是没看出眉目来`
----------------解决方案--------------------------------------------------------
用异常处理吗。当输入的日期不能转化时就进入catch块,然后再用条件语句将错误的日期变成正确的日期再输出不就行了吗。个人见解。
----------------解决方案--------------------------------------------------------