Searching...
Friday, 13 December 2013

Spring Formatter Example

07:50

Spring Formatter interface has many implementations, in this example we will see how to format Currency and Date using Spring formatter.

Example

package com.pretech;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import org.springframework.format.Formatter;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.format.number.CurrencyFormatter;
public class SpringFormatterExample {
	public static void main(String[] args) throws ParseException {
		// Currency Formatter
		Formatter<Number> currencyFormatter = new CurrencyFormatter();
		System.out.println(currencyFormatter.print(12, Locale.US));
		// Date Formatter
		Formatter<Date> datetformatter = new DateFormatter();
		System.out.println(datetformatter.print(new Date(), Locale.CHINA));
		System.out.println(datetformatter.print(new Date(), Locale.JAPANESE));
	}
}

Output



$12.00
2013-12-13
2013/12/13


0 comments: