Java by API/java.text/MessageFormat — различия между версиями
Admin (обсуждение | вклад) м (1 версия)  | 
				|
(нет различий) 
 | |
Версия 17:43, 31 мая 2010
MessageFormat: format(String pattern, Object... arguments)
 
import java.text.MessageFormat;
import java.util.Date;
public class MainClass {
  public static void main(String args[]) throws Exception {
    Double kb = new Double(3.5);
    Date today = new Date();
    String pattern = "{0}K was deleted on {1}.";
    Object[] arguments = { kb, today };
    System.out.println(MessageFormat.format(pattern, arguments));
  }
}
   
   
MessageFormat: setLocale(Locale locale)
 
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
public class MainClass {
  public static void main(String[] argv) {
    String pattern = "{0}K was deleted on {1}.";
    MessageFormat formatter = new MessageFormat(pattern);
    Double kb = new Double(3.5);
    Date today = new Date();
    Object[] arguments = { kb, today };
    formatter.setLocale(Locale.US);
    System.out.println(formatter.format(arguments));
    formatter.setLocale(Locale.FRANCE);
    System.out.println(formatter.format(arguments));
    pattern = "On {1}, {0}K was deleted.";
    formatter.applyPattern(pattern);
    System.out.println(formatter.format(arguments));
    formatter.setLocale(Locale.US);
    System.out.println(formatter.format(arguments));
  }
}
   
   
new MessageFormat(String pattern)
 
import java.text.MessageFormat;
import java.util.Date;
class MessageFormatApp {
  public static void main(String args[]) {
    String pattern = "The time is {0,time} and ";
    pattern += "your number is {1,number}.";
    MessageFormat format = new MessageFormat(pattern);
    Object objects[] = { new Date(), new Integer((int) (Math.random() * 1000)) };
    String formattedOutput = format.format(objects);
    System.out.println(formattedOutput);
  }
}