String to Date mapping in Dozer can be configured in multiple ways using the XML configurations.
1. Field Level Mapping
Date format in which date needs to be parsed can be specified on that particular field
1 2 3 4 |
<field> <a date-format="MM/dd/yyyy">fieldLevelDate</a> <b>fieldDate</b> </field> |
2. Class Level Mapping
For many fields a default class level mapping can be specified
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<mapping date-format="MM-dd-yyyy"> <class-a>com.kscodes.dozer.SourceBean</class-a> <class-b>com.kscodes.dozer.DestinationBean</class-b> <field> <a>dateObj1</a> <b>dateObject1</b> </field> <field> <a>dateObj2</a> <b>dateObject2</b> </field> </mapping> |
3. Top Level Mapping
A default date format can also be specified at the very top mappings level. This default date format will be applied to all field mappings unless it is overridden at a lower level.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> <configuration> <date-format>MM/dd/yy</date-format> </configuration> <mapping> <class-a>com.kscodes.dozer.SourceBean</class-a> <class-b>com.kscodes.dozer.DestinationBean</class-b> <field> ..... </field> </mapping> </mappings> |
Lets see an simple example on this
SourceBean
1 2 3 4 5 6 7 8 9 10 |
package com.kscodes.dozer; public class SourceBean { private String fieldLevelDate; private String classLevelDate; //Getter Setters } |
DestinationBean
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.kscodes.dozer; import java.util.Date; public class DestinationBean { private Date fieldDate; private Date classDate; //Setter Getters } |
Mapper XML
We will use both field and class level mappings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="UTF-8"?> <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> <mapping date-format="MM-dd-yyyy"> <class-a>com.kscodes.dozer.SourceBean</class-a> <class-b>com.kscodes.dozer.DestinationBean</class-b> <field> <a date-format="MM/dd/yyyy hh:mm:ss">fieldLevelDate</a> <b>fieldDate</b> </field> <field> <a>classLevelDate</a> <b>classDate</b> </field> </mapping> </mappings> |
Now lets convert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.kscodes.dozer; import java.util.ArrayList; import java.util.List; import org.dozer.DozerBeanMapper; public class SimpleDozerTest { public static void main(String args[]) { List<String> mappingFileUrls = new ArrayList<>(); mappingFileUrls.add( "file:\\K:\\Kscodes\\workspace\\DozerSampleProject\\resources\\employee-mapper.xml"); // Create a Mapper Object DozerBeanMapper mapper = new DozerBeanMapper(); SourceBean sourceBean = new SourceBean(); // As we have the MM/dd/yyyy hh:mm:ss for this field sourceBean.setFieldLevelDate("07/10/2013 10:12:22"); // We have a MM-dd-yyyy format for other fields sourceBean.setClassLevelDate("05-25-2017"); mapper.setMappingFiles(mappingFileUrls); // Use the mapper Object and then map the required object DestinationBean destBean = mapper.map(sourceBean, DestinationBean.class); System.out.println(destBean); } } |
Output
References
1. String to Date mapping – Dozer Guide