java.text.format

本文详细介绍了Java中数字格式化(DecimalFormat)、日期格式化(simpleDateFormat)和文本替换格式化(MessageFormat)的使用方法,包括具体实例演示,并强调了在数据库中保存日期时应注意的事项。

oh ,now it's time to sum up the class of "java.text.format" , as i forget how to format a date after using other's tools .

1、数字格式化   DecimalFormat

使用 java.text.format  decimalFormat = new DecimalFormat("#.##"); 保留2位小数,允许整数与小数不存在。

使用 java.text.format decimalFormat = new DecimalFormat("0.0#");保留2位小数,无小数,小数位补0 。 无整数,整数位补0


2、日起格式化   simpleDateFormat

使用java.text.format dateFormat = new SimpleDateFormat(pattern) .

pattern : yyyy-MM-dd HH:mm:ss SSS E 表示 2014-12-06 23:59:58 333 星期四  and then ,you can short of the pattern just to suit yourself

注意 : 使用mysql格式化日起保存到数据库的时候,请不要用毫秒,否则会四舍五入,  2014-12-06 23:59:58 500 会保存为 2014-12-07 00:00:00


3、文本替换格式化 MessageFormat

描述:使用一个数组中的元素去替换String(模板)中{ArgumentIndex , FormatType , FormatStyle}中的指定位置

{ArgumentIndex , FormatType , FormatStyle} : {数组下标,格式化数组类型,格式}

例如 : String message = "xiao ming is {0} {1,number,#.##}" , Object  array = new Object[]{"high",3.33333} , String value = MessageFormat.format(message,array);

结果 : xiao ming is high 3.33 .   message就是模板。

then just thinking the result   if the message is "xiao ming is {} " . 是会全部打出呢 还是 说取消大括号 ?

建议使用:new MessageFormat(message).format(array);  因为上面的代码我无法得出正确结论 ,同时下面的代码就是上面代码的解析。。


for more message , just view  http://www.xuebuyuan.com/1576172.html 


anr问题trace如下: "main" prio=5 tid=1 Blocked | group="main" sCount=1 ucsCount=0 flags=1 obj=0x72df6468 self=0xb400007072878c00 | sysTid=3104 nice=-2 cgrp=ssfg sched=0/0 handle=0x71230e9d28 | state=S schedstat=( 273294290029 918944300434 897810 ) utm=18065 stm=9263 core=5 HZ=100 | stack=0x7fe106e000-0x7fe1070000 stackSize=8188KB | held mutexes= at com.android.server.alarm.AlarmManagerService$DeliveryTracker.onSendFinished(AlarmManagerService.java:5798) - waiting to lock <0x09677b19> (a java.lang.Object) held by thread 281 at android.app.PendingIntent$FinishedDispatcher.run(PendingIntent.java:382) at android.os.Handler.handleCallback(Handler.java:973) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loopOnce(Looper.java:282) at android.os.Looper.loop(Looper.java:387) at com.android.server.SystemServer.run(SystemServer.java:1069) at com.android.server.SystemServer.main(SystemServer.java:677) at java.lang.reflect.Method.invoke(Native method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:983) DumpLatencyMs: 212.411 "binder:3104_E" prio=5 tid=281 Runnable | group="main" sCount=0 ucsCount=0 flags=0 obj=0x1770d568 self=0xb400006e2db00800 | sysTid=4606 nice=-2 cgrp=ssfg sched=1073741824/0 handle=0x6e0b1ab730 | state=R schedstat=( 113789554601 690500424244 405734 ) utm=6620 stm=4758 core=5 HZ=100 | stack=0x6e0b0b4000-0x6e0b0b6000 stackSize=989KB | held mutexes= "mutator lock"(shared held) at android.icu.util.Currency.getAvailableCurrencyCodes(Currency.java:200) at libcore.icu.ICU.getCurrencyCode(ICU.java:671) at java.util.Currency.getInstance(Currency.java:421) at java.text.DecimalFormatSymbols.initializeCurrency(DecimalFormatSymbols.java:933) at java.text.DecimalFormatSymbols.setCurrency(DecimalFormatSymbols.java:508) at java.text.DecimalFormatSymbols.fromIcuInstance(DecimalFormatSymbols.java:1085) at java.text.DecimalFormat.getDecimalFormatSymbols(DecimalFormat.java:2819) at java.text.SimpleDateFormat.zeroPaddingNumber(SimpleDateFormat.java:1678) at java.text.SimpleDateFormat.subFormat(SimpleDateFormat.java:1593) at java.text.SimpleDateFormat.format(SimpleDateFormat.java:1117) at java.text.SimpleDateFormat.format(SimpleDateFormat.java:1087) at java.text.DateFormat.format(DateFormat.java:333) at java.text.Format.format(Format.java:159) at com.android.server.alarm.AlarmManagerService.setImplLocked(AlarmManagerService.java:2473) at com.android.server.alarm.AlarmManagerService.setImpl(AlarmManagerService.java:2425) - locked <0x09677b19> (a java.lang.Object) at com.android.server.alarm.AlarmManagerService$4.set(AlarmManagerService.java:3103) at android.app.IAlarmManager$Stub.onTransact(IAlarmManager.java:198) at android.os.Binder.execTransactInternal(Binder.java:1523) at android.os.Binder.execTransact(Binder.java:1457) DumpLatencyMs: 84.4358这最后分析是什么结果呢
最新发布
08-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值