Skip to content

Instantly share code, notes, and snippets.

@petarov
Created October 12, 2018 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petarov/65924012f4eceb4258f58cf2b2e8b961 to your computer and use it in GitHub Desktop.
Save petarov/65924012f4eceb4258f58cf2b2e8b961 to your computer and use it in GitHub Desktop.
MyBatis Microsoft OffsetDateTimeTypeHandlerJDBC42
public class OffsetDateTimeTypeHandlerJDBC42 extends BaseTypeHandler<OffsetDateTime> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, OffsetDateTime parameter, JdbcType jdbcType)
throws SQLException {
ps.setObject(i, parameter);
}
@Override
public OffsetDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
return toOffsetDateTime(rs.getObject(columnName, DateTimeOffset.class));
}
@Override
public OffsetDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return toOffsetDateTime(rs.getObject(columnIndex, DateTimeOffset.class));
}
@Override
public OffsetDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return toOffsetDateTime(cs.getObject(columnIndex, DateTimeOffset.class));
}
private OffsetDateTime toOffsetDateTime(DateTimeOffset dto) {
if (dto != null) {
return OffsetDateTime.ofInstant(dto.getTimestamp().toInstant(),
ZoneOffset.ofHours(dto.getMinutesOffset() / 60));
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment