A default date format can also be specified at the very top mappings level. This default date format will be applied to all field mapppings unless it is overridden at a lower level.
As you see above snippet, I specified data-format setting in <configuration> element, which is applicable for all the mappings.
Find the below working application.
Product1.java
package com.sample.app.model;
import java.util.Date;
public class Product1 {
private Date manufacturingDate;
public Date getManufacturingDate() {
return manufacturingDate;
}
public void setManufacturingDate(Date manufacturingDate) {
this.manufacturingDate = manufacturingDate;
}
@Override
public String toString() {
return "Product1 [manufacturingDate=" + manufacturingDate + "]";
}
}
Product2.java
package com.sample.app.model;
public class Product2 {
private String dateString;
public String getDateString() {
return dateString;
}
public void setDateString(String dateString) {
this.dateString = dateString;
}
@Override
public String toString() {
return "Product2 [dateString=" + dateString + "]";
}
}
Product3.java
package com.sample.app.model;
import java.util.Date;
public class Product3 {
private Date manufacturingDate;
private Date expiryDate;
public Product3() {
}
public Product3(Date manufacturingDate, Date expiryDate) {
this.manufacturingDate = manufacturingDate;
this.expiryDate = expiryDate;
}
public Date getManufacturingDate() {
return manufacturingDate;
}
public void setManufacturingDate(Date manufacturingDate) {
this.manufacturingDate = manufacturingDate;
}
public Date getExpiryDate() {
return expiryDate;
}
public void setExpiryDate(Date expiryDate) {
this.expiryDate = expiryDate;
}
@Override
public String toString() {
return "Product3 [manufacturingDate=" + manufacturingDate + ", expiryDate=" + expiryDate + "]";
}
}
Product4.java
package com.sample.app.model;
public class Product4 {
private String manufacturingDateString;
private String expiryDateString;
public Product4() {
}
public Product4(String manufacturingDateString, String expiryDateString) {
this.manufacturingDateString = manufacturingDateString;
this.expiryDateString = expiryDateString;
}
public String getManufacturingDateString() {
return manufacturingDateString;
}
public void setManufacturingDateString(String manufacturingDateString) {
this.manufacturingDateString = manufacturingDateString;
}
public String getExpiryDateString() {
return expiryDateString;
}
public void setExpiryDateString(String expiryDateString) {
this.expiryDateString = expiryDateString;
}
@Override
public String toString() {
return "Product4 [manufacturingDateString=" + manufacturingDateString + ", expiryDateString=" + expiryDateString
+ "]";
}
}
date_formatting_at_top_level.xml
<?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">
<configuration>
<date-format>MM/dd/yyyy HH:mm:ss:SS</date-format>
</configuration>
<mapping>
<class-a>com.sample.app.model.Product3</class-a>
<class-b>com.sample.app.model.Product4</class-b>
<field>
<a>manufacturingDate</a>
<b>manufacturingDateString</b>
</field>
<field>
<a>expiryDate</a>
<b>expiryDateString</b>
</field>
</mapping>
<mapping>
<class-a>com.sample.app.model.Product1</class-a>
<class-b>com.sample.app.model.Product2</class-b>
<field>
<a>manufacturingDate</a>
<b>dateString</b>
</field>
</mapping>
</mappings>
DateToStringTopLevelMapping.java
package com.sample.app;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import org.dozer.DozerBeanMapper;
import com.sample.app.model.Product1;
import com.sample.app.model.Product2;
import com.sample.app.model.Product3;
import com.sample.app.model.Product4;
public class DateToStringTopLevelMapping {
public static void main(String[] args) {
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.setMappingFiles(Arrays.asList("date_formatting_at_top_level.xml"));
Product1 product1 = new Product1();
product1.setManufacturingDate(new Date());
Product2 product2 = mapper.map(product1, Product2.class);
Product1 product1ConvertedBack = mapper.map(product2, Product1.class);
System.out.println("product1 : " + product1);
System.out.println("product2 : " + product2);
System.out.println("product1ConvertedBack : " + product1ConvertedBack);
Date manufacturingDate = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
Date expiryDate = Date.from(LocalDate.now().plusDays(100).atStartOfDay(ZoneId.systemDefault()).toInstant());
Product3 product3 = new Product3(manufacturingDate, expiryDate);
Product4 product4 = mapper.map(product3, Product4.class);
Product3 product3ConvertedBack = mapper.map(product4, Product3.class);
System.out.println("\nproduct3 : " + product3);
System.out.println("product4 : " + product4);
System.out.println("product3ConvertedBack : " + product3ConvertedBack);
}
}
Output
product1 : Product1 [manufacturingDate=Sun Feb 06 20:44:12 IST 2022] product2 : Product2 [dateString=02/06/2022 20:44:12:219] product1ConvertedBack : Product1 [manufacturingDate=Sun Feb 06 20:44:12 IST 2022] product3 : Product3 [manufacturingDate=Sun Feb 06 00:00:00 IST 2022, expiryDate=Tue May 17 00:00:00 IST 2022] product4 : Product4 [manufacturingDateString=02/06/2022 00:00:00:00, expiryDateString=05/17/2022 00:00:00:00] product3ConvertedBack : Product3 [manufacturingDate=Sun Feb 06 00:00:00 IST 2022, expiryDate=Tue May 17 00:00:00 IST 2022]
No comments:
Post a Comment