Tuesday 8 March 2022

Dozer: Specify default date formatting at class mapping level

In Dozer, you can specify a default date format at the class mapping level. This default date format will be applied to all field mappings unless you override at the field level.

 

Example

<mapping date-format="MM/dd/yyyy HH:mm:ss:SS">
	<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>

 

Find the below working application.

 

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_to_string_at_class_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">

	<mapping date-format="MM/dd/yyyy HH:mm:ss:SS">
		<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>

</mappings>

DateToString1.java

package com.sample.app;

import java.io.IOException;
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.Product3;
import com.sample.app.model.Product4;

public class DateToString1 {

	public static void main(String[] args) throws IOException {

		DozerBeanMapper mapper = new DozerBeanMapper();
		mapper.setMappingFiles(Arrays.asList("date_to_string_at_class_level.xml"));

		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(product3);
		System.out.println(product4);
		System.out.println(product3ConvertedBack);

	}

}

Output

Product3 [manufacturingDate=Sun Feb 06 00:00:00 IST 2022, expiryDate=Tue May 17 00:00:00 IST 2022]
Product4 [manufacturingDateString=02/06/2022 00:00:00:00, expiryDateString=05/17/2022 00:00:00:00]
Product3 [manufacturingDate=Sun Feb 06 00:00:00 IST 2022, expiryDate=Tue May 17 00:00:00 IST 2022]






 

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment