(1)Mon Jun 17 18:03:29 CST 2019------转换为---------2019-06-17 18:03:29 星期一 下午
package date;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* java.text.SimpleDateFormat
* 可以根据一个给定的日期格式在Date与String之间相互转换
* @author ta
*
*/
public class SimpleDateFormat_Format {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now);
/*
* 2019-06-17 18:03:29
* yyyy-MM-dd HH:mm:ss
*/
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E a");
/*
* String format(Date date)
* 将给定的Date所表示的时间按照当前SDF指定的日期
* 格式转换为字符串
*/
String line = sdf.format(now);
System.out.println(line);
}
}
(2)2019-06-17 18:00:00----转换为----Mon Jun 17 18:00:00 CST 2019
package date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 将一个字符串解析为Date
* @author ta
*
*/
public class SimpleDateFormat_parse {
public static void main(String[] args) {
String str = "2019-06-17 18:00:00";
SimpleDateFormat sdf
= new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
try {
Date date = sdf.parse(str);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}