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

org.simpleflatmapper.converter.impl.time.ObjectToJavaZonedDateTimeConverter 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.Context;
import org.simpleflatmapper.converter.ContextualConverter;

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


public class ObjectToJavaZonedDateTimeConverter implements ContextualConverter {
    private final ZoneId zone;

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

    @Override
    public ZonedDateTime convert(Object o, Context context) throws Exception {
        if (o == null) {
            return null;
        }

        if (o instanceof Date) {
            return Instant.ofEpochMilli(((Date) o).getTime()).atZone(zone);
        }

        if (o instanceof Instant) {
            return((Instant)o).atZone(zone);
        }

        if (o instanceof ZonedDateTime) {
            return (ZonedDateTime) o;
        }

        if (o instanceof LocalDateTime) {
            return ((LocalDateTime)o).atZone(zone);
        }

        if (o instanceof TemporalAccessor) {
            return ZonedDateTime.from((TemporalAccessor) o).withZoneSameLocal(zone);
        }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy