ITeye博客

date 转成String ,String 转成date

ileson · 2010-01-26 12:45 · Java · 编程语言 · Markdown

//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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。