关于Date类型的常见问题及处理方法

来源:互联网 发布:重庆外包seo找谁好 编辑:程序博客网 时间:2024/06/05 07:54

1、在前台页面上传入date类型的字段为空值的时候会发现进不了controller,页面报错如下:

HTTP Status 400 -type Status reportmessagedescription The request sent by the client was syntactically incorrect.

在此情况下在controller中加入以下代码解决:

    @InitBinder    public void initBinder(WebDataBinder binder) {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");        dateFormat.setLenient(false);        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));    }

2、在保存到oracle数据库时,会发现date类型的字段只保存进了日期,而没有保存时间,如下:
这里写图片描述
这种情况是因为oracle中默认的date类型的格式为DD-MON-YY,解决办法如下:
2.1、将mybatis配置文件中jdbcType类型去掉,如下图:
这里写图片描述
但是这种情况下,如果保存的date类型字段为null值时会如下错误:

setting null for parameter #10 with JdbcType OTHER . 

这是因为:

**DefaultParameterHandler类在解析参数的时候,无法解析参数类型,指定默认的类型OTHER.** public void setParameters(PreparedStatement ps) throws SQLException {    ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();    if (parameterMappings != null) {      for (int i = 0; i < parameterMappings.size(); i++) {        ParameterMapping parameterMapping = parameterMappings.get(i);        if (parameterMapping.getMode() != ParameterMode.OUT) {          Object value;          String propertyName = parameterMapping.getProperty();          if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params            value = boundSql.getAdditionalParameter(propertyName);          } else if (parameterObject == null) {            value = null;          } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {            value = parameterObject;          } else {            MetaObject metaObject = configuration.newMetaObject(parameterObject);            value = metaObject.getValue(propertyName);          }          TypeHandler typeHandler = parameterMapping.getTypeHandler();          JdbcType jdbcType = parameterMapping.getJdbcType();          //注意下面这句话          if (value == null && jdbcType == null) jdbcType = configuration.getJdbcTypeForNull();          typeHandler.setParameter(ps, i + 1, value, jdbcType);        }      }    }  } 

可以自定义插入一个日期来避免这种错误,如手动插入new Date(0);(该日期在java中为1970-01-01 08:00:00)
2.2、修改oracle中Date类型的默认格式

1)在sql*plus中修改当前会话的日期格式SQL> alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';将当前会话的时间格式修改为这种格式: 2003-01-28 15:23:38,即四位年-两位月-两位日,空格,24小时的制的小时:分钟:秒。这种修改方法,只对当前会话有效。注意,是对当前会话,而不是当前的sql*plus窗口。即如果你这样修改之后,又使用connect命令以其他用户连接到数据库或者是连接到其他的数据库,则这个日期格式就失效了,又恢复到缺省的日期格式。    (2)修改注册表(只对windows系统)在注册表/hkey_local_machine/software/oracle/home0主键中增加一个字串(8i版本),字串名为nls_date_format,字串的值为你希望定义的时间格式,如: yyyy-mm-dd hh24:mi:ss ,然后重新启动sql*plus。这种修改方法,对sql*plus窗口有效,即不论你打开多少个sql*plus窗口,缺省的都是这种时间格式。修改服务器端的注册表无效,只有修改客户端的注册表才有效。    (3)Linux下:oracle用户编辑 .bash_profile下 加入以下内容       export  NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS '       重新登录即可生效    (4)用sysdba登录然后更新props$这个表里的字段即可update props$ set value = 'YYYY-MM-DD HH24:MI:SS' where parameter = 'NLS_DATE_FORMAT';

参考文章:
http://www.cnblogs.com/carekee/articles/4532905.html
http://blog.sina.com.cn/s/blog_7e3d739f0101pujn.html
http://www.cnblogs.com/havery/articles/4025879.html
(如有侵权,请通知我删除)