Close

Spring Framework - SimpleTypeConverter Examples

Spring Framework 

Converting direct values (not tied to a bean like BeanWrapper approach) to convert types.

package com.logicbig.example.typeconverter;

import org.springframework.beans.SimpleTypeConverter;

public class SimpleTypeConverterExample {
public static void main (String[] args) {
SimpleTypeConverter tc = new SimpleTypeConverter();
Double d = tc.convertIfNecessary("345", Double.class);
System.out.println(d);
}
}

Output

345.0
Original Post




Using Custom property editor.

package com.logicbig.example.typeconverter;

import org.springframework.beans.SimpleTypeConverter;
import org.springframework.beans.propertyeditors.CustomDateEditor;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleTypeConverterExample2 {
public static void main (String[] args) {
SimpleTypeConverter tc = new SimpleTypeConverter();
tc.registerCustomEditor(Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MMM-dd"), false));
Date date = tc.convertIfNecessary("2015-JUN-10", Date.class);
System.out.println(date);
}

private static class LocalBean {
private Date date;
}
}

Output

Wed Jun 10 00:00:00 CDT 2015
Original Post




See Also