![JAR search and dependency download from the Maven repository](/logo.png)
org.sfm.jdbc.impl.getter.time.JavaYearMonthResultSetGetter 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.*;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
public class JavaYearMonthResultSetGetter implements Getter {
private final int index;
private final ZoneId zone;
public JavaYearMonthResultSetGetter(JdbcColumnKey key, ZoneId zoneId) {
this.index = key.getIndex();
this.zone = zoneId;
}
@Override
public YearMonth get(ResultSet target) throws Exception {
Object o = target.getObject(index);
if (o == null) {
return null;
}
if (o instanceof Date) {
final ZonedDateTime dateTime = Instant.ofEpochMilli(((Date) o).getTime()).atZone(zone);
return YearMonth.of(dateTime.getYear(), dateTime.getMonth());
}
if (o instanceof Integer || o instanceof Long) {
int l = ((Number)o).intValue();
int year = l / 100;
int month = l % 100;
return YearMonth.of(year, month);
}
if (o instanceof YearMonth) {
return (YearMonth) o;
}
if (o instanceof TemporalAccessor) {
return YearMonth.from((TemporalAccessor) o);
}
throw new IllegalArgumentException("Cannot convert " + o + " to YearMonth");
}
@Override
public String toString() {
return "JavaYearMonthResultSetGetter{" +
"column=" + index +
'}';
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy