关于Date类型的常见问题及处理方法
2017-06-01 10:33
477 查看
1、在前台页面上传入date类型的字段为空值的时候会发现进不了controller,页面报错如下:
在此情况下在controller中加入以下代码解决:
2、在保存到oracle数据库时,会发现date类型的字段只保存进了日期,而没有保存时间,如下:
这种情况是因为oracle中默认的date类型的格式为DD-MON-YY,解决办法如下:
2.1、将mybatis配置文件中jdbcType类型去掉,如下图:
但是这种情况下,如果保存的date类型字段为null值时会如下错误:
这是因为:
可以自定义插入一个日期来避免这种错误,如手动插入new Date(0);(该日期在java中为1970-01-01 08:00:00)
2.2、修改oracle中Date类型的默认格式
参考文章:
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
(如有侵权,请通知我删除)
HTTP Status 400 - type Status report message description 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
(如有侵权,请通知我删除)
相关文章推荐
- javascript 关于Date 时间类型 处理方法
- Struts1.x处理Date类型数据的方法
- oracle数据库对date字段类型存在空值进行排序的处理方法
- 关于PreparedStatement插入Date类型值的方法
- java 直接返回Date类型数据给前端,前端的处理方法
- SpringMVC处理Date类型的成员变量方法
- 关于PreparedStatement插入Date类型值的方法
- GridView控件常见问题及处理方法===1. 关于隐藏字段的处理:==2. 关于按钮列的事件:==3. 关于按钮的客户端提示:==4. 关于时间字段的格式化输出:==5. 关于绑定空记录:
- hibernate 关于date类型的模糊查询解决方法
- SpringMVC处理Date类型的成员变量方法
- SpringMVC处理Date类型的成员变量方法
- 关于PreparedStatement插入Date类型值的方法
- 关于extjs date类型的处理
- 【android基础】之关于重复类型事件的处理方法
- SpringMVC处理Date类型的属性的方法
- js处理date类型的各种方法
- oracle数据库对date字段类型存在空值进行排序的处理方法
- 处理CLOB,BLOB,Date,Timestamp数据类型的方法
- oracle date 类型字段的处理方法
- SpringMVC处理Date类型的成员变量方法