org.sfm.map.impl.getter.time.JavaInstantFromObjectGetter 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.
The newest version!
package org.sfm.map.impl.getter.time;
import org.sfm.reflect.Getter;
import java.time.Instant;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
public class JavaInstantFromObjectGetter implements Getter {
private final Getter getter;
public JavaInstantFromObjectGetter(Getter getter) {
this.getter = getter;
}
@Override
public Instant get(S target) throws Exception {
Object o = getter.get(target);
if (o == null) {
return null;
}
if (o instanceof Date) {
return Instant.ofEpochMilli(((Date) o).getTime());
}
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 "JavaInstantFromObjectGetter{" +
"getter=" + getter +
'}';
}
}