java Timestamp、Date和String之间的互转
1.String 转 Date
String dateStr = "2012-12-31 00:00:00";
Date date = new Date();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = sdf.parse(dateStr);
2.Date 转 String
Date date = new Date();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(date);
3.String 转Timestamp
String tsStr = "2012-12-31 00:00:00";
Timestamp ts = new Timestamp(System.currentTimeMillis());
ts = Timestamp.valueOf(tsStr);
4.Timestamp 转 String
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "";
tsStr = sdf.format(ts);
5.Timestamp 转 Date
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date = new Date();
date = ts;
6.Date 转 Timestamp
Date date = new Date();
String time = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
time = sdf.format(date);
Timestamp ts = Timestamp.valueOf(time);