ChoiceFormat format() method in Java with Examples Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The format() method of java.text.ChoiceFormat class is used to get the appended string builder of the format value of particular limit value passed as parameter and text passed as parameter in this method. Syntax: public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition status) Parameter: This method takes the following parameter as follow number: which is the particular limit of choiceformat object for which format have to be found and appendedtoAppendTo: which is the new text which is to be appended with the formatstatus: which determines if there is no special status to be returned Return Value: This method return appended value of text and format in the form of StringBuffer object. Exception: This method throws NullPointerException if toAppendto value is null.Below are the examples to illustrate the format() method:Example 1: Java // Java program to demonstrate // format() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { try { // creating and initializing ChoiceFormat ChoiceFormat cf1 = new ChoiceFormat( "4#wed| 5#thu | 6#fri | 7#sat"); // creating and initializing StringBuffer StringBuffer str = new StringBuffer("Sun"); // getting the required format with appended text // ChoiceFormat Object // using format() method StringBuffer value = cf1.format(6, str, null); // display the result System.out.print("Formatted text with appended value: " + value.toString()); } catch (NullPointerException e) { System.out.println("str is null"); System.out.println("Exception thrown: " + e); } } } OutputFormatted text with appended value: Sunfri Example 2: Java // Java program to demonstrate // format() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { try { // creating and initializing ChoiceFormat ChoiceFormat cf1 = new ChoiceFormat( "4#wed| 5#thu | 6#fri | 7#sat"); // creating and initializing StringBuffer StringBuffer str = null; // getting the required format with appended text // ChoiceFormat Object // using format() method StringBuffer value = cf1.format(6, str, null); // display the result System.out.print("Formatted text with appended value: " + value.toString()); } catch (NullPointerException e) { System.out.println("str is null"); System.out.println("Exception thrown: " + e); } } } Output: str is null Exception thrown: java.lang.NullPointerException Reference: https://docs.oracle.com/javase/9/docs/api/java/text/ChoiceFormat.html#format-double-java.lang.StringBuffer-java.text.FieldPosition- Comment More infoAdvertise with us Next Article DateFormat format() Method in Java with Examples R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-text package Java- ChoiceFormat Practice Tags : Java Similar Reads DateFormat format() Method in Java with Examples DateFormat class present inside java.text package is an abstract class that is used to format and parse dates for any locale. It allows us to format date to text and parse text to date. DateFormat class provides many functionalities to obtain, format, parse default date/time. DateFormat class extend 2 min read ChoiceFormat getLimits() method in Java with Examples The getLimits() method of java.text.ChoiceFormat class is used to get the attached limit to choice format object in time of initialization. It returns the array of double type with the limits.Syntax: public double[] getLimits() Parameter: This method does not accept any parameter.Return Value: This 2 min read ChoiceFormat parse() method in Java with Examples The parse() method of java.text.ChoiceFormat class is used to get the limit value for particular format in ChoiceFormat object. Syntax: public Number parse(String text, ParsePosition status) Parameter: This method takes the following parameters: text: which is the text for which limit value have to 2 min read ChronoLocalDate format() method in Java with Examples The format() method of ChronoLocalDate interface in Java method formats this date using the specified formatter. Syntax: public String format(DateTimeFormatter formatter) Parameter: This method accepts a parameter obj which specifies the formatter to be used and it is not null. Exceptions: The funct 2 min read Formatter close() method in Java with Examples The close() method is a built-in method of the java.util.Formatter which closes the formatter. In case it is already closed, it will have no effect. Closing it means that it will release the resources that it is already holding. Syntax: public void close() Parameters: The function accepts no paramet 2 min read ChronoZonedDateTime format() method in Java with Examples The format() method of ChronoZonedDateTime interface in Java is used to format this date-time using the specified formatter passed as parameter.This date-time will be passed to the formatter to produce a string. Syntax: default String format(DateTimeFormatter formatter) Parameters: This method accep 2 min read Like