Spring Formatter Example

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


No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts