这两天一直因为一件事情头疼,就是通过JSP页面的日期控件传送日期到后台的问题。

  1.接收问题:

    一开始不能用Date类型接收,否则会报错,接受形式如右图113943717.png

后来在类中加入方法:

@InitBinder
      protected void initBinder(HttpServletRequest request,
              ServletRequestDataBinder binder) throws Exception {
          DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
          CustomDateEditor dateEditor = new CustomDateEditor(format, true);
          binder.registerCustomEditor(Date.class, dateEditor);
      }

接收成功。

  2.SQL查询:

  然后就是查询。如果我直接使用“select 字段名 from 表名 where dateStr=”+occuron.就会报错。后来换用:“select 字段名 from  表名 where dateStr=to_date('”+occuron+“','YYYY-MM-DD'”,仍然失败。

  最后使用一下方法解决:

  将获取的日期转换成String格式:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate = sdf.format(occuron);

然后利用to_char查询:

String getAlarmLogSql="select * from Table where occuron=to_date('"+currentDate+"','yyyy-mm-dd hh24:mi:ss')";


OK ,问题解决!