Date – String(格式化)
- public final String format(Date date)
String – Date(解析)
- public Date parse(String source)
DateFormat:可以进行日期和字符串的格式化解析,但是由于是抽象类,所以使用具体子类SimpleDateFormat。
SimpleDateFormat的构造方法:
- SimpleDateFormat():默认模式
- SimpleDateFormat(String pattern):给定的模式
- 这个模式的字符串该如何写呢?
- 通过查看API,我们就找到了对应的模式
年 y ,月M ,日 d ,时 H,分 m,秒 s
举例:2014年12月12日 12:12:12
代码示例:
public class DateFormatDemo {
public static void main(String[] args) throws ParseException {
//Date -- String
//创建日期对象
Date d = new Date();
//创建格式化对象
//SimpleDateFormat sdf = new SimpleDateFormat();
//给定模式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//public final String format(Date date)
String s = sdf.format(d);
System.out.println(s);
//String -- Date
String str = "2008-08-08 12:12:12";
//在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(str);
System.out.println(dd);
}
}
运行结果:
2019年05月06日 17:16:21
Fri Aug 08 12:12:12 CST 2008