Question

I'm getting an InstantiationException when trying to map Date -> Calendar.

Simple test follows:

    @Test
    public void testConversion()
    {
        GregorianCalendar cal = new GregorianCalendar(2009, 2, 3);
        Date sourceValue = cal.getTime();
        DozerBeanMapper mapper = new DozerBeanMapper();
        Object result = mapper.map(sourceValue, Calendar.class);
    }

According to the docs, this is supported out of the box (even though Calendar is abstract). Anyone got experience with this and able to point out what I'm doing wrong?

Was it helpful?

Solution

You are right. This throws InstantionException (I consider this a bug in dozer. Will you file it in their bug tracking system?).

However. It works when you convert the Date <--> Calendar values not on the root level. This test works for me (dozer 5.1):

    public static class Source {
        private Date value;
        public void setValue(Date value) {
            this.value = value;
        }
        public Date getValue() {
            return value;
        }
    }

    public static class Target {
        private Calendar value;
        public void setValue(Calendar value) {
            this.value = value;
        }
        public Calendar getValue() {
            return value;
        }
    }


    @Test
    public void testConversion()
    {
        final GregorianCalendar cal = new GregorianCalendar(2009, 2, 3);
        Source source = new Source(){{ setValue(cal.getTime());}};

        DozerBeanMapper mapper = new DozerBeanMapper();
        Target result = (Target) mapper.map(source, Target.class);
        assertEquals(cal.getTimeInMillis(), result.getValue().getTimeInMillis());
    }

OTHER TIPS

If you change Calendar.class to GregorianCalendar.class the test works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top