Java Tutorial/I18N/Message Format

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

Combine date value in a sentence

   <source lang="java">

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

}</source>





Currency number format

   <source lang="java">

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

}</source>





Date Number Sample

   <source lang="java">

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

}</source>





Floating point numbers

   <source lang="java">

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

}</source>





Format Date() in long

   <source lang="java">

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</source>





Format Date() in short format

   <source lang="java">

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

}</source>





Format Date value in full length

   <source lang="java">

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</source>





Format Date() value in medium format

   <source lang="java">

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

}</source>





Format message with Integer fillers

   <source lang="java">

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</source>





Formatting a Message Containing a Date

   <source lang="java">

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

}</source>





Formatting a Message Containing a Number

   <source lang="java">

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

}</source>





Formatting a Message Containing a Time

   <source lang="java">

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

}</source>





Percent value format

   <source lang="java">

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</source>





Set MessageFormat to Locale.US

   <source lang="java">

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

}</source>





Substitute tokens in a String

   <source lang="java">

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 !</source>





Use a custom format

   <source lang="java">

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

}</source>





Use a custom format for Date value

   <source lang="java">

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</source>





Use MessageFormat to format a sentence

   <source lang="java">

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

}</source>