/**
* 将字符串时间'2015-9-8 17:05:06'转化为格式'2015-09-08 17:05:06'
*/
import java.text.SimpleDateFormat;
public class TestDate{
public static void main(String[] args) throws Exception{
String time = "2015-9-8 17:05:06";//注意:时分秒必须都有,否则会出错
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date_util = sdf.parse(time); //转换为util.date
java.sql.Date date_sql = new java.sql.Date(date_util.getTime());//转换为sql.date
System.out.println(date_util); //Tue Sep 08 17:05:06 CST 2015
System.out.println(date_sql); //2015-09-08
String date = sdf.format(date_sql);
System.out.println(date); //2015-09-08 17:05:06
date = sdf.format(date_util);
System.out.println(date); //2015-09-08 17:05:06
}
}
运行结果: