zoukankan      html  css  js  c++  java
  • JAVA核心技术I---JAVA基础知识(格式化相关类)

    一:格式化相关类

    (一)java.text包java.text.Format的子类

    –NumberFormat:数字格式化,抽象类
         DecimalFormat
    –MessageFormat:字符串格式化
    –DateFormat:日期/时间格式化,抽象类
         SimpleDateFormat

    (二)java.time.format包下

    –DateTimeFormatter

    二:相关类的使用

    (一)NumberFormat的使用

     

            NumberFormat nf = NumberFormat.getInstance();
            System.out.println(nf.format(100000));
            String name="100000";
            try {
                System.out.println(nf.parse(name));
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
    100,000
    100000
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    100,000.00

            String name="100d000";
            try {
                System.out.println(nf.parse(name));
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
    100
            String name="d100000";
            try {
                System.out.println(nf.parse(name));
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
    Unparseable number: "d100000"

    (二)DecimalFormat的使用

            DecimalFormat df1,df2;
             
            System.out.println("整数部分为0的情况,0/#的区别");
            // 整数部分为0 , #认为整数不存在,可不写; 0认为没有,但至少写一位,写0
            df1 = new DecimalFormat("#.00");
            df2 = new DecimalFormat("0.00");
             
            System.out.println(df1.format(0.1)); // .10  
            System.out.println(df2.format(0.1)); // 0.10  
             
            System.out.println("小数部分0/#的区别");
            //#代表最多有几位,0代表必须有且只能有几位,同C中的域宽,当域宽小于整数部分,整数部分会失效,全部显示
            df1 = new DecimalFormat("0.00");
            df2 = new DecimalFormat("0.##");
             
            System.out.println(df1.format(0.1)); // 0.10
            System.out.println(df2.format(0.1)); // 0.1
             
            System.out.println(df1.format(0.006)); // 0.01
            System.out.println(df2.format(0.006)); // 0.01
             
            System.out.println("整数部分有多位");
            //0和#对整数部分多位时的处理是一致的 就是有几位写多少位
            df1 = new DecimalFormat("0.00");
            df2 = new DecimalFormat("#.00");
             
            System.out.println(df1.format(2)); // 2.00
            System.out.println(df2.format(2)); // 2.00
             
            System.out.println(df1.format(20)); // 20.00
            System.out.println(df2.format(20)); // 20.00
             
            System.out.println(df1.format(200)); // 200.00
            System.out.println(df2.format(200)); // 200.00
    整数部分为0的情况,0/#的区别
    .10
    0.10
    小数部分0/#的区别
    0.10
    0.1
    0.01
    0.01
    整数部分有多位
    2.00
    2.00
    20.00
    20.00
    200.00
    200.00
            df1 = new DecimalFormat("0.00");
            df2 = new DecimalFormat("0.##");
             
            System.out.println(df1.format(10.1)); // 0.10
            System.out.println(df2.format(10.1)); // 0.1
    10.10
    10.1

    (三)MessageFormat的使用(字符串格式化)

    –支持多个参数-值对位复制文本
    –支持变量的自定义格式
             int planet = 7;
             String event = "a disturbance in the Force";
    
             String result = MessageFormat.format(
                 "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
                 planet, new Date(), event);  //planet对应0处,new Date()对应1,event对应2
             
             System.out.println(result);
    At 下午3:52:37 on 2019年1月3日, there was a disturbance in the Force on planet 7.
             int fileCount = 1273;
             String diskName = "MyDisk";
             Object[] testArgs = {new Long(fileCount), diskName};
    
             MessageFormat form = new MessageFormat(
                 "The disk "{1}" contains {0} file(s).");    //转义
    
             System.out.println(form.format(testArgs));
    The disk "MyDisk" contains 1,273 file(s).
             MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
             Object[] objs = {new Double(3.1415)};
             String result = mf.format( objs );
             System.out.println(result);
    3.14, 3.1
            String message = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}";  
            Object[] array = new Object[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"};  
            String value = MessageFormat.format(message, array);  
            System.out.println(value);  
            
            message = "oh, {0,number,#.##} is a good number"; 
            array = new Object[]{new Double(3.1415)};  
            value = MessageFormat.format(message, array);
            System.out.println(value);  
    ABCDEFGHIJKLMNOPQ
    oh, 3.14 is a good number

    parse的使用(重点):按照格式解析字符串,返回数组对象

             MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
             Object[] objs = {new Double(3.1415)};
             String result = mf.format( objs );
             // result now equals "3.14, 3.1"
             objs = null;
             objs = mf.parse(result, new ParsePosition(0));
             for(int i=0;i<objs.length;i++) {
                 System.out.println(objs[i]);
             }
    3.1
    ParsePosition设置访问起始下标,0表示从{0}开始全部获取,1表示从{1}开始索引,但是顺序不变0,1,...
             MessageFormat mf2 = new MessageFormat("{0}, {2}, {1}");
             String forParsing = "x, y, z";
             Object[] obj2 = mf2.parse(forParsing, new ParsePosition(0));
             // result now equals {new String("z")}
    
             for(int i=0;i<obj2.length;i++) {
                 System.out.println(obj2[i]);
             }
    x
    z
    y

    (四)DateFormat的使用(时间格式化,抽象类)

    –SimpleDateFormat 工厂模式
    –parse:将字符串格式化为时间对象
    –format:将时间对象格式化为字符串
    –如将当前时间转为化YYYY-MM-DD HH24:MI:SS
            String strDate = "2008-10-19 10:11:30.345" ;  
            // 准备第一个模板,从字符串中提取出日期数字  
            String pat1 = "yyyy-MM-dd HH:mm:ss.SSS" ;  
            // 准备第二个模板,将提取后的日期数字变为指定的格式  
            String pat2 = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒" ;  
            SimpleDateFormat sdf1 = new SimpleDateFormat(pat1) ;        // 实例化模板对象  
            SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ;        // 实例化模板对象  
            Date d = null ;  
            try{  
                d = sdf1.parse(strDate) ;   // 将给定的字符串中的日期提取出来  
            }catch(Exception e){            // 如果提供的字符串格式有错误,则进行异常处理  
                e.printStackTrace() ;       // 打印异常信息  
            }  
            System.out.println(sdf2.format(d)) ;    //日期变为新的格式  
    –parse:将字符串格式化为时间对象
    –format:将时间对象格式化为字符串

    三:格式化练习

    (一)验证身份证号码是否正确

    输入一个字符串,请判断是否满足身份证基本要求,并返回具体的生日yyyy-mm-dd。如果输入数据有误,请输出0000-00-00。基本要求是:
    a)必须是18位;
    b) 前面位数必须是数字,最后一位可以是数字或小写字母;
    c) 日期是存在的。
    输入格式:
    一个身份证号码,18位字符串
    
    输出格式:
    yyyy-mm-dd
    
    输入样例:
    53010219200508011x
    
    输出样例:
    1920-05-08
    import java.text.*;
    import java.util.*;
    
    public class ClasslibTest {
        public static int judge(String iden) {
            if(iden.length() != 18) {
                return -1;
            }
            
            int i=0;
            int flag = 1;
            
            for(i=0;i<iden.length()-1;i++) {
                if(!(iden.charAt(i)>='0'&&iden.charAt(i)<='9')) {
                    flag=0;
                    break;
                }
            }
            
            if(!((iden.charAt(i)>='0'&&iden.charAt(i)<='9')||(iden.charAt(i)>='a'&&iden.charAt(i)<='z'))) {
                flag=0;
            }
            
            if(flag==0) {
                return -2;
            }
            
            return flag;
        }
        
        public static String getDate(String iden) {
            String sub=iden.substring(6, 14);
            return sub;
        }
        
        public static void main(String[] args) {
            Scanner inp = new Scanner(System.in);
            String strDate = inp.next() ;  
            int flag = judge(strDate);
            
            if(flag!=1) {
                return ;
            }
            
            String substr=getDate(strDate);
            
            String part1 = "yyyyMMdd";
            String part2 = "yyyy-MM-dd";
            
            SimpleDateFormat sf = new SimpleDateFormat(part1);
            SimpleDateFormat sf2 = new SimpleDateFormat(part2);
            
            Date d=null;
            
            try {
                d = sf.parse(substr);    //获取时间
            }catch(Exception e) {
                e.printStackTrace();
                return;
            }
            
            System.out.println(sf2.format(d));
            
        }
    }
    实现代码
    53010219200508011x
    1920-05-08

    (二)验证身份证号码是否正确(带校验算法)

    输入一个字符串,请判断是否满足身份证基本要求,并返回具体的生日yyyy-mm-dd。
    如果输入数据有误,请输出0000-00-00。基本要求是:
    a)必须是18位;
    b) 前面位数必须是数字,最后一位可以是数字或小写字母;
    c) 日期是存在的;
    d)最后一位校验码检查。
    校验码规则如下:
    
    1、将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:79105842163791058422、将这17位数字和系数相乘的结果相加。
    
    3、用加出来和除以11,看余数是多少?
    
    4、余数只可能有0-123456789-10这11个数字。其分别对应的最后一位身份证的号码为1-0-X-987654325、通过上面得知如果余数是3,就会在身份证的第18位数字上出现的是9。如果对应的数字是10,身份证的最后一位号码就是罗马数字x。
    import java.text.*;
    import java.util.*;
    
    public class ClasslibTest {
        public static int judge(String iden) {
            if(iden.length() != 18) {
                return -1;
            }
            
            int i=0;
            int flag = 1;
            
            for(i=0;i<iden.length()-1;i++) {
                if(!(iden.charAt(i)>='0'&&iden.charAt(i)<='9')) {
                    flag=0;
                    break;
                }
            }
            
            if(!((iden.charAt(i)>='0'&&iden.charAt(i)<='9')||(iden.charAt(i)>='a'&&iden.charAt(i)<='z'))) {
                flag=0;
            }
            
            if(flag==0) {
                return -2;
            }
            
            return flag;
        }
        
        public static String getDate(String iden) {
            String sub=iden.substring(6, 14);
            return sub;
        }
        
        public static void printDate(String strDate) {
        
            String substr=getDate(strDate);
            
            String part1 = "yyyyMMdd";
            String part2 = "yyyy-MM-dd";
            
            SimpleDateFormat sf = new SimpleDateFormat(part1);
            SimpleDateFormat sf2 = new SimpleDateFormat(part2);
            
            Date d=null;
            
            try {
                d = sf.parse(substr);    //获取时间
            }catch(Exception e) {
                e.printStackTrace();
                return;
            }
            
            System.out.println(sf2.format(d));
        }
        
        public static int checkIden(String strDate) {
            int flag = judge(strDate);
            
            if(flag!=1) {
                return -1;
            }
            
            int everPos[]= {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
            int LastPos[]= {1,0,-1,9,8,7,6,5,4,3,2};    //-1表示X
            int n=0;
            for(int i=0;i<strDate.length()-1;i++) {
                n+=(strDate.charAt(i)-'0')*everPos[i];
            }
            
            n %= 11;
            
            if((LastPos[n]==-1&&strDate.charAt(17)=='x')||(LastPos[n]==(strDate.charAt(17)-'0')))
                return 1;
            else
                return -2;
        }
        
        public static void main(String[] args) {
    
            Scanner inp = new Scanner(System.in);
            String strDate = inp.next() ;  
            int flag = checkIden(strDate);
            
            if(flag==1) {
                printDate(strDate);
            }else {
                System.out.println("0000-00-00");
            }
        }
    }
    实现代码
    23402613390168801x
    0000-00-00
    
    53010219200508011x
    1920-05-08
  • 相关阅读:
    Unix命令大全
    vs2008 与 IE8出现的兼容性问题
    Java 创建文件、文件夹以及临时文件
    如何修改Wamp中mysql默认空密码
    PAT 乙级真题 1003.数素数
    Tags support in htmlText flash as3
    DelphiXE4 FireMonkey 试玩记录,开发IOS应用 还是移植
    10 Great iphone App Review sites to Promote your Apps!
    HTML tags in textfield
    Delphi XE4 IOS 开发, "No eligible applications were found“
  • 原文地址:https://www.cnblogs.com/ssyfj/p/10214798.html
Copyright © 2011-2022 走看看