org.sfm.jdbc.impl.getter.time.JavaLocalDateTimeResultSetGetter 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.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
public class JavaLocalDateTimeResultSetGetter implements Getter {
private final int index;
private final ZoneId zone;
public JavaLocalDateTimeResultSetGetter(JdbcColumnKey key, ZoneId zoneId) {
this.index = key.getIndex();
this.zone = zoneId;
}
@Override
public LocalDateTime 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()).atZone(zone).toLocalDateTime();
}
if (o instanceof LocalDateTime) {
return (LocalDateTime) o;
}
if (o instanceof TemporalAccessor) {
return LocalDateTime.from((TemporalAccessor) o);
}
throw new IllegalArgumentException("Cannot convert " + o + " to LocalDateTime");
}
@Override
public String toString() {
return "JavaLocalDateTimeResultSetGetter{" +
"column=" + index +
'}';
}
}