date 转成String ,String 转成date

来源:互联网 发布:陈冠希淘宝头像 编辑:程序博客网 时间:2024/04/28 14:01

//Date类型转换为String
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String nowTime = sdf.format(now);

 


//String类型转换为Date

方法一:
String s = "2008-01-15";
String[] a = s.split("[-]");
Date d = new Date(Integer.parseInt(a[0]),Integer.parseInt(a[1]),Integer.parseInt(a[2]));

方法二:

String strDate="2008-08-08";
Date date=java.sql.Date.valueOf(strDate);

 

 

 

注:java 中还有一个类叫Timestamp

 

Date d=new Date();
Timestamp ts=new Timestamp(d.getTime());

 

此类型直接可以插入到mysql 数据库中。。。。

mysql 对应表中字duan 类型为Date,DateTime;TimeStamp