Java/Development Class/MessageFormat

Материал из 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 value 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("Today is {0,date,short} and UTC of 0 is {1,date,short}",
       params);
   System.out.println(msg);
 }

}

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



Full length 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("Today is {0,date,full} and UTC of 0 is {1,date,full}",
       params);
   System.out.println(msg);
 }

}

</source>
   
  
 
  



Long style 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("Today is {0,date,long} and UTC of 0 is {1,date,long}",
       params);
   System.out.println(msg);
 }

}

</source>
   
  
 
  



Medium length 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("Today is {0,date,medium} and UTC of 0 is {1,date,medium}",
       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>
   
  
 
  



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>