Date -- String(格式化)

来源:互联网 发布:小额贷款业务软件 编辑:程序博客网 时间:2024/04/30 04:53
 * Date --String(格式化)
 * public final String format(Date date)
 * 
 * String -- Date(解析)
 * public Date parse(String source)
 * 
 * DateForamt:可以进行日期和字符串的格式化和解析,但是由于是抽象类,所以使用具体子类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);
}
}
0 0
原创粉丝点击