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

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

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
(如有侵权,请通知我删除)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值