当前位置: 移动技术网 > IT编程>开发语言>Java > 16_常用API_第16天(正则表达式、Date、DateFormat、Calendar)_讲义

16_常用API_第16天(正则表达式、Date、DateFormat、Calendar)_讲义

2018年08月28日  | 移动技术网IT编程  | 我要评论

今日内容介绍
1、正则表达式的定义及使用
2、date类的用法
3、calendar类的用法

==========================================第一阶段========================================

01正则表达式的概念和作用

  • a: 正则表达式的概念和作用
    • a: 正则表达式的概述
      • 正则表达式也是一个字符串,用来定义匹配规则,在pattern类中有简单的规则定义。
        可以结合字符串类的方法使用。
      • 简单记:正则表达式是具有特殊含义的字符串。
    • b: 正则表达式的作用
    • 比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个限制长度的事情就是正则表达式做的

02正则表达式语法规则

  • a: 正则表达式语法规则
    • a: 字符
      • x 代表的是字符x
      • \ 代表的是反斜线字符''
      • \t 代表的是制表符
      • \n 代表的是换行符
      • \r 代表的是回车符
    • b: 字符类
      • [abc] a、b 或 c(简单类)
      • [^abc] 任何字符,除了 a、b 或 c(否定)
      • [a-za-z] a到 z 或 a到 z,两头的字母包括在内(范围)
      • [0-9] 0到9的字符都包括
      • [a-za-z_0-9] 代表的字母或者数字或者下划线(即单词字符)
    • c: 预定义字符类
      • . 任何字符。
      • \d 数字:[0-9]
      • \w 单词字符:[a-za-z_0-9]如"com.itheima.tests"/finish
    • d: 边界匹配器
      • ^ 代表的是行的开头
      • $ 代表的是行的结尾
      • \b 代表的是单词边界
    • e: 数量词
      • x? x,一次或一次也没有
      • x* x,零次或多次
      • x+ x,一次或多次
      • x{n} x,恰好 n 次
      • x{n,} x,至少 n 次
      • x{n,m} x,至少 n 次,但是不超过 m 次

03正则表达式练习和相关的string类方法

  • a: 正则表达式练习和相关的string类方法
    • a: boolean matches(string 正则的规则)
      • "abc".matches("[a]")
      • 匹配成功返回true
    • b: string[] split(string 正则的规则)
      • "abc".split("a")
      • 使用规则将字符串进行切割
    • c: string replaceall( string 正则规则,string 字符串)
      • "abc0123".repalceall("[\d]","#")
      • 按照正则的规则,替换字符串

04正则表达式匹配练习

* a: 正则表达式匹配练习
    * a: 案例代码
        public class regexdemo {
            public static void main(string[] args) {
                checktel();
            }
            
            
            /*
             *  检查手机号码是否合法
             *  1开头 可以是34578  0-9 位数固定11位
             */
            public static void checktel(){
                string telnumber = "1335128005";
                //string类的方法matches
                boolean b = telnumber.matches("1[34857][\\d]{9}");
                system.out.println(b);
            }
            
            /*
             *  检查qq号码是否合法
             *  0不能开头,全数字, 位数5,10位
             *  123456 
             *  \\d  \\d匹配不是数字
             */
            public static void checkqq(){
                string qq = "123456";
                //检查qq号码和规则是否匹配,string类的方法matches
                boolean b = qq.matches("[1-9][\\d]{4,9}");
                system.out.println(b);
            }
        }

05正则表达式切割练习

* a: 正则表达式切割练习
    * a: 案例代码
        public class regexdemo1 {
            public static void main(string[] args) {
                split_1();
                split_2();
                split_3();

            }
            
            /*
             * string类方法split对字符串进行切割
             * 192.168.105.27 按照 点切割字符串
             */
            public static void split_3(){
                string ip = "192.168.105.27";
                string[] strarr = ip.split("\\.");
                system.out.println("数组的长度"+strarr.length);
                for(int i = 0 ; i < strarr.length ; i++){
                    system.out.println(strarr[i]);
                }
            }
            
            /*
             * string类方法split对字符串进行切割
             * 18 22 40 65 按照空格切割字符串
             */
            public static void split_2(){
                string str = "18    22     40          65";
                string[] strarr = str.split(" +");
                system.out.println("数组的长度"+strarr.length);
                for(int i = 0 ; i < strarr.length ; i++){
                    system.out.println(strarr[i]);
                }
            }
            
            /*
             *  string类方法split对字符串进行切割
             *  12-25-36-98  按照-对字符串进行切割
             */
            public static void split_1(){
                string str = "12-25-36-98";
                //按照-对字符串进行切割,string类方法split
                string[] strarr = str.split("-");
                system.out.println("数组的长度"+strarr.length);
                for(int i = 0 ; i < strarr.length ; i++){
                    system.out.println(strarr[i]);
                }
            }
        }       

06正则表达式替换练习

* a: 正则表达式替换练习
    * a: 案例代码
        public class regexdemo1 {
            public static void main(string[] args) {
                replaceall_1();
            }
            
            /*
             * "hello12345world6789012"将所有数字替换掉
             * string类方法replaceall(正则规则,替换后的新字符串)
             */
            public static void replaceall_1(){
                string str = "hello12345world6789012";
                str = str.replaceall("[\\d]+", "#");
                system.out.println(str);
            }
        }
        

07正则表达式邮箱地址验证

* a: 正则表达式邮箱地址验证
    * a: 案例代码
        public class regexdemo2 {
            public static void main(string[] args) {
                checkmail();
            }
            /*
             *  检查邮件地址是否合法
             *  规则:
             *   1234567@qq.com
             *   mym_ail@sina.com
             *   nimail@163.com
             *   wodemail@yahoo.com.cn    
             *   
             *   @: 前  数字字母_ 个数不能少于1个
             *   @: 后  数字字母     个数不能少于1个
             *   .: 后面 字母 
             *     
             */
            public static void checkmail(){
                string email ="abc123@sina.com";
                boolean b = email.matches("[a-za-z0-9_]+@[0-9a-z]+(\\.[a-z]+)+");
                system.out.println(b);
            }
        }
            

08毫秒值概念

  • a: 毫秒值概念
    • a: 时间和日期类
      • java.util.date
    • b: 毫秒概念
      • 1000毫秒=1秒
    • c: 毫秒的0点
      • system.currenttimemillis() 返回值long类型参数
      • 获取当前日期的毫秒值 3742769374405
      • 时间原点; 公元1970年1月1日,午夜0:00:00 英国格林威治 毫秒值就是0
      • 时间2088年8月8日
      • 时间和日期的计算,必须依赖毫秒值

09date类的构造方法

* a: date类的构造方法
    * a: 空参构造
        * public date()
    * b: 带参构造
        * public date(long times)

    

==============================第二阶段====================================

10date类的get和set方法

  • a:date类的get和set方法
    • public long gettime()
      • 将当前的日期对象,转为对应的毫秒值
    • public void settime(long times);
      • 根据给定的毫秒值,生成对应的日期对象

11日期格式化simpledateformat

  • a: 日期格式化simpledateformat
    • a: 对日期进行格式化(自定义)
      • 对日期格式化的类 java.text.dateformat 抽象类, 普通方法,也有抽象的方法
      • 实际使用是子类 java.text.simpledateformat 可以使用父类普通方法,重写了抽象方法
    • b: 对日期进行格式化的步骤
      • 1: 创建simpledateformat对象
        • 在类构造方法中,写入字符串的日期格式 (自己定义)
      • 2: simpledateformat调用方法format对日期进行格式化
        • public string format(date date) 传递日期对象,返回字符串
        • 日期模式:
        • yyyy 年份
        • mm 月份
        • dd 月中的天数
        • hh 0-23小时
        • mm 小时中的分钟
        • ss 秒
        • yyyy年mm月dd日 hh点mm分钟ss秒 汉字修改,: - 字母表示的每个字段不可以随便写

12字符串转成日期对象

  • a: 字符串转成日期对象
    • a: 使用步骤
      • 1: 创建simpledateformat的对象
        • 构造方法中,指定日期模式
      • 2: 子类对象,调用方法 parse 传递string,返回date
        • 注意: 时间和日期的模式yyyy-mm-dd, 必须和字符串中的时间日期匹配

13calendar类_1

  • a: calendar类_1
    • a: 日历类(抽象类)
      • java.util.calendar
    • b: 创建对象
      • calendar类写了静态方法 getinstance() 直接返回了子类的对象
      • 不需要直接new子类的对象,通过静态方法直接获取

14calendar类_2

* a: calendar类_2
    * a: 成员方法
        * gettime() 把日历对象,转成date日期对象
        * get(日历字段) 获取指定日历字段的值
    * b: 代码演示
        calendar c = calendar.getinstance();
        // 获取年份
        int year = c.get(calendar.year);
        // 获取月份
        int month = c.get(calendar.month) + 1;
        // 获取天数
        int day = c.get(calendar.day_of_month);
        system.out.println(year + "年" + month + "月" + day + "日");
        
        

15calendar类_3

* a: calendar类_3
    * a: 成员方法
        * set(int field,int value)  设置指定的时间
    * b: 代码演示
        /*
         * calendar类的set方法 设置日历 set(int field,int value) field 设置的是哪个日历字段 value
         * 设置后的具体数值
         * 
         * set(int year,int month,int day) 传递3个整数的年,月,日
         */
        public static void function_1() {
            calendar c = calendar.getinstance();
            // 设置,月份,设置到10月分
            // c.set(calendar.month, 9);
    
            // 设置年,月,日
            c.set(2099, 4, 1);
    
            // 获取年份
            int year = c.get(calendar.year);
            // 获取月份
            int month = c.get(calendar.month) + 1;
            // 获取天数
            int day = c.get(calendar.day_of_month);
            system.out.println(year + "年" + month + "月" + day + "日");
        }
    

16calendar类_4

* a: calendar类_4
    * a: 成员方法
        * add(int field, int value) 进行整数的偏移
        * int get(int field) 获取指定字段的值
    * b: 案例演示
        /*
         * calendar类方法add 日历的偏移量,
         * 可以指定一个日历中的字段,
         * 进行整数的偏移 add(int field, int value)
         */
        public static void function_2() {
            calendar c = calendar.getinstance();
            // 让日历中的天数,向后偏移280天
            c.add(calendar.day_of_month, -280);
            // 获取年份
            int year = c.get(calendar.year);
            // 获取月份
            int month = c.get(calendar.month) + 1;
            // 获取天数
            int day = c.get(calendar.day_of_month);
            system.out.println(year + "年" + month + "月" + day + "日");
        }

17日期练习_活了多少天

* a: 日期练习_活了多少天
    * a: 案例代码
        /*
         *  计算活了多少天
         *   生日  今天的日期
         *   两个日期变成毫秒值,减法
         */
        public static void function() throws exception {
            system.out.println("请输入出生日期 格式 yyyy-mm-dd");
            //获取出生日期,键盘输入
            string birthdaystring = new scanner(system.in).next();
            //将字符串日期,转成date对象
            //创建simpledateformat对象,写日期模式
            simpledateformat sdf = new simpledateformat("yyyy-mm-dd");
            //调用方法parse,字符串转成日期对象
            date birthdaydate = sdf.parse(birthdaystring);
            
            //获取今天的日期对象
            date todaydate = new date();
            
            //将两个日期转成毫秒值,date类的方法gettime
            long birthdaysecond = birthdaydate.gettime();
            long todaysecond = todaydate.gettime();
            long secone = todaysecond-birthdaysecond;
            
            if(secone < 0){
                system.out.println("还没出生呢");
            }
            else{
            system.out.println(secone/1000/60/60/24);
            }
            
        }

18日期练习_闰年计算

* a: 日期练习_闰年计算
    * a: 案例代码
        /*
         *  闰年计算
         *  2000 3000
         *  高级的算法: 日历设置到指定年份的3月1日,add向前偏移1天,获取天数,29闰年
         */
        public static void function_1(){
            calendar c = calendar.getinstance();
            //将日历,设置到指定年的3月1日
            c.set(2088, 2, 1);
            //日历add方法,向前偏移1天
            c.add(calendar.day_of_month, -1);
            //get方法获取天数
            int day = c.get(calendar.day_of_month);
            system.out.println(day);
        }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网