[Java] 12.1. 格式化

當輸出的文字需要日期、不同國家的貨幣、數值到小數幾位等,這時可以使用Java的格式化功能。

 

image/svg+xml12.1. 格式化 – Format DateFormat 類別與 Locale 類別, 產⽣不同國家的⽇期格式 class Employee { String name; String country ; double salary ; Employee( String pName, String pCountry , double pSalary){ name = pName; country = pCountry; salary = pSalary; } } import java.util.*; class Boss { void printPayroll ( Employee e ){ System.out.println( " 收款⼈ :" + e.name); System.out.println( " 國籍 :" + e.country); System.out.println( " ⽇期 :" + ( new Date())); System.out.println( " ⾦額 :" + e.salary ); } } class Main{ public static void main( String [] args){ Employee e1 = new Employee ( "Jack" , "US" , 150 ); Employee e2 = new Employee ( "Mary" , "FRANCE" , 100 ); Employee e3 = new Employee ( "Eric" , “TAIWAN” , 45000 ); Boss b = new Boss (); b.printPayroll (e1); b.printPayroll(e2); b.printPayroll(e3); } } 執⾏結果 收款⼈ :Jack 國籍 :US ⽇期 :Wed Dec 15 22:00:40 CST 2010 ⾦額 :150.0 收款⼈ :Mary 國籍 :FRANCE ⽇期 :Wed Dec 15 22:00:40 CST 2010 ⾦額 :100.0 收款⼈ :Eric 國籍 :TAIWAN ⽇期 :Wed Dec 15 22:00:40 CST 2010 ⾦額 :45000.0 import java.util.*; class Employee { String name; Locale country ; double salary; Employee ( String pName, Locale pCountry , double pSalary){ name = pName; country = pCountry; salary = pSalary; } } import java.util.*; import java.text.*; class Boss { void printPayroll ( Employee e){ NumberFormat format = NumberFormat . getCurrencyInstance ( e.country ); String salaryText = format . format ( e.salary ); System.out.println( " 收款⼈ :" + e.name); System.out.println( " 國籍 :" + e.country); System.out.println( " ⽇期 :" + ( new Date())); System.out.println( " ⾦額 :" + salaryText ); } } import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee ( "Jack" , Locale.US , 150 ); Employee e2 = new Employee( "Mary" , Locale.FRANCE, 100 ); Employee e3 = new Employee( "Eric" , Locale.TAIWAN, 45000 ); Boss b = new Boss (); b.printPayroll ( e1 ); b.printPayroll(e2); b.printPayroll(e3); } } 執⾏結果 收款⼈ :Jack 國籍 :en_US ⽇期 :Wed Dec 15 22:29:51 CST 2010 ⾦額 :$150.00 收款⼈ :Mary 國籍 :fr_FR ⽇期 :Wed Dec 15 22:29:51 CST 2010 ⾦額 :100,00 收款⼈ :Eric 國籍 :zh_TW ⽇期 :Wed Dec 15 22:29:51 CST 2010 ⾦額 :NT$45,000.00 import java.util.*; class Employee { String name; Locale country; double salary; double hours ; Employee ( String pName, Locale pCountry, double pSalary, double pHours ){ name = pName; country = pCountry; salary = pSalary; height = pHeight; hours = pHours; } } import java.text.NumberFormat; import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee ( "Jack" , Locale.US, 150 , 1500.3 ); Employee e2 = new Employee( "Mary" , Locale.FRANCE, 100 , 1000.5 ); Employee e3 = new Employee( "Eric" , Locale.TAIWAN, 45000 , 1100.1 ); NumberFormat f1 = NumberFormat.getNumberInstance ( e1 . country ); System.out.println( f1 . format ( e1 . hours )); NumberFormat f2 = NumberFormat.getNumberInstance(e2.country); System.out.println(f2.format(e2.hours)); NumberFormat f3 = NumberFormat.getNumberInstance(e3.country); System.out.println(f3.format(e3.hours)); } } 執⾏結果 1500.3 1.000,5 1100.1 下列為 NumberFormat 常⽤的⽅法: import java.util.*; import java.text.*; class Boss { void printPayroll ( Employee e){ NumberFormat format = NumberFormat.getCurrencyInstance(e.country); String salaryText = format.format(e.salary); System.out.println( " 收款⼈ :" + e.name); System.out.println( " 國籍 :" + e.country); System.out.println( " ⽇期 :" + ( new Date ())); System.out.println( " ⾦額 :" + salaryText); } } import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee( "Jack" , Locale.US , 150 ); Employee e2 = new Employee( "Mary" , Locale.FRANCE , 100 ); Employee e3 = new Employee( "Eric" , Locale.TAIWAN , 45000 ); Boss b = new Boss (); b.printPayroll ( e1 ); b.printPayroll ( e2 ); b.printPayroll ( e3 ); } } 執⾏結果 收款⼈ :Jack 國籍 :en_US ⽇期 :Wed Dec 15 22:29:51 CST 2010 ⾦額 :$150.00 收款⼈ :Mary 國籍 :fr_FR ⽇期 :Wed Dec 15 22:29:51 CST 2010 ⾦額 :100,00 收款⼈ :Eric 國籍 :zh_TW ⽇期 :Wed Dec 15 22:29:51 CST 2010 ⾦額 :NT$45,000.00 import java.util.*; import java.text.*; class Boss { void printPayroll ( Employee e ){ NumberFormat format = NumberFormat.getCurrencyInstance(e.country); String salaryText = format.format(e.salary); DateFormat dateFormat = DateFormat.getDateTimeInstance ( DateFormat.FULL , DateFormat.FULL , e.country ); String dateText = dateFormat.format ( new Date ()); System.out.println( " 收款⼈ :" + e.name); System.out.println( " 國籍 :" + e.country); System.out.println( " ⽇期 :" + dateText ); System.out.println( " ⾦額 :" + salaryText); } } 下表為 DateFormat 類別常⽤的⽅法: import java.util.*; import java.text.*; class Main{ public static void main( String [] args){ Date d = new Date(); DateFormat df1 = DateFormat.getInstance (); System.out.println(df1.format(d)); DateFormat df2 = DateFormat.getDateInstance ( DateFormat.FULL , Locale.TAIWAN ); System.out.println(df2.format(d)); DateFormat df3 = DateFormat.getTimeInstance ( DateFormat.FULL , Locale.TAIWAN ); System.out.println(df3.format(d)); } } 靜態⽅法 功能說明 getInstance() 取得 NumberFormat 物件,使⽤的 Loacle 物件預 設為⽬前系統所屬的地域。 getInstance(Locale l) 指定 Locale 物件,取得 NumberFormat 物件。 getNumbrInstance() 取得 NumberFormat 物件,此物件的格式化功 能將只限制在格式化數值,使⽤的 Loacle 物件 預設為⽬前系統所屬的地域。 getNumbrInstance(Locale l) 指定 Locale 物件,取得 NumberFormat 物件,此 物件的格式化功能將只限制在格式化數值。 getCurrencyInstance() 取得 NumberFormat 物件,此物件的格式化功 能將只限制在格式化貨幣,使⽤的 Loacle 物件 預設為⽬前系統所屬的地域。 getCurrencyInstance(Locale l) 指定 Locale 物件,取得 NumberFormat 物件,此 物件的格式化功能將只限制在格式化貨幣。 靜態屬性 功能說明 DateFormat.SHORT 樣式 (style) 常數。如 12.13.52 3:30pm DateFormat.MEDIUM 樣式 (style) 常數。如 Jan 12, 1952 DateFormat.LONG 樣式 (style) 常數。如 January 12, 1952 3:30:32pm DateFormat.FULL 樣式 (style) 常數。如 Tuesday April 12 1952 AD 靜態⽅法 功能說明 getInstance() 取得 DateFormat 物件,使⽤的 Loacle 物件預 設為⽬前系統所屬的地域,此取得的 DateFormat 包含時間部份的格式化。 getDateInstance() 取得針對⽇期進⾏格式化的 DateFormat 件,使⽤的 Loacle 物件預設為⽬前系統所 屬的地域。 getDateInstance(int style, Locale l) 取得針對⽇期進⾏格式化的 DateFormat 件,設定 style 參數,指定⽇期顯⽰的長短 格式,並指定地域取得 DateFormat 物件。 style 參為可使⽤ DateFormat 取得,例如: 設定為 DateFormat.MEDIUM ,則⽇期格式 ”Jan 12, 2008” getTimeInstance() 取得針對時間進⾏格式化的 DateFormat 件,使⽤的 Loacle 物件預設為⽬前系統所 屬的地域。 getTimeInstance(int stype, Locale l) 取對針對時間進⾏格式化的 DateFormat 件,⽽ stype 參數指定時間顯⽰的長短格 式,最後以 l 參數指定地域取得 DateFormat 物件。 getDateTimeInstance() 取得針對⽇期與時間進⾏格式化的 DateFormat 物件,使⽤的 Loacle 物件預設為 ⽬前系統所屬的地域。 getDateTimeInstance(int dStype, int tStype, Locale l) 取得針對⽇期與時間進⾏格式化的 DateFormat 物件,設定 dStyle 參數指定⽇期 顯⽰的長短格式,⽽ tStype 參數指定時間 顯⽰的長短格式,最後以 l 參數指定地域取 DateFormat 物件。 ⽅法 功能說明 format( Date date) 格式化⽇期,成為⼀個字串。 J12_1_1 – Employee.java 1. 這是員⼯類別( Employee )。 J12_1_1 – Main.java 2. 透過國家屬性( country )指定員⼯所屬國籍,並以所 屬國家的貨幣單位設定薪資於薪資屬性上( salary )。 3. 這是老闆類別( Boss ),其中有個列印薪資單⽅法 printPayroll() ),會依代入的員⼯列印新資單資訊。 4. 將薪資屬性的值直接列印。 5. 建立 3 位員⼯,並設定所屬國家,與該國貨幣計價的薪資。 Jack ,美國國籍( US ), 150 美元; Mary ,法國國籍( FRANCE ), 100 歐元; Eric ,台灣國籍( TAIWAN ), 45000 台幣。 6. 建立老闆物件( Boss ),並列印3位員⼯的薪資單。但 要如何依每位員⼯所屬的國家的貨幣格式,列印薪資單呢?若 沒有各國的貨幣格式符號,恐怕這些⾦額會讓⼈產⽣誤會吧! J12_1_1 – Boss.java J12_1_2 – Employee.java J12_1_2 – Main.java J12_1_2 – Boss.java 1. 這是員⼯類別( Employee ),現在將國家屬性 country )的型別變更為 java.util 套件下的 Locale 型別,以透過 Locale 換定員⼯所屬的國家 2. 建構式的參數也改以傳入 Locale 別的物件,已代入國家屬性中。 3. 這是老闆類別( Boss ),其中有個列印薪資單⽅法 printPayroll() ),會依代入的員⼯列印新資單資訊。 4. 透過 java.text 套件下的 NumberFormat 類別的 getCurrencyInstance() ⽅法,取得將數值格式化成貨幣格式的⽂字,傳入的參數即為員⼯的國家屬性,其 Locale 型別的物件,⽤以指定特定國家的貨幣格式。 5. 透過 NumberFormat 上的 format() ⽅法,實際將員⼯的薪資 格式化成所屬國家貨幣的格式,並以字串( String )⽅式回傳。 6. 最後將格式化好的薪資⽂字列印出來。 7. 因此,只要在建立員⼯物件的時候,透過 Locale 指定員⼯所屬的國家。 8. 老闆物件( Boss )即可透過列印薪資單⽅法,依照 每位員⼯所屬的國家,列印出不同國家的薪資格式。 J12_1_3 – Employee.java J12_1_3 – Main.java 1. 現在為員⼯類別( Employee )加上⼯作總時數屬性( hours )。 2. 並透過建構式的參數代入⼯作總時數屬性的值。 3. 在建立員⼯物件時,代入⼯作總時數。 4. 透過 java.text 套件下的 NumberFormat 類別的 getNumberInstance() ⽅法,取得將數值格式化特定國家格 式的⽂字,傳入的參數即為員⼯的國家屬性,其是透過 Locale 型別的物件,⽤以指定特定國家的數值格式。 5. 最後呼叫 format() ⽅法,實際的將數值格式化成特定國家的數值格式⽂字。 6. 特別的是 Locale 指定為 Locale.FRANCE 格式化後 的數值,千分位會以句號( . )表⽰,⽽⼩數點會以逗號 , )的⽅式表⽰,此為法國( France )的數值格式。  J12_1_4 – Boss.java J12_1_4 – Main.java 1. 這是老闆類別( Boss ),其中有個列印薪資單⽅法 printPayroll() ),會依代入的員⼯列印新資單資訊。 2. 透過 new Date() 建立 Date 物件,以取得⽬前的 ⽇期時間,並列印出此時間以表⽰列印薪資單的時間。 3. 雖然已使⽤ Locale 指定每位員⼯所屬的國家。 4. 要如何依所屬國家的⽇期格式,顯⽰列印薪資單的⽇期呢? J12_1_5 – Boss.java 1. 這是老闆類別( Boss ),列印薪資單⽅法 printPayroll() )會依代入的員⼯列印新資單資訊。 3. 1 2 參數,是⽤與指定⽇期與時間完整 顯⽰的程序,使⽤ DateFormat.FULL 常數值 代入表⽰要以最完整的⽅式顯⽰。 2. 透過 java.text 套件下的 DateFormat 類別,呼叫 getDateTimeInstaance() 取得格式化⽇期時間的物件。 4. 3 參數,則透過員⼯物件代入所 屬的國家資訊,即 Locale 物件。 5. 透過 format() ⽅法實際將⽇期格式化成特定國家的 ⽇期格式,並將格式化後的⽂字存於 dateText 變數中。 6. 最後只要將格式化後的⽂字輸出,即可顯⽰特定國家的格式化⽂字。 J12_1_6 – Main.java 1. getInstance() ⽅法取得的物件,等同於呼叫 getDateTimeInstance() ⽅法取得物件相同,皆可針對⽇期時間 進⾏格式化,⽽預設的 Locale 即為⽬前作業系統所屬的地域。 2. getDateInstance() ⽅法取得的物件,將指針對⽇期部 份進⾏格式化,格式化後的⽂字並不會包含時間部份的資訊。 3. getTimeInstance() ⽅法取得的物件,將指針對時間部 份進⾏格式化,格式化後的⽂字並不會包含⽇期部份的資訊。 老闆列印薪資單, 如何讓每位員⼯皆可以收到該國家格式的薪資單呢? NumberFormat Locale 類別, 產⽣不同國家的薪資格式 NumberFormat 為⼯作總時數加上千分位與⼩數點 1 2 3 1 2 3 NumberFormat 類別常⽤的⽅法 薪資單上的⽇期時間, ⼜如何格式化成特定國家的格式呢? 1 2 3 1 2 3 DateFormat 類別常⽤的屬性與⽅法

留言