Java Tutorial/I18N/Message Format
Содержание
- 1 Combine date value in a sentence
- 2 Currency number format
- 3 Date Number Sample
- 4 Floating point numbers
- 5 Format Date() in long
- 6 Format Date() in short format
- 7 Format Date value in full length
- 8 Format Date() value in medium format
- 9 Format message with Integer fillers
- 10 Formatting a Message Containing a Date
- 11 Formatting a Message Containing a Number
- 12 Formatting a Message Containing a Time
- 13 Percent value format
- 14 Set MessageFormat to Locale.US
- 15 Substitute tokens in a String
- 16 Use a custom format
- 17 Use a custom format for Date value
- 18 Use MessageFormat to format a sentence
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>