Java Tutorial/I18N/Message Format

Материал из Java эксперт
Перейти к: навигация, поиск

Combine date value in a sentence

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("Today is {0,date} and UTC of 0 is {1,date}", params);
    System.out.println(msg);
  }
}





Currency number format

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Double(123.45), new Double(1234.56) };
    String msg = MessageFormat.format("{0,number,currency} a""s and {1,number,currency} b""s",
        params);
    System.out.println(msg);
  }
}





Date Number Sample

import java.text.MessageFormat;
import java.util.Date;
public class DateNumberSample {
  public static void main(String args[]) {
    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));
  }
}





Floating point numbers

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Double(123.45), new Double(1234.56) };
    String msg = MessageFormat.format("{0,number,#.#} a""s and {1,number,#.#} b""s", params);
    System.out.println(msg);
  }
}





Format Date() in long

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("{0,time,long} and UTC of 0 is {1,time,long}", params);
    System.out.println(msg);
  }
}
//8:31:27 PDT AM and UTC of 0 is 4:00:00 PST PM





Format Date() in short format

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("{0,time,short} and UTC of 0 is {1,time,short}", params);
    System.out.println(msg);
  }
}





Format Date value in full length

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String  msg = MessageFormat.format("{0,time,full} and UTC of 0 is {1,time,full}", params);
    System.out.println(msg);
  }
}
//8:31:58 o"clock AM PDT and UTC of 0 is 4:00:00 o"clock PM PST





Format Date() value in medium format

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("{0,time,medium} and UTC of 0 is {1,time,medium}", params);
    System.out.println(msg);
  }
}





Format message with Integer fillers

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Integer(123), new Integer(1234) };
    String msg = MessageFormat.format("{0,number} a""s and {1,number} b""s", params);
    System.out.println(msg);
  }
}
//123 a"s and 1,234 b"s





Formatting a Message Containing a Date

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("Today is {0} and UTC of 0 is {1}", params);
    System.out.println(msg);
  }
}





Formatting a Message Containing a Number

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Integer(123), new Integer(1234) };
    String msg = MessageFormat.format("{0} a""s and {1} b""s", params);
    System.out.println(msg);
  }
}





Formatting a Message Containing a Time

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("The time is {0} and UTC of 0 is {1}", params);
    msg = MessageFormat.format("The time is {0,time} and UTC of 0 is {1,time}", params);
    System.out.println(msg);
  }
}





Percent value format

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Double(123.45), new Double(1234.56) };
    String msg = MessageFormat
        .format("{0,number,percent} a""s and {1,number,percent} b""s", params);
    System.out.println(msg);
  }
}
//12,345% a"s and 123,456% b"s





Set MessageFormat to Locale.US

import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
public class MessageFormatReuse {
  public static void main(String args[]) {
    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));
  }
}





Substitute tokens in a String

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) {
    Object[] params = new Object[] { "hello", "!" };
    System.out.println(MessageFormat.format("{0} world {1}", params));
  }
}
//hello world !





Use a custom format

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Integer(123), new Integer(1234) };
    String msg = MessageFormat.format("{0,number,#} a""s and {1,number,#} b""s", params);
    System.out.println(msg);
  }
}





Use a custom format for Date value

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat
        .format("{0,time,HH-mm-ss} and UTC of 0 is {1,time,HH-mm-ss}", params);
    System.out.println(msg);
  }
}
// 08-32-33 and UTC of 0 is 16-00-00





Use MessageFormat to format a sentence

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 lucky 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);
  }
}