java.util.Date和String类型的转换是非常常用的,现在总结一下:
1. Date转换为String
//Date --->String
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateNow = new Date(System.currentTimeMillis());
String now = dft.format(dateNow);
System.out.println(now);
2. String 转换为Date
//String ---->Date
try {
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startTime = dft.parse("2014-10-15 00:00:00");
System.out.println(startTime.toString());
} catch (ParseException e) {
e.printStackTrace();
}