The Format
class (in java.text
) is an abstract base class used for formatting and parsing locale-sensitive information like numbers, dates, messages, and text. It provides a common interface for all formatting subclasses such as DateFormat
, NumberFormat
, MessageFormat
, etc.
Key Features:
- Serves as the superclass for all formatter classes.
- Defines the standard methods for formatting objects to strings and parsing strings to objects.
- Supports locale-sensitive formatting.
- Allows subclassing to create custom formatters.
Commonly Used Methods

Simple Program
import java.text.Format; import java.text.DateFormat; import java.util.Date; public class SimpleFormatExample { public static void main(String[] args) { Format formatter = DateFormat.getDateInstance(DateFormat.LONG); String formattedDate = formatter.format(new Date()); System.out.println("Formatted Date: " + formattedDate); } } /* Formatted Date: May 23, 2025 (based on current date and locale) */
Problem Statement
Paani and Mahesh are developing a multi-lingual billing system for an international e-commerce platform. They need to format invoices that show the date and total amount according to the customer’s locale. By extending the Format
class, they ensure consistent parsing and formatting across components.
import java.text.*; import java.util.*; public class CustomInvoiceFormatter extends Format { private final DateFormat dateFormat; private final NumberFormat numberFormat; public CustomInvoiceFormatter(Locale locale) { this.dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); this.numberFormat = NumberFormat.getCurrencyInstance(locale); } @Override public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { if (!(obj instanceof Invoice)) { throw new IllegalArgumentException("Object must be of type Invoice"); } Invoice invoice = (Invoice) obj; toAppendTo.append("Invoice Date: ") .append(dateFormat.format(invoice.getDate())) .append(" | Total: ") .append(numberFormat.format(invoice.getTotal())); return toAppendTo; } @Override public Object parseObject(String source, ParsePosition pos) { // For simplicity, parsing is not implemented. return null; } public static void main(String[] args) { Locale usLocale = Locale.US; Locale frLocale = Locale.FRANCE; Invoice invoice = new Invoice(new Date(), 299.99); CustomInvoiceFormatter usFormatter = new CustomInvoiceFormatter(usLocale); CustomInvoiceFormatter frFormatter = new CustomInvoiceFormatter(frLocale); System.out.println("US Invoice: " + usFormatter.format(invoice)); System.out.println("French Invoice: " + frFormatter.format(invoice)); } } class Invoice { private final Date date; private final double total; public Invoice(Date date, double total) { this.date = date; this.total = total; } public Date getDate() { return date; } public double getTotal() { return total; } } /* US Invoice: Invoice Date: May 23, 2025 | Total: $299.99 French Invoice: Invoice Date: 23 mai 2025 | Total: 299,99 € */
The Format
class is a foundation for all locale-sensitive formatters used when
- You need to implement custom formatting logic
- You work with localization/internationalization
- You want to build consistent format-parse interfaces across your application