MessageFormat

MessageFormatPattern:java

String

     MessageFormatPattern FormatElement String

FormatElement:json

{ ArgumentIndex }

     { ArgumentIndex , FormatType }

     { ArgumentIndex , FormatType , FormatStyle }

FormatType: one of數組

number 
     date
     time
     choice

FormatStyle:code

short
     medium
     long
     full
     integer
     currency
     percent
     SubformatPattern
package json712.study_javaio;

import java.text.ChoiceFormat;
import java.text.Format;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Date;

import org.junit.Test;

public class MessageFormatTest {
	/***
	 * 
	 * @Title: mformatterTest1
	 * @Description: 不定參
	 * @param:
	 * @return: void
	 */
	@Test
	public void mformatterTest1() {
		int planet = 7;
		String event = "a disturbance in the Force";

		String result = MessageFormat.format("At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
				planet, new Date(), event);
		System.out.println(result);
	}

	/****
	 * 
	 * @Title: mformatterTest2 @Description: 數組 @param: @return: void @throws
	 */
	@Test
	public void mformatterTest2() {
		int fileCount = 1273;
		String diskName = "MyDisk";
		Object[] testArgs = { new Long(fileCount), diskName };
		MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0} file(s).");
		System.out.println(form.format(testArgs));
	}

	@Test
	public void mformatterTest3() {
		MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
		double[] filelimits = { 0, 1, 2 };
		String[] filepart = { "no files", "one file", "{0,number} files" };
		ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
		form.setFormatByArgumentIndex(0, fileform);
		int fileCount = 1273;
		String diskName = "MyDisk";
		Object[] testArgs = { new Long(fileCount), diskName };
		System.out.println(form.format(testArgs));
	}
	
	@Test
	public void mformatterTest4(){
		 MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
		 Object[] objs = {new Double(3.1415)};
		 String result = mf.format( objs );
		 System.out.println(result);
		 // result now equals "3.14, 3.1"
		 objs = null;
		 objs = mf.parse(result, new ParsePosition(0));
		 System.out.println(objs[0]);
		 // objs now equals {new Double(3.1)}
		
		 double[] limits = {1,2,3,4,5,6,7};
		 String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
		 ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
		 ParsePosition status = new ParsePosition(0);
		 for (double i = 0.0; i <= 8.0; ++i) {
		     status.setIndex(0);
		     System.out.println(i + " -> " + form.format(i) + " -> "
		                              + form.parse(form.format(i),status));
		 }
	}
	
	@Test
	public void mformatterTest5(){
		 double[] filelimits = {0,1,2};
		 String[] filepart = {"are no files","is one file","are {0} files"};
		 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
		 Format[] testFormats = {fileform, null, NumberFormat.getInstance()};
		 MessageFormat pattform = new MessageFormat("There {0} on {1}");
		 pattform.setFormats(testFormats);
		 
		 Object[] testArgs = {null, "ADisk", null};
		 
		 for (int i = 0; i < 4; ++i) {
		     testArgs[0] = new Integer(i);
		     testArgs[2] = testArgs[0];
		     System.out.println(pattform.format(testArgs));
		 }
	}
}

運行結果:orm

At 22:07:32 on 2017-5-31, there was a disturbance in the Force on planet 7.
The disk "MyDisk" contains 1,273 file(s).
The disk "MyDisk" contains 1,273 files.
3.14, 3.1
3.1
0.0 -> Sun -> 1.0
1.0 -> Sun -> 1.0
2.0 -> Mon -> 2.0
3.0 -> Tue -> 3.0
4.0 -> Wed -> 4.0
5.0 -> Thur -> 5.0
6.0 -> Fri -> 6.0
7.0 -> Sat -> 7.0
8.0 -> Sat -> 7.0
There are no files on ADisk
There is one file on ADisk
There are 2 files on ADisk
There are 3 files on ADisk
相關文章
相關標籤/搜索