Use Java.Text Packets to formulate numbers and dates -ZT

zhaozj2021-02-16  143

Transferred from 9CBS Friends Blog "Flying Sky" -

Formatting output can be used in C, which is similar to PrintF ("% D% 8.2F / N", 1001, 52.335), but the system.out.println () in Java does not correspond to the corresponding function. To format the output, you must use the classes in the Java.Text package to implement similar operations (if you don't reflect the superiority of object-oriented, it is said that JDK1.5 is ready to make up). Of course, the function of the java.text package is still very powerful. It is strange that many books have not been involved, and who is generally do you have a work all day to see the API?

Note: Because it is very simple here, please refer to the following DEMO program.

Formatting numbers

In the NumberFormat class, we provide us with formatting 4 numbers: integer, decimal, currency, and percentage, through factory methods getNumberinstance, GetNumberintance, getCurrencyInstance, getpercentInstance methods to get the appropriate instance objects. For example, we have to represent RMB 88888.88 yuan with a string, so that you will write:

Numberformat nf = numberformat.getcurrencyinstance ();

System.out.println (NF.Format (88888.88));

Custom formatted numbers

However, for a slightly complex demand, Numberformat is not full, but fortunately, Java also provides custom formatting. To use the DecimalFormat object, you must provide it to provide a formatted mode (Pattern):

String Pattern = ...

DecimalFormat DF = New DecimalFormat (Pattern);

or:

Decimalformat DF = New decimalformat ();

Df. ApplyPattern (Pattern);

Then, you will then call its Format method.

So the key is how this model is defined. There is a pattern of syntax in Javadoc in the DECIMALFORMAT class, but it is difficult to say clearly (I can't tell, huh, huh), please see Demo yourself more. Below is a meanings of some characters in the pattern:

character

meaning

0

One number, this lack is displayed as 0. Use to make up

#

One digit, this lack is not displayed

.

Digital point, don't say more

,

Thousands of separators

E

Scientific notation

%

percentage

Format date

The easiest way to convert the date to a string is to call the Tostring or Tolocalestring method of the Date class:

System.out.println (New Date ());

Output: 2004-8-7 8:16:14. But if we want to make 2 bits of the month and day, don't time, you can't make it feel. Java.Text.dateFormat provides a large number of factory methods: getDateInstance (int style), GetTimeInstance (int ", GetDateTyle, int TimeStyle, etc.). Where Style must be DateFormat.long, DateFormat.Medium, one of DateFormat.short. The DefaultDateFormat method in Demo made a simple experiment.

Custom formatted date:

Similarly, java.text.SIMPLEDATEFORMAT can customize formatting by mode (Pattern):

String Pattern = ... SimpleDateFormat DF = New SimpleDateFormat (Pattern);

or:

SimpleDateFormat DF = New SimpleDateFormat ();

Df. ApplyPattern (Pattern);

Here's a pattern symbolic watch in Javadoc in SimpleDateFormat:

symbol

significance

Legal value

Example

y

Year

Year

1996; 96

M

Month in year

Month

Jul; jul; 07

di

Day in month

Number

10

a

AM / PM MARKER

TEXT

Pm

Hide

Hour in day (0-23)

Number

0

hide

Hour in am / pm (1-12)

Number

12

M

Minute in hour

Number

30

s

SECOND IN Minute

Number

55

S

Millisecond

Number

978

z

Time Zone

General Time Zone

Pacific standard Time; PST; GMT-08: 00

Z

Time Zone

RFC 822 TIME ZONE

-0800

Note that the size of the symbols is different, and the number of symbols can also cause the output to be different. For example, with mm will display 01 in January, and use M will not make up. For year, two YY will only output two years, YYY will output 4 years.

In fact, the above class also provides many other methods, especially for localization, custom formatting, and Parse methods that are converted from strings to the corresponding object, and attach the formatting results to one StringBuffer method (should be used to improve performance).

Finally, a small Demo and output results:

TestFormat.java:

Import java.text. *;

Import java.util. *;

Public class testformat {

Public static void main (String [] args) {

DefaultNumberformat ();

SYSTEM.OUT.Println ();

Customnumberformat ();

SYSTEM.OUT.Println ();

DefaultDateFormat ();

SYSTEM.OUT.Println ();

CustomDateFormat ();

SYSTEM.OUT.Println ();

}

Public static void defaultnumberformat () {

INT I = 123456;

Double x = 882323.23523;

Double P = 0.528;

Double C = 52.83;

Numberformat nf = Numberformat.getInstance ();

System.out.println ("Integer" i "Is Displayed AS" NF.Format (i));

System.out.println ("Double" X "Is Displayed AS" NF.Format (x));

Numberformat nfint = Numberformat.GetintegerInstance ();

System.out.println ("Integer" I "Is Displayed AS" NFINT.FORMAT (i)); Numberformat NFNumber = Numberformat.getNumberinstance ();

System.out.println ("Double" X "Is Displayed AS" NFNumber.Format (x));

Numberformat nfpercent = Numberformat.getPercentInstance ();

System.out.println ("Percent" P "IS Displayed AS" NFpercent.Format (P));

Numberformat nfcurrency = Numberformat.getcurrencyinstance ();

System.out.println ("currency" p "is displayed as" nfcurrency.format (c));

// This is not involved in the corresponding PARSE method

}

Public static void customernumberformat () {

Double x = 1000.0 / 3;

System.out.println ("Default Output IS" X);

PatternPrint ("###, ###. ##", x);

Patternprint ("####. ##", x);

PatternPrint ("####. 00", x);

PatternPrint ("####. 0 #", x);

PatternPrint ("00000. ##", x);

PatternPrint ("$ ###, ###. ##", x);

PatternPrint ("0. ### E0", x);

PatternPrint ("00. ##%", x);

Double y = 23.0012;

System.out.println ("Default Output IS" Y);

PatternPrint ("###, ###. ##", y);

PatternPrint ("####. ##", y);

PatternPrint ("####. 00", y);

PatternPrint ("####. 0 #", y);

Patternprint ("00000. ##", y);

PatternPrint ("$ ###, ###. ##", y);

PatternPrint ("0. ### E0", y);

Patternprint ("00. ##%", y);

}

Public static void patternprint (String Pattern, Double X) {

DecimalFormat DF = New DecimalFormat (Pattern);

System.out.println ("Output for Pattern" Pattern "IS" DF.FORMAT (X));

}

Public static void defaultdateformat () {

Date Date = new date (); system.out.println ("Simple Date" Date.TolocaString ());

DateFormat DF = DateFormat.getDatetimeInstance ();

System.out.println (DF.Format (date));

DateFormat DFLONG = DateFormat.getdateTimeInstance (DateFormat.long, DateFormat.long);

System.out.println (DFLONG.FORMAT (date));

DateFormat DfMedium = DateFormat.GetdateTimeInstance (DateFormat.medium,

DateFormat.medium);

System.out.println (DFMedium.Format (Date));

DateFormat Dfshort = DateFormat.GetdateTimeInstance (DateFormat.Short, DateFormat.short);

System.out.println (Dfshort.Format (Date));

}

Public static void customdateformat () {

Date Date = new date ();

Patternprint ("YYYY.MM.DD HH: MM: SS Z", DATE); // Two mm, DD can cause complementing

PatternPrint ("YY Month Monthly M Monthly HH MM Division", Date); // Two YY is displayed as two years

PatternPrint ("Eee, MMM D, 'YY", DATE

Patternprint ("H: MM A", DATE);

PatternPrint ("HH 'O'' &ck' A, ZZZZ", DATE);

PatternPrint ("YYYY.MMMMM.DD GGG HH: MM AAA", DATE);

PatternPrint ("Eee, D Mmm YYYY HH: MM: SS Z", DATE);

PatternPrint ("YYMMDDHMMMSSS", DATE);

}

Public static void patternprint (String Pattern, Date Date) {

SimpleDateFormat DF = New SimpleDateFormat (Pattern);

System.out.println (DF.Format (date));

}

}

Output:

Integer 123456 IS Displayed AS 123, 456

Double 882323.23523 is displayed as 882, 323.235

Integer 123456 IS Displayed AS 123, 456

Double 882323.23523 is displayed as 882, 323.235

Percent 0.528 IS Displayed AS 53%

Currency 0.528 IS Displayed AS ¥ 52.83

DEFAULT OUTPUT IS 333.3333333333333

Output for pattern ###, ###. ## is 333.33

Output for pattern ####. ## is 333.33

Output for pattern ####. 00 is 333.33

Output for pattern ####. 0 # IS 333.33output for pattern 00000. ## is 00333.33

Output for pattern $ ###, ###. ## is $ 333.33

Output for pattern 0. ### E0 IS 3.333E2

Output for pattern 00. ##% IS 33333.33%

Default Output is 23.0012

Output for pattern ###, ###. ## is 23

Output for pattern ####. ## is 23

Output for pattern ####. 00 is 23.00

Output for pattern ####. 0 # IS 23.0

Output for pattern 00000. ## is 00023

Output for pattern $ ###, ###. ## is $ 23

Output for pattern 0. ### E0 is 2.3e1

Output for pattern 00. ##% IS 2300.12%

Simple Date 2004-8-7

8:16:14

2004-8-7

8:16:14

August 7, 2004, 08:14 14

2004-8-7

8:16:14

04-8-7 8:16 AM

2004.08.07

08:16:14 GMT

08:00

August 7, 2004 08:16

Saturday, August 7, '04

8:16 am

08 O'Clock morning, GMT 08: 00

02004. August.07 AD 08:16 am

Saturday, 7 August 2004 08:

16:14

0800

040807081614 0800

转载请注明原文地址:https://www.9cbs.com/read-8296.html

New Post(0)