SimpleDateFormate的两个方法:parse(),format();
首先,我们对SimpleDateFormate这个模板指定模板例如:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
1.parse():将String的对象根据 模板提供的yyyy-mm-dd进行转化成为Date类型,如果String的对象不是指定的模板类型的话就会报错
2.format():将Date对象根据模板转化为String对象。
public class TestFile {
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
Date date = new Date();
System.out.println(date);
String d1 = new String("2012-02-12");
date = simpleDateFormat.parse(d1);//将2012-02-12转化为Thu Jan 12 00:02:00 CST 2012
System.out.println(date);
String d2 = simpleDateFormat.format(date);//将Thu Jan 12 00:02:00 CST 2012转化为2012-02-12
System.out.println(d2);
}
}