Text MessageFormat in Java With Example

0
47
Spread the love
  • 23
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    132
    Shares

MessageFormat in Java
MessageFormat in Java

What this article will cover?

MessageFormat in Java, MessageFormat in java example, MessageFormat java api,MessageFormat java class and how to use MessageFormat in java

MessageFormat in Java

MessageFormat is a class that helps formatting a message String. It also helps to substitute objects into the predefined position in the message String.

MessageFormat in java example

If we want to display a message multiple times, MessageFormat helps a lot. We can achieve the same by creating an object of  MessageFormat class by giving the pattern string. Once the object has been created, we can call the format() method (defined in this class) with the array of objects to be formatted into the output message.

The same method can be applied if we want to format the message only one time. In that case we need to call format() method with a message or pattern string or an array of argument objects. They will be formatted ans substitute in to the message.

The message or pattern should clearly state where each argument should be substitute. We can obtain this by digits enclosed in a curly braces.

Example-

If the pattern is “{0}” it signifies that the first object needs to be substitute with a string. Similarly “{5}” signifies that the sixth object needs to be substitute with a string.

Just in case the object that needs to be inserted is not String, the JVM checks if the same is an instance of Date or Number or the subclass of Date or Number.

If they are an instance of Date or Number or the subclass of Date or Number, MessageFormat insert them with default DateFormat or NumberFormat.

If they are not an instance of Date or Number or the subclass of Date or Number, MessageFormat insert them with by invoking the objcet’s toString() method to convert.

The applicable and default patterns are:

  • date
  • time
  • number
  • choice

They will be auto formatted as date,time,number and choice before substituting them into pattern String.

However they needs to be separated by comma.

MessageFormat java class:

public class java.text.MessageFormat extends java.text.Format{
// Public Constructor
public MessageFormat(String pattern);
// Class Methods
public static String format(String pattern, Object[] arguments);
// Public Instance Methods
public void applyPattern(String newPattern);
public Object clone(); // Overrides Format
public boolean equals(Object obj); // Overrides Object
public final StringBuffer format(Object[] source, StringBuffer result,
FieldPosition ignore);
public final StringBuffer format(Object source, StringBuffer result,
FieldPosition ignore); // Defines Format
public Format[] getFormats();
public Locale getLocale();
public int hashCode(); // Overrides Object
public Object[] parse(String source, ParsePosition status);
public Object[] parse(String source) throws ParseException;
public Object parseObject(String text, ParsePosition status);
// Defines Format
public void setFormat(int variable, Format newFormat);
public void setFormats(Format[] newFormats);
public void setLocale(Locale theLocale);
public String toPattern();
}

how to use this class?

This area also covers the messageformat java api.

MessageFormat is kind of Printf() function,we find in C language. MessageFormat supports the below functions:

public Object[] parse(String source, ParsePosition status);
public Object[] parse(String source) throws ParseException;
public Object parseObject(String text, ParsePosition status);

The parse() method enables us to parse an array  of objects from a specified String. It is governed by the specified pattern.

public void setFormat(int variable, Format newFormat); 
public void setFormats(Format[] newFormats);

setFormat() method enables us to change the Format object in a particular position with a particular pattern.

public void applyPattern(String newPattern);

This method enables us to set a new pattern for the MessageFormat.

public String toPattern();

This method returns the current formatting pattern.

public void setLocale(Locale theLocale);

This method specifies that the MessageFormat should use a non default Locale.We can insert DateFormat and NumberFormat objects to format dates and number. We can use them for formatting the message as a pattern.

Reference:

Oracle MessageFormat

 

 

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 23
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    132
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here