Java : Format with Examples

Format (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Format methods.


Summary

Format is an abstract base class for formatting locale-sensitive information such as dates, messages, and numbers.

Class diagram

final var value = 1234;

final Format format2 = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(format2.format(value)); // $1,234.00

final Format format1 = NumberFormat.getCurrencyInstance(Locale.JAPAN);
System.out.println(format1.format(value)); // ¥1,234

Constructors

Format ()

Sole constructor.

I think it's rare to create a subclass of Format. Therefore, the code example is omitted.

Methods

Object clone ()

Creates and returns a copy of this object.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var value = 1234;

final Format format = NumberFormat.getCurrencyInstance();
System.out.println(format.format(value)); // $1,234.00

if (format.clone() instanceof Format cloned) {
    System.out.println(cloned.format(value)); // $1,234.00
}

final String format (Object obj)

Formats an object to produce a string.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var value = 1234;

final Format format1 = NumberFormat.getCurrencyInstance();
System.out.println(format1.format(value)); // $1,234.00

final Format format2 = NumberFormat.getCurrencyInstance(Locale.JAPAN);
System.out.println(format2.format(value)); // ¥1,234

abstract StringBuffer format (Object obj, StringBuffer toAppendTo, FieldPosition pos)

Formats an object and appends the resulting text to a given string buffer.

final var num = 123.456;

final Format format = NumberFormat.getNumberInstance(Locale.US);
System.out.println(format.format(num)); // 123.456

{
    final var sb = new StringBuffer("num = ");
    System.out.println(sb); // num =

    final var pos = new FieldPosition(NumberFormat.INTEGER_FIELD);
    final var ret = format.format(num, sb, pos);
    System.out.println(ret); // num = 123.456

    System.out.println(pos.getBeginIndex()); // 6
    System.out.println(pos.getEndIndex()); // 9
}
{
    final var sb = new StringBuffer("num = ");
    System.out.println(sb); // num =

    final var pos = new FieldPosition(NumberFormat.FRACTION_FIELD);
    final var ret = format.format(num, sb, pos);
    System.out.println(ret); // num = 123.456

    System.out.println(pos.getBeginIndex()); // 10
    System.out.println(pos.getEndIndex()); // 13
}

AttributedCharacterIterator formatToCharacterIterator (Object obj)

Formats an Object producing an AttributedCharacterIterator.

final var num = 123;

final Format format = NumberFormat.getNumberInstance(Locale.US);
System.out.println(format.format(num)); // 123

final var it = format.formatToCharacterIterator(num);

System.out.println("-- iterator --");
for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
    System.out.println(c);
}

// Result
// ↓
//-- iterator --
//1
//2
//3

Object parseObject (String source)

Parses text from the beginning of the given string to produce an object.

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final Format format1 = NumberFormat.getCurrencyInstance();
final var ret1 = format1.parseObject("$1,234.00");
System.out.println(ret1); // 1234

final Format format2 = NumberFormat.getCurrencyInstance(Locale.JAPAN);
final var ret2 = format2.parseObject("¥1,234");
System.out.println(ret2); // 1234

abstract Object parseObject (String source, ParsePosition pos)

Parses text from a string to produce an object.

final Format format = NumberFormat.getNumberInstance(Locale.US);

final var source = "123 456";
final var pos = new ParsePosition(0);

final var ret1 = format.parseObject(source, pos);
System.out.println(ret1); // 123
System.out.println(pos.getIndex()); // 3

pos.setIndex(pos.getIndex() + 1);

final var ret2 = format.parseObject(source, pos);
System.out.println(ret2); // 456
System.out.println(pos.getIndex()); // 7

final var ret3 = format.parseObject(source, pos);
System.out.println(ret3); // null

Related posts

To top of page