FieldPosition和ParsePosition

  1. package net.zcmusicbox.yeah.test.text.format;  
      
    import java.math.BigDecimal;  
    import java.text.DateFormat;  
    import java.text.FieldPosition;  
    import java.text.NumberFormat;  
    import java.text.ParsePosition;  
    import java.text.SimpleDateFormat;  
    import java.util.Date;  
      
    public class SetEndIndex {  
        public static void main(String args[]) throws Exception {  
            NumberFormat numForm = NumberFormat.getInstance();  
            StringBuffer dest1 = new StringBuffer();  
            FieldPosition pos = new FieldPosition(NumberFormat.INTEGER_FIELD);  
            BigDecimal bd1 = new BigDecimal(2.342323232323D);  
            dest1 = numForm.format(bd1, dest1, pos);  
            System.out.println("dest1 = " + dest1);  
            System.out.println("INTEGER portion is at: " + pos.getBeginIndex() + ", " + pos.getEndIndex());  
            pos = new FieldPosition(NumberFormat.FRACTION_FIELD);  
            StringBuffer dest2 = new StringBuffer();  
            dest2 = numForm.format(bd1, dest2, pos);  
            System.out.println("dest2 = " + dest2);  
            System.out.println("FRACTION portion is at: " + pos.getBeginIndex() + ", " + pos.getEndIndex());  
      
            DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);  
            StringBuffer dest3 = new StringBuffer();  
            //关注的是几号  
            pos = new FieldPosition(DateFormat.DATE_FIELD);  
            dest3 = df.format(new Date(), dest3, pos);  
            System.out.println("dest3 = " + dest3);  
            //结果当前时间为2012年6月27日 下午04时06分56秒  则beginIndex为7 endIndex为9  
            System.out.println("FRACTION portion is at: " + pos.getBeginIndex() + ", " + pos.getEndIndex());  
      
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
            String input[] = { "abc 2013-10-01 Vancouver, B.C.", "1248-03-01 Ottawa, ON", "1323-06-06 Toronto, ON" };  
            for (int i = 0; i < input.length; i++) {  
                ParsePosition pp = new ParsePosition(4);//从第四位开始处理  
                Date d = formatter.parse(input[i], pp);  
                if (d == null) {  
                    //结果只处理了"abc 2013-10-01 Vancouver, B.C."   
                    //"1248-03-01 Ottawa, ON"从第四位开始是"8-03-01 Ottawa, ON"无法转换  
                    System.err.println("Invalid date in " + input[i]);  
                    continue;  
                }  
                //成功转换后ParsePosition.getIndex()就是匹配的字符串结尾的索引  
                String location = input[i].substring(pp.getIndex());  
                System.out.println(" on " + d + " in " + location);  
      
            }  
        }  
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值