java.text鍖呮牸寮忓寲鏁板瓧鍜屾棩鏈
鍙戝竷: 2008-12-09 18:14 | 浣滆: webmaster | 鏉ユ簮: 鏈珯鍘熷垱 | 鏌ョ湅: 22娆
娉ㄦ剰锛氱敱浜庤繖閲岃寰楀緢绠鐣ワ紝鍥犳璇峰弬鐓т笅闈㈢殑Demo绋嬪簭銆
鏍煎紡鍖栨暟瀛
鍦NumberFormat绫讳腑涓烘垜浠彁渚涗簡鏍煎紡鍖4绉嶆暟瀛楃殑鏂规硶锛氭暣鏁般佸皬鏁般佽揣甯佸拰鐧惧垎姣旓紝閫氳繃宸ュ巶鏂规硶getNumberInstance, getNumberIntance, getCurrencyInstance, getPercentInstance鏂规硶鑾峰緱鐩稿簲鐨勫疄渚嬪璞″氨琛屻備緥濡傛垜浠浠ュ瓧绗︿覆琛ㄧず浜烘皯甯88888.88鍏冿紝杩欐牱鏉ュ啓灏辫锛
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(88888.88));
瀹氬埗鏍煎紡鍖栨暟瀛
鍙槸瀵逛簬绋嶅井澶嶆潅涓鐐圭殑闇姹傦紝NumberFormat灏辨弧瓒充笉浜嗕簡锛屽垢濂java杩樻彁渚涗簡DecimalFormat瀹炵幇瀹氬埗鐨勬牸寮忓寲銆傝浣跨敤DecimalFormat瀵硅薄锛屽繀椤绘彁渚涚粰瀹冩彁渚涗竴涓牸寮忓寲鐨勬ā寮(pattern)锛
String pattern = 鈥
DecimalFormat df = new DecimalFormat(pattern);
鎴栬咃細
DecimalFormat df = new DecimalFormat();
df. applyPattern(pattern);
鐒跺悗灏辫皟鐢ㄥ畠鐨format鏂规硶灏辫浜嗐
鎵浠ュ叧閿氨鏄繖涓ā寮忔庝箞瀹氫箟銆傚湪DecimalFormat绫荤殑JavaDoc涓湁妯″紡鐨勮娉曡〃绀猴紝涓嶈繃寰堥毦璇存竻妤氾紙鏄垜璇翠笉娓呮锛屽懙鍛碉級锛岃鐪嬬湅Demo鑷繁澶氳瘯璇曞惂銆備笅闈㈡槸妯″紡涓煇浜涘瓧绗︾殑鍚箟琛細
|
瀛楃 |
鍚箟 |
|
0 |
涓浣嶆暟瀛楋紝杩欎竴浣嶇己澶辨樉绀轰负0銆傜敤鏉ヨˉ闆 |
|
# |
涓浣嶆暟瀛, 杩欎竴浣嶇己澶卞氨涓嶆樉绀 |
|
. |
灏忔暟鐐癸紝涓嶇敤澶氳浜嗗惂 |
|
, |
鍗冧綅鍒嗛殧绗 |
|
E |
绉戝璁℃暟娉 |
|
% |
鐧惧垎姣 |
鏍煎紡鍖栨棩鏈
鎶婃棩鏈熻浆鍖栦负瀛楃涓叉渶绠鍗曠殑鏂规硶灏辨槸璋冪敤Date绫荤殑toString鎴栬toLocaleString鏂规硶锛
System.out.println(new Date());
杈撳嚭锛2004-8-7 8:16:14銆傚彲鏄鏋滄垜浠兂鎶婃湀鍜屾棩琛ユ垚2浣嶄笉瑕佹椂鍒嗙2004-08-07锛岃繖灏变笉鐏典簡銆java.text.DateFormat鎻愪緵浜嗗ぇ閲忕殑宸ュ巶鏂规硶锛getDateInstance(int style), getTimeInstance(int style), getDateTimeInstance(int dateStyle, int timeStyle)绛夌瓑銆傚叾涓style蹇呴』鏄DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT涔嬩竴銆Demo涓殑defaultDateFormat鏂规硶浣滀簡涓涓畝鍗曠殑瀹為獙銆
瀹氬埗鏍煎紡鍖栨棩鏈燂細
鍚屾牱锛java.text.SimpleDateFormat鍙互閫氳繃妯″紡(pattern)瀹炵幇瀹氬埗鏍煎紡鍖栵細
String pattern = 鈥
SimpleDateFormat df = new SimpleDateFormat(pattern);
鎴栬咃細
SimpleDateFormat df = new SimpleDateFormat();
df. applyPattern(pattern);
涓嬮潰鏄SimpleDateFormat鐨javadoc涓殑涓涓ā寮忕鍙风畝琛細
|
绗﹀彿 |
鎰忎箟 |
鍚堟硶鏁板 |
绀轰緥 |
|
y |
Year |
1996; 96 | |
|
M |
Month in year |
July; Jul; 07 | |
|
d |
Day in month |
10 | |
|
a |
Am/pm marker |
PM | |
|
H |
Hour in day (0-23) |
0 | |
|
h |
Hour in am/pm (1-12) |
12 | |
|
m |
Minute in hour |
30 | |
|
s |
Second in minute |
55 | |
|
S |
Millisecond |
978 | |
|
z |
Time zone |
Pacific Standard Time; PST; GMT-08:00 | |
|
Z |
Time zone |
-0800 |
娉ㄦ剰鐨勬槸锛岀鍙风殑澶у皬鍐欐剰涔夋槸涓嶅悓鐨勶紝绗﹀彿鐨勪釜鏁颁篃浼氬鑷磋緭鍑轰笉涓鏍枫備緥濡傜敤MM灏变細鎶1鏈堜唤鏄剧ず鎴01锛岃岀敤M鍒欎笉浼氳ˉ闆躲傚浜庡勾浠斤紝涓や釜yy浼氬彧杈撳嚭涓や綅骞翠唤锛yyyy鍒欎細杈撳嚭4浣嶅勾浠姐
瀹為檯涓婏紝涓婇潰鐨勭被杩樻彁渚涗簡寰堝鍏朵粬鏂规硶锛岀壒鍒槸鐢ㄤ簬鏈湴鍖(Locale)瀹氬埗鏍煎紡鍖栫殑鏂规硶锛屼互鍙婁粠瀛楃涓茶〃绀鸿浆鍖栦负鐩稿簲瀵硅薄鐨parse鏂规硶锛岃繕鏈夋妸鏍煎紡鍖栫粨鏋滈檮鍔犲埌涓涓StringBuffer鐨勬柟娉曪紙搴旇鏄敤鏉ユ彁楂樻ц兘锛夈
鏈鍚庢槸涓涓皬Demo鍜岃緭鍑虹粨鏋滐細
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));
//杩欓噷娌℃湁娑夊強鐩稿簲鐨parse鏂规硶
}
public static void customNumberFormat() {
double x = 1000.0 / 3;
System.out.println("default output is " + x);
patternPrint("###,###.##", x);
patternPrint("####.##", x);
patternPrint("####.00", x);




发表于 2009-1-5 19:48
| 











