Skip to content

Instantly share code, notes, and snippets.

@twogood
Created June 13, 2017 13:26
Show Gist options
  • Save twogood/04048c7f2de390ab68249970f1fd1cf0 to your computer and use it in GitHub Desktop.
Save twogood/04048c7f2de390ab68249970f1fd1cf0 to your computer and use it in GitHub Desktop.
Using LocalDate fields with BeanMapper in JDBI
//
// USAGE:
//
// jdbi.registerColumnMapper(new LocalDateColumnMapper());
//
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultColumnMapper;
public class LocalDateColumnMapper implements ResultColumnMapper<LocalDate> {
@Override
public LocalDate mapColumn(ResultSet r, int columnNumber, StatementContext ctx) throws SQLException {
return toLocalDate(r.getDate(columnNumber));
}
@Override
public LocalDate mapColumn(ResultSet r, String columnLabel, StatementContext ctx) throws SQLException {
return toLocalDate(r.getDate(columnLabel));
}
private static LocalDate toLocalDate(Date date) {
return (date == null) ? null : date.toLocalDate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment