Dozer mapping JodaTime property not working as expected

后端 未结 5 1772
野性不改
野性不改 2021-02-19 14:05

I am using Dozer to map between a Document class to DocumentManagementBean class, both of my own making. Both have a property, with getters and setters, of Joda DateTime type, c

5条回答
  •  北海茫月
    2021-02-19 14:40

    The basic problem is that Dozer creates a new blank instance of DateTime, via new DateTime(), and thats how you end up with the current date/time instead of the original one. There might be multiple solutions, I usually went with a customconverter, globally defined:

        
            org.joda.time.DateTime
            org.joda.time.DateTime
        
    

    and

    public class DateTimeCustomConverter extends DozerConverter {
    
        public DateTimeCustomConverter() {
            super(DateTime.class, DateTime.class);
        }
    
        @Override
        public DateTime convertTo(final DateTime source, final DateTime destination) {
    
            if (source == null) {
                return null;
            }
    
            return new DateTime(source);
        }
    
        @Override
        public DateTime convertFrom(final DateTime source, final DateTime destination) {
    
            if (source == null) {
                return null;
            }
    
            return new DateTime(source);
            }
    
    }
    

    It may overdo it, though :)

提交回复
热议问题