All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleflatmapper.converter.impl.time.ObjectToJavaYearConverter Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 9.0.2
Show newest version
package org.simpleflatmapper.converter.impl.time;

import org.simpleflatmapper.converter.Converter;

import java.time.*;
import java.time.temporal.TemporalAccessor;
import java.util.Date;


public class ObjectToJavaYearConverter implements Converter {
    private final ZoneId zone;

    public ObjectToJavaYearConverter(ZoneId zoneId) {
        this.zone = zoneId;
    }

    @Override
    public Year convert(Object o) throws Exception {
        if (o == null) {
            return null;
        }

        if (o instanceof Date) {
            final ZonedDateTime dateTime = Instant.ofEpochMilli(((Date) o).getTime()).atZone(zone);
            return Year.of(dateTime.getYear());
        }

        if (o instanceof Integer || o instanceof Long) {
            return Year.of(((Number)o).intValue());
        }

        if (o instanceof TemporalAccessor) {
            return Year.from((TemporalAccessor) o);
        }

        throw new IllegalArgumentException("Cannot convert " + o + " to Year");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy