Java.text.Format System Summary

Source: Internet
Author: User
Tags dateformat instance method

1.1. Format method
Java.text.Format as an abstract class, there are two abstract methods,
Format (object obj, StringBuffer toappendto, fieldposition POS): Formats an object and appends the resulting text to the given character buffer. is to format obj as a string and add it to toappendto.
Parseobject (string source, Parseposition Pos): Parses a string literal and generates an object. Is the reverse method of the Format method and converts a string to object.
Additional format and Parseobject are overloads of the two methods.
Method Formattocharacteriterator (Object obj) is not an abstract method, and the API requires subclasses to implement this method.

1.2. Format sub-class
The Java 2 platform provides three special format subclasses for formatted dates, messages, and numbers: DateFormat (abstract Class), Messageformat, and NumberFormat (abstract class).
-->dateformat-->simpledateformat
Format-->messageformat
-->numberformat-->choiceformat
--DecimalFormat for formatting decimal digits

1.3. DateFormat usage
DateFormat is an abstract class of date/time formatting subclasses, with some static get***instance () methods to get the instance. By setting the length and region of the result, to get the format of date, time, etc. formatter. Not very common. The
typically uses the SimpleDateFormat subclass, New SimpleDateFormat ("Yyyy-mm-dd") or New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"), To get the usual time format. DateFormat's Get***instance () method is generally obtained simpledateformat.
See DateFormat Source code has a question, if inherit the DateFormat class, that DateFormat. Get***instance () method or return SimpleDateFormat? The Dateformat.get is private and cannot be overwritten. There are also static fields in the
DateFormat class, such as Week_of_month_field, Week_of_year_field. The API says it is used in fieldposition for alignment. The
Java.util.Calendar is also a class related to date operations, and the implementation class is GregorianCalendar. operates primarily on dates. Note the difference between the Add method and the Roll method, the roll method does not change the larger field. There are also some static variable Day_of_month,day_of_week, etc., used in the Get/set/add/roll method.

1.4. Use of Messageformat
As with SimpleDateFormat, you need to pass in a pattern. Such as
String result = Messageformat.format (
"At {1,time} on {1,date}, there is {2} on planet {0,number,integer}.",
7, New Date (), "a Message");
which
{1,time} 1 refers to the first parameter, and time refers to the formatted type. The settings are based on the API and are formatted by calling NumberFormat and DateFormat.
You can also call the parse method to convert the string to OBEJCT.

1.5. Use of NumberFormat
1.5.1. Introduction of NumberFormat
NumberFormat is the abstract base class for all number formats. The class structure is similar to DateFormat. It also obtains the realization class DecimalFormat through the Get***instance method mainly.
The more commonly used methods are:
Setparseintegeronly: Impact analysis only. is set to true, the position after the decimal point is ignored.
Setdecimalseparatoralwaysshown: Only affects formatting, and only affects the number of digits after the decimal point? Sets whether the grouping symbol is displayed. For example, in 1,234, whether the number is displayed.
Setgroupingused: whether to group. Returns true if the group is used in this format. For example, in the English locale, if a group is set, the number 1234567 may be formatted as "1,234,567".
You can also set the maximum/small number of decimal/integer parts.
The parseposition in the parse method and the FieldPosition in the format method are studied.

1.5.2. Subclass DecimalFormat
DecimalFormat is a specific subclass of NumberFormat that is used to format decimal digits. You can customize the format type by passing in the pattern in DecimalFormat. The Rounding method is Half-even (rounded). The
DecimalFormat mode contains positive and negative sub-patterns, such as "#,# #0.00;-#,# #0.00".; The following represents the negative number pattern. A separate "0.00" is equivalent to "0.00;-0.00". If there is an explicit negative subtype, it specifies only a negative number prefix and suffix;
0: The representation is a digit, or 0 if it does not exist; #: Represents a number;,: Group delimiter;

The

1.5.3. Subclass Choiceformat
Choiceformat is typically used to process complex numbers in Messageformat. When creating Choiceformat, you can specify a format array and a limit array. The lengths of these arrays must be the same.
Example for converting the week in the API
Double[] limits = {1,2,3,4,5,6,7};
String[] MonthNames = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"};
Choiceformat form = new Choiceformat (limits, monthNames);
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));
}
Form.format (i) is converted to the name of the day of the week based on the number of limits. The numbers in limits must be sorted in ascending order, and if the supplied number is not in limits, then the first or last index is selected.
Form.parse (Form.format (i), status) is converted to a value in limits based on name.

1.5.4. Examples of Choiceformat
Here is an example of a more complex pattern format in Choiceformat's API, which is used with Messageformat:
Generate Choiceformat
Double[] filelimits = {0,1,2};
String[] Filepart = {"Is no files", "was one file", "is {2} files"};//Here 2 refers to the value of the second element taken from the Testargs
Choiceformat fileform = new Choiceformat (filelimits, Filepart);

Defines the format array testformats, respectively, Choiceformat,null, NumberFormat
Choiceformat for {0}, NULL for {1}, NumberFormat for {2}
Format[] Testformats = {fileform, null, numberformat.getinstance ()};

Set the formats of Messageformat to Testformats
Format order in Testformats corresponds to the order of the format elements in the pattern string
It is best to use the Setformatsbyargumentindex method instead of using Setformats
Messageformat pattform = new Messageformat ("there {0} on {1}");
Pattform.setformats (Testformats);

For Fomat
Object[] Testargs = {null, "Adisk", null};
for (int i = 0; i < 4; ++i) {
Testargs[0] = new Integer (i);
TESTARGS[2] = testargs[0];
The Testargs value at this time is {0, "Adisk", 0}
The TESTARGR element is processed according to the definition of Messageformat
System.out.println (Pattform.format (Testargs));
}

1.5.5. Pattern construction Method of Choiceformat
There is another way to construct Choiceformat, passing in pattern.
Assigning a schema to a Choiceformat object is fairly straightforward. For example:
Choiceformat FMT = new Choiceformat (
" -1#is negative| 0#is zero or fraction | 1#is One |1.0

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java.text.Format System Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.