在使用DateFormat对象的parse()方法将字符串解析为日期时,
需要输入固定格式的字符串,这显然不够灵活。
为了能够更好地格式化日期、解析字符串,Java中提供了一个SimpleDateFormat类。
说明:SimpleDateFormat类是DateFormat类的子类,
它可以使用new关键字创建实例对象。
在创建实例对象时,它的构造方法需要接收一个表示日期格式模板的字符串参数。
package changYongLei;
import java.text.*;
import java.util.*;
public class GeShiHuaLei_SimpleDateFormat {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat();
System.out.println(sdf.format(new Date()));
SimpleDateFormat sdf2 = new SimpleDateFormat("Gyyyy-MM-dd:今天是yyyy年的第D天,E");
System.out.println(sdf2.format(new Date()));
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
String s = "2020-5-15";
System.out.println(sdf3.parse(s));
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200604090353889.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1Njk2Mjg4,size_16,color_FFFFFF,t_70)