public voidDatetoString()
{
//时间格式字符串
String str1 = "10/8/2012 12:3:6";
//字符串中的日期格式
DateFormat from_type = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
//想要得到的日期显示格式
DateFormat to_type = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//用来做中间转换的Date
Date date = null;
try {
//将字符串转换成日期格式
date = from_type.parse(str1);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
//将日期格式转换成字符串
String str = to_type.format(date);
System.out.println(str);
}
结果是:
Mon Oct 08 12:03:06 CST 2012
2012-10-08 12:03:06