1. 程式人生 > >16_常用API_第16天(正則表達式、Date、DateFormat、Calendar)

16_常用API_第16天(正則表達式、Date、DateFormat、Calendar)

調用方法 pat 簡單 throw test 邊界 sdf 當前 所有

今日內容介紹
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();
            }
            /*
             *  檢查郵件地址是否合法
             *  規則:
             *   [email protected]
             *   [email protected]
             *   [email protected]
             *   [email protected]    
             *   
             *   @: 前  數字字母_ 個數不能少於1個
             *   @: 後  數字字母     個數不能少於1個
             *   .: 後面 字母 
             *     
             */
            public static void checkMail(){
                String email ="[email protected]";
                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);
        }

16_常用API_第16天(正則表達式、Date、DateFormat、Calendar)