import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
class TestDateFormat
{
int year;
int date;
String month;
String week;
String temp;
String tempYear;
String tempDate;
Date aDate;
SimpleDateFormat dateFormat=null;
TestDateFormat(String temp)
{
this.temp=temp;
this.check();
}
TestDateFormat()
{
temp=this.systemIn();
this.check();
}
public void check() //通过检索字符,为年月日初始化
{
if(temp.indexOf("/")!=-1) //第一种格式,为年月日初始化 即MM/dd/yyyy型
{
month=temp.substring(0, temp.indexOf("/"));
tempDate=temp.substring(temp.indexOf("/")+1,temp.indexOf("/", temp.indexOf("/")+1));
date=Integer.parseInt(tempDate);
tempYear=temp.substring(temp.indexOf("/", temp.indexOf("/")+1)+1,temp.length());
year=Integer.parseInt(tempYear);
if(year<1800||year>2100)
{
System.out.println("输入年限超出范围,请重新输入");
this.systemIn();
}
}
else if(temp.indexOf(" ")!=-1&&temp.indexOf(",")!=-1) //第二种格式 即MM dd,yyyy型
{
month=temp.substring(0,temp.indexOf(" "));
tempDate=temp.substring(temp.indexOf(" ")+1,temp.indexOf(","));
date=Integer.parseInt(tempDate);
tempYear=temp.substring(temp.indexOf(","), temp.length());
year=Integer.parseInt(tempYear);
if(year<1800||year>2100)
{
System.out.println("输入年限超出范围,请重新输入");
this.systemIn();
}
}
}
public String systemIn() //键盘输入字符串
{
String s="";
System.out.println("请输入:\n 如:3/18/2007");
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
}
catch(IOException e)
{
System.out.println("产生异常");
}
System.out.println("你输入的字符串是"+s);
return s;
}
public String stringFormat() //符合Date要求的字符串
{
return tempYear+"-"+month+"-"+tempDate;
}
public Date dateFormat() //将字符串转化为Date对象
{
SimpleDateFormat df = new SimpleDateFormat("d-MMM-yyyy", Locale.ENGLISH);
try
{
aDate = df.parse(this.stringFormat());
}
catch (ParseException e)
{
e.printStackTrace();
}
return aDate;
}
public String toString()
{
dateFormat = new SimpleDateFormat("MM dd,yyyy,E");
return "输出:"+dateFormat.format(aDate).toString();
}
}
public class Bh
{
public static void main(String[] args)
{
TestDateFormat temp=new TestDateFormat();
System.out.println(temp.toString());
}
}