1、String与Date类型转换:
1、获取当前系统时间:
Date
Calendar
Date
System.currentTimeMillis();
2、Date类型转换为String类型:
SimpleDateFormat
SimpleDateFormat
String
String
System.out.println(str);
System.out.println(str1);
说明:sdf和sdf1只是两个不懂的格式化类型的定义,类型可以自由定义。
效果如下:
String
SimpleDateFormat
SimpleDateFormat
("yyyy-MM-dd
Date
Date
System.out.println(date3);
System.out.println(date4);
说明:sdf2和sdf3的格式要求,区别是时间部分,另不可在格式化中出现中文字符。
String
//只显示日期部分
System.out.println(java.sql.Date.valueOf(str4));
由于Date类型只能显示时间部分,而无法显示时间不分,因而出现了timestamp类型。
2、String与Timestamp类型转换:
1、String转换为Timestamp类型:
String
Timestamp.valueOf(str3);
System.out.println(Timestamp.valueOf(str3));
Long
SimpleDateFormat
System.out.println(Timestamp.valueOf(format.format(l)));