String转换为Timestamp

两种方式将String类型的数据转换为Timestamp类型的数据

1. 使用Timestamp的valueOf()方法

String str = "2015-02-25 11:11:11";
Timestamp t = Timestamp.valueOf(str);

2. 使用Timestamp的构造方法

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
	Date date = sf.parse(str);
	System.out.println(date);
	Timestamp t1 = new Timestamp(date.getTime());
	System.out.println(t1);
} catch (ParseException e) {
	e.printStackTrace();
}

发布了45 篇原创文章 · 获赞 21 · 访问量 66万+

猜你喜欢

转载自blog.csdn.net/yin_jw/article/details/43938653