Home  >  Spring Core

Example of Converter and Formatter in Spring

By Arvind Rai, October 03, 2013
Spring provides built-in converter and formatter. GenericConversionService provides convert method to convert one data type to another data type. In case of Formatter, there are DateFormatter and NumberFormatter etc that formats data according to Locale.

Convert method of GenericConversionService

In spring convert(Object source, Class<T> target) can convert one data type to another data type like String to List, String to Integer, String to Float etc.

TypeConverter.java
package com.concretepage;
import java.util.List;
import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.core.convert.support.GenericConversionService;
public class TypeConverter {
	public static void main(String[] args) {
		GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
		//converting string to list
		List<String> list = conversionService.convert("A,B,C,D",List.class);
		for(String s: list){
			System.out.println(s);
		}
		//converting string to integer
		Integer i = conversionService.convert("10",Integer.class);
		System.out.println(i);
		//converting string to float
		Float f = conversionService.convert("15f",Float.class);
		System.out.println(f);
	}
}
 
Output
A
B
C
D
10
15.0

DateFormatter and NumberFormatter in Spring

Spring provides Formatter like DateFormatter and NumberFormatter. DateFormatter formats date in any locale. And in same way NumberFormatter formats number according to given Locale.

FormatterTest.java
package com.concretepage;
import java.util.Date;
import java.util.Locale;
import org.springframework.format.Formatter;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.format.number.NumberFormatter;
public class FormatterTest {
	public static void main(String[] args) {
		//date formatter
		Formatter dtform = new DateFormatter();
		System.out.println(dtform.print(new Date(), Locale.ITALY));
		
		System.out.println(dtform.print(new Date(), Locale.JAPANESE));
		
		//number formatter
		Formatter numform = new NumberFormatter();
		System.out.println(numform.print(10, Locale.ITALY));
		System.out.println(numform.print(10.5, Locale.GERMAN));
		
	}
}
 
Output
3-ott-2013
2013/10/03
10
10,5


pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.concretepage</groupId>
  <artifactId>spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring</name>
  
  <properties>
     <spring.version>3.2.2.RELEASE</spring.version>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
      <scope>compile</scope>
    </dependency>
    
     <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
      <optional>true</optional>
    </dependency>
    
    <dependency>
	  <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
	  <version>2.1_3</version>
	  <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>mysql</groupId>
	 <artifactId>mysql-connector-java</artifactId>
	 <version>5.1.17</version>
    </dependency>
    
    <dependency>
     <groupId>javax.inject</groupId>
     <artifactId>javax.inject</artifactId>
     <version>1</version>
    </dependency>
    
    <dependency>
	 <groupId>org.aspectj</groupId>
	 <artifactId>aspectjweaver</artifactId>
	 <version>1.5.4</version>
   </dependency>
   
   <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-agent</artifactId> 
     <version>2.5.6</version> 
     <scope>provided</scope> 
    </dependency> 
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-tx</artifactId>
	  <version>${spring.version}</version>
	  <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>2.0.8</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.codehaus.fabric3.api</groupId>
	 <artifactId>javax-jta</artifactId>
	 <version>1.1.1</version>
    </dependency>
           
  </dependencies>
  
</project>
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us