Join Forum Sign In Ask Question

Concretepage.com
Example of Converter and Formatter in Spring
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&ltT> target) can convert one data type to another data type like String to List,
String to Integer, String to Float etc.
TypeConverter.java
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
Latest Post
AsyncContext Example in Servlet 3.0
Axis2 Code First Approach Web Services in
Java
Example of JarInputStream in Java
JAR Packer and Unpacker with Pack200
Format in Java
How to Manage Power and Sleep Mode in
Windows 8
Java Callable Example
Java SAAJ Web Service Example
Download file Using JAX-WS Web Service in
Java
Core Java Interview Questions: Executor
Framework
ConcurrentLinkedDeque Java Example
Find us on Facebook
Featured
How to write generic method in java
Example of Files in java.nio.file API | Java
NIO 2
How to Enable Hibernate Option in Windows 8
Custom Login Page and Custom Error
Message in Spring Security
How to Start Private Browsing in Mozilla
Firefox
JAX-WS Web Service Simple Example
How to Create a Java Project with Maven in
Eclipse
Home > Spring Core
Tweet Tweet
0
0
Like Like
HOME FORUM WEB SERVICES
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&ltString> 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);
}
}

package com.concretepage;
import java.util.Date;
import java.util.Locale;
import org.springframework.format.Formatter;
import org.springframework.format.datetime.DateFormatter;
Enter Your Email to Get Latest Post Go
Concretepage.com
211 people like Concretepage.com.
Facebook social plugin
Like Like
MORE INTERVIEWQ&A QUIZ MY COMPUTER ORM SPRING JAVA EE COREJAVA
converted by Web2PDFConvert.com
Output
3-ott-2013
2013/10/03
10
10,5
pom.xml
Eclipse
Example of JQuery JSON on UI
Spring MVC Example with Annotation
Popular Tutorials
Learn Java SE 7
Learn Design Patterns in Java
Microsoft Windows 8 Tips and Tricks
Core Java Interview Questions and Answers
Spring Interview Questions and Answers
Hibernate Interview Questions and Answers
Learn Spring Security
Learn By Quiz
Forum For Question & Answer
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));

}
}

&ltproject 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">
&ltmodelVersion&gt4.0.0</modelVersion>
&ltgroupId&gtcom.concretepage</groupId>
&ltartifactId&gtspring</artifactId>
&ltversion&gt0.0.1-SNAPSHOT</version>
&ltname&gtspring</name>

&ltproperties>
&ltspring.version&gt3.2.2.RELEASE</spring.version>
</properties>

&ltdependencies>
&ltdependency>
&ltgroupId&gtorg.springframework</groupId>
&ltartifactId&gtspring-core</artifactId>
&ltversion>${spring.version}</version>
&ltscope&gtcompile</scope>
</dependency>

&ltdependency>
&ltgroupId&gtorg.springframework</groupId>
&ltartifactId&gtspring-context</artifactId>
&ltversion>${spring.version}</version>
&ltscope&gtcompile</scope>
</dependency>

&ltdependency>
&ltgroupId&gtaopalliance</groupId>
&ltartifactId&gtaopalliance</artifactId>
&ltversion&gt1.0</version>
&ltoptional&gttrue</optional>
</dependency>

&ltdependency>
converted by Web2PDFConvert.com
Tweet Tweet
&ltgroupId&gtcglib</groupId>
&ltartifactId&gtcglib-nodep</artifactId>
&ltversion&gt2.1_3</version>
&ltoptional&gttrue</optional>
</dependency>

&ltdependency>
&ltgroupId&gtmysql</groupId>
&ltartifactId&gtmysql-connector-java</artifactId>
&ltversion&gt5.1.17</version>
</dependency>

&ltdependency>
&ltgroupId&gtjavax.inject</groupId>
&ltartifactId&gtjavax.inject</artifactId>
&ltversion&gt1</version>
</dependency>

&ltdependency>
&ltgroupId&gtorg.aspectj</groupId>
&ltartifactId&gtaspectjweaver</artifactId>
&ltversion&gt1.5.4</version>
</dependency>

&ltdependency>
&ltgroupId&gtorg.springframework</groupId>
&ltartifactId&gtspring-agent</artifactId>
&ltversion&gt2.5.6</version>
&ltscope&gtprovided</scope>
</dependency>

&ltdependency>
&ltgroupId&gtorg.springframework</groupId>
&ltartifactId&gtspring-tx</artifactId>
&ltversion>${spring.version}</version>
&ltscope&gtcompile</scope>
</dependency>

&ltdependency>
&ltgroupId&gtorg.springframework</groupId>
&ltartifactId&gtspring-jdbc</artifactId>
&ltversion&gt2.0.8</version>
&ltscope&gtcompile</scope>
</dependency>

&ltdependency>
&ltgroupId&gtorg.codehaus.fabric3.api</groupId>
&ltartifactId&gtjavax-jta</artifactId>
&ltversion&gt1.1.1</version>
</dependency>

</dependencies>

</project>

Share Share Share Share
converted by Web2PDFConvert.com
Copyright ©2014 concretepage.com | All rights reserved | Privacy Developed by : TechWox Corporation
Related Post
HibernateTemplate in Spring
AnnotationSessionFactoryBean in Spring
HibernateTransactionManager in Spring
Example of Spring AOP
Spring Hibernate Integration Example
Rollback-for and no-rollback-for in Spring
registerShutdownHook in Spring
Spring Expression Language Support
@Required in Spring Annotation
@Autowired and @Qualifier in Spring
@Resource in Spring Annotation
@PostConstruct and @PreDestroy in Spring Annotation
Spring Custom Qualifier Annotation
Spring Constructor Injection
Example of @Cacheable in Spring
Example of @CacheEvict in Spring
ConcurrentMap-based Cache in Spring
Example of @Configuration in Spring
0 Comments Concretepage.com
Sort by Best
Start the discussion…
Subscribe Add Disqus to your site
Related Links
Learn Core Java
Learn Spring
Learn Hibernate
Learn Jquery
Using Window OS
Favorite Links
Java Technology
Hibernate Annotations
Spring Framework
JQuery
About Us
We are a group of software developers.
We enjoy learning and sharing technologies.
To improve the site's content, your valuable suggestions
are most welcome. Thanks
Email : concretepage@gmail.com

Referral
Home Core Java Spring Hibernate Jquery My Computer News Forum Quiz Interview Q & A Maven Eclipse IDE
converted by Web2PDFConvert.com

Sign up to vote on this title
UsefulNot useful