DateOnlyTypeHandler.java

  1. /*
  2.  *    Copyright 2009-2021 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.type;

  17. import java.sql.CallableStatement;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.util.Date;

  22. /**
  23.  * @author Clinton Begin
  24.  */
  25. public class DateOnlyTypeHandler extends BaseTypeHandler<Date> {

  26.   @Override
  27.   public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType)
  28.       throws SQLException {
  29.     ps.setDate(i, new java.sql.Date(parameter.getTime()));
  30.   }

  31.   @Override
  32.   public Date getNullableResult(ResultSet rs, String columnName)
  33.       throws SQLException {
  34.     java.sql.Date sqlDate = rs.getDate(columnName);
  35.     if (sqlDate != null) {
  36.       return new Date(sqlDate.getTime());
  37.     }
  38.     return null;
  39.   }

  40.   @Override
  41.   public Date getNullableResult(ResultSet rs, int columnIndex)
  42.       throws SQLException {
  43.     java.sql.Date sqlDate = rs.getDate(columnIndex);
  44.     if (sqlDate != null) {
  45.       return new Date(sqlDate.getTime());
  46.     }
  47.     return null;
  48.   }

  49.   @Override
  50.   public Date getNullableResult(CallableStatement cs, int columnIndex)
  51.       throws SQLException {
  52.     java.sql.Date sqlDate = cs.getDate(columnIndex);
  53.     if (sqlDate != null) {
  54.       return new Date(sqlDate.getTime());
  55.     }
  56.     return null;
  57.   }

  58. }