String ==> Date
//String 转化为Date
try {
String dateStr = "2018/10/16 16:34:23";
//注意format的格式要与日期String的格式相匹配
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = sdf.parse(dateStr);
System.out.println(date.toString());
} catch (Exception e) {
e.printStackTrace();
}
String ==> Timestamp
//String 转化为Timestamp
//valueOf(String str)
try {
String tsStr = "2018-10-16 11:49:45";
Timestamp ts = Timestamp.valueOf(tsStr);
System.out.println(ts);
} catch (Exception e) {
e.printStackTrace();
}
Date ==> String
//Date 转化为String,通过SimpleDateFormat的format方法
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(new Date());
System.out.println(format);
Date ==> Timestamp
//Date 转化为Timestamp
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
System.out.println(timestamp);
Timestamp ==> Date
//Timestamp 转化为Date
Timestamp timestam= new Timestamp(System.currentTimeMillis());
Date d = timestam;
System.out.println(d.toString() + "==="+d);
Timestamp ==> String
//Timestamp 转化为String
Timestamp timesta = new Timestamp(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format1 = simpleDateFormat.format(timesta);
System.out.println(format1);
SO,结果为: