java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
Date d = new Date();
String time = d.toString();
         News news = new News();
news.setTitle(title);
news.setContents(contents);
news.setCreatedate(sdf.parse(time));下面是数据库
         String sql ="insert News (title,contents,createdate)values(?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,news.getTitle());
pstmt.setString(2,news.getContents());
pstmt.setTimestamp(3,new Timestamp(news.getCreatedate().getTime()));
pstmt.execute();
为什么会出现: Servlet.service() for servlet jsp threw exception
java.text.ParseException: Unparseable date: "Sat Aug 08 10:34:15 CST 2009"
at java.text.DateFormat.parse(Unknown Source)
请高手帮帮忙

解决方案 »

  1.   

    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd"); 
    Date d = new Date(); 
    news.setTitle(title); 
    news.setContents(contents); 
    news.setCreatedate(sdf.format(d));
    这样用  
      

  2.   

    不行啊 news.setCreatedate(sdf.format(d)); 
    sdf.format(d)是字符串啊 而createdate是Date
      

  3.   

    晕  你不要把日期转化成字符串   你没看我的代码么
    你那个toString不用
      

  4.   

    直接把? 改成  getdate()
    最简单了  但是防止不了  sql注入
      

  5.   

    <%
    int year,month,day,hour,minute,second;
      String time,sy1="",sy2="";
      GregorianCalendar calendar;
      calendar = new GregorianCalendar();
      year = calendar.get(Calendar.YEAR);
      month = calendar.get(Calendar.MONTH)+1;
      day = calendar.get(Calendar.DAY_OF_MONTH);
      hour = calendar.get(Calendar.HOUR_OF_DAY);
      minute = calendar.get(Calendar.MINUTE);
      second = calendar.get(Calendar.SECOND);
      if( month < 10)
     {sy1 = "0"+month;}
     else
     {sy1 = ""+month;}
    if( day < 10)
     {sy2 = "0"+day;}
      else
    {sy2 = ""+day;}
      time= year +"-"+sy1+"-" + sy2+" "+hour+":"+minute+":"+second; 
    %>
      

  6.   

    其实很简单,你用的date类型是util的
    如果你想直接插入,就要看你的数据库的date类型是什么了
    一般你可以尝试下
    java.sql.Date d = new Date();
    如果不行,那可能是你数据库的date类型是Timestamp
    所以你可以用
    Timestamp t = new Timestamp();
    应该没问题