关于格式化日期的问题!
想作出以下效果输入1982 3 18
程序自动输出18-03-1982
但是public String toString()想了一上午都不知道怎么写 试了几个方法都行不通
谢谢
代码如下
import javax.swing.JOptionPane;
import java.text.SimpleDateFormat;
import java.util.Date;
class MyException extends Exception
{
public String toString()
{
return "请输入正确的日期!";
}
}
class MyDate
{
private int year;
private int month;
private int day;
public MyDate(int year,int month,int day)
{
this.year=year;
this.month=month;
this.day=day;
}
public void setDate()throws MyException
{
switch(month)
{ case 1:
case 3:
case 5:
case 7:
case 8:
case 11:
if(year>2007 || month>12 || day>31)
{
throw new MyException();
}
break;
case 2:
if((year%4==0 && year%100!=0) || year%400==0)
{
if(year>2007 || month>12 || day>29)
{
throw new MyException();
}
}
else
if(year>2007 || month>12 || day>28)
{
throw new MyException();
}
break;
case 4:
case 6:
case 9:
case 10:
case 12:
if(year>2007 || month>12 || day>28)
{
throw new MyException();
}
break;
}
}
public String toString()
{
}
}
public class Birthday
{
public static void main(String []args)
{
try
{
int year=Integer.parseInt(JOptionPane.showInputDialog("请输入出生年"));
int month=Integer.parseInt(JOptionPane.showInputDialog("请输入出生月"));
int day=Integer.parseInt(JOptionPane.showInputDialog("请输入出生日"));
MyDate sjm=new MyDate(year,month,day);
sjm.setDate();
JOptionPane.showMessageDialog(null,sjm.toString(),"你的生日是",JOptionPane.INFORMATION_MESSAGE);
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,e,"输入错误",JOptionPane.ERROR_MESSAGE);
}
catch(MyException e)
{
JOptionPane.showMessageDialog(null,e,"输入错误",JOptionPane.ERROR_MESSAGE);
}
}
}