首页 > "2016-07-30"这个形式的String类型 怎么转换成timestamp类型"2016-07-30 00:00:00"

"2016-07-30"这个形式的String类型 怎么转换成timestamp类型"2016-07-30 00:00:00"

jsp:
deadline = "2016-07-30"

在servlet中接收:
String deadline = request.getParameter("deadline");

在数据库中deadline的类型是:timestamp类型。

具体转换代码如下::
String deadline = request.getParameter("deadline");
Timestamp ts = new Timestamp(System.currentTimeMillis());
ts = Timestamp.valueOf(deadline);

报错:只有"2016-07-30 00:00:00" 这个形式的String才能转换成Timestamp类型。

      而"2016-07-30"这个形式的String转换会报错。
 Date是这个Timestamp的父子类关系。由Date向Timestamp转换要借助String。
 

我在网上看的这些关系,有点懵,不知道要怎么用。希望会的人儿,给我点指导。谢谢!!!!!

我现在到了我上面代码的这一步了 还需要怎么改,才能不报错呢!
请大神们指教。我不是特别明白。


赞同第一种
SimpleDateFormat _formatter = new SimpleDateFormat("yyyy-MM-dd");
Date d = _formatter.parse(“你的日期变量”)


StringBuffer sb = new StringBuffer(request.getParameter("deadline"));
String deadline = sb.append(" 00:00:00").toString();
Timestamp ts = new Timestamp(System.currentTimeMillis());   
try {   
        ts = Timestamp.valueOf(deadline);   
        System.out.println(ts);   
    } catch (Exception e) {   
        e.printStackTrace();   
    }  


 SimpleDateFormat _formatter = new SimpleDateFormat("yyyy-MM-dd");
Date d = _formatter.parse(“你的日期变量”)
【热门文章】
【热门文章】