oblac / jodd-util

Essential Java utilities.
https://util.jodd.org
BSD 2-Clause "Simplified" License
39 stars 9 forks source link

OffsetDateTime converter #10

Closed bligny closed 2 years ago

bligny commented 2 years ago

I'm using jodd.bean.BeanCopy to clone my beans, possibly with required conversions.

As far as I can see, the default converters that are registered (in TypeConverterManager.registerDefaults) do NOT include any converter to convert a value to java.time.OffsetDateTime, right ?

How can I register additional custom converters, in

new BeanCopy(srcBean, targetBean)
            .includeFields(true)
            .declared(true)
            .forced(true)
                         // .register(MyOffsetDateTimeConverter)    <= HOW ?
            .copy();
igr commented 2 years ago

Hi!

You can register custom and new type converters using TypeConverterManager.get().register(....) method. Do this during the initialization of your application. From that point, all bean utils will be aware of your type converter.

Of course - you can send me your code, I will add OffsetDateTime to BeanUtils asap!

bligny commented 2 years ago

You can register custom and new type converters using TypeConverterManager.get().register(....)

I was afraid that, when using BeanCopy I would not have access to the private instance of 'TypeConverterManager' (in BeanUtilUtil) but I did not catch that TypeConverterManager.get()always returns the same singleton :-) !!!

Thanks for your quick response.

igr commented 2 years ago

Yeah, there is a singleton that is used by default; and in most cases, you may choose your own instance of TypeConverterManager; that is how it is designed :)

bligny commented 2 years ago

Of course - you can send me your code, I will add OffsetDateTime to BeanUtils asap!

Here it is:

TypeConverterManager.get().register(OffsetDateTime.class, 
        new OffsetDateTimeConverter( ZoneId.of("Europe/Paris") )
    );      

---

/*
 * OffsetDateTime converter
 */
public class OffsetDateTimeConverter implements TypeConverter<OffsetDateTime> {

    private ZoneId _zoneId;

    public OffsetDateTimeConverter() {
        this( ZoneId.systemDefault() );
    }

    public OffsetDateTimeConverter(ZoneId zoneId) {
        _zoneId = zoneId;
    }

    @Override
    public OffsetDateTime convert(Object value) {
        if (value==null) {
            return null;
        } else if (value instanceof ZonedDateTime) {
            ZonedDateTime input = ZonedDateTime.class.cast(value);
            return input.toOffsetDateTime();
        } else if (value instanceof LocalDateTime) {
            LocalDateTime input = LocalDateTime.class.cast(value);          
            return input.atZone(_zoneId).toOffsetDateTime();
        } else if (value instanceof LocalDate) {
            LocalDate input = LocalDate.class.cast(value);
            return input.atStartOfDay(_zoneId).toOffsetDateTime();
        } else if (value instanceof Date) {             
            Date input = Date.class.cast(value);
            return OffsetDateTime.ofInstant( input.toInstant(), _zoneId);
        } else if (value instanceof String) {
            return OffsetDateTime.parse((String)value);
        } else {
            throw new TypeConversionException(value);
        }
    }

}