org.sfm.jdbc.impl.getter.time.JavaInstantResultSetGetter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleFlatMapper Show documentation
Show all versions of simpleFlatMapper Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.sfm.jdbc.impl.getter.time;
import org.sfm.jdbc.JdbcColumnKey;
import org.sfm.reflect.Getter;
import java.sql.ResultSet;
import java.time.Instant;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
public class JavaInstantResultSetGetter implements Getter {
private final int index;
public JavaInstantResultSetGetter(JdbcColumnKey key) {
this.index = key.getIndex();
}
@Override
public Instant get(ResultSet target) throws Exception {
Object o = target.getObject(index);
if (o == null) {
return null;
}
if (o instanceof Date) {
return Instant.ofEpochMilli(((Date) o).getTime());
}
if (o instanceof Instant) {
return (Instant) o;
}
if (o instanceof TemporalAccessor) {
return Instant.from((TemporalAccessor) o);
}
if (o instanceof Long || o instanceof Integer) {
return Instant.ofEpochMilli(((Number)o).longValue());
}
throw new IllegalArgumentException("Cannot convert " + o + " to Instant");
}
@Override
public String toString() {
return "JavaInstantResultSetGetter{" +
"column=" + index +
'}';
}
}