1. 程式人生 > >正則表示式、Pattern和Matcher類、Math類、Random類、System類、BigDecimal類、Date類、SimpleDateFormat類、Calendar類

正則表示式、Pattern和Matcher類、Math類、Random類、System類、BigDecimal類、Date類、SimpleDateFormat類、Calendar類

1.正則表示式

即給定的一個用來描述符合一定規則的字串的單個字串,是一種規則; 正則表示式的組成規則: 規則字元在java.util.regex Pattern類中 A:字元 x 字元 x。舉例:‘a’表示字元a \ 反斜線字元。 \n 新行(換行)符 (’\u000A’) \r 回車符 (’\u000D’) B:字元類 [abc] a、b 或 c(簡單類) [^abc] 任何字元,除了 a、b 或 c(否定) [a-zA-Z] a到 z 或 A到 Z,兩頭的字母包括在內(範圍) [0-9] 0到9的字元都包括 C:預定義字元類 . 任何字元。我的就是.字元本身,怎麼表示呢? . \d 數字:[0-9] \w 單詞字元:[a-zA-Z_0-9] 在正則表示式裡面組成單詞的東西必須有這些東西組成 D:邊界匹配器 ^ 行的開頭 $ 行的結尾 \b 單詞邊界 就是不是單詞字元的地方。 舉例:hello world?haha;xixi E:Greedy 數量詞 X? X,一次或一次也沒有 比如""空串 就是沒有 X* X,零次或多次 大於等於1次 都算多次 X+ X,一次或多次 X{n} X,恰好 n 次 X{n,} X,至少 n 次 X{n,m} X,至少 n 次,但是不超過 m 次

//    使用者名稱必須是6-16位之間的字母下劃線或者數字
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("請錄入使用者名稱:");
    String s = sc.next();
    String regx="\\w{6,16}";
    boolean b = s.matches(regx);
    if(b){
        System.out.println("格式正確");
    }else {
        System.out.println("格式錯誤!!!");
    }
    }
}
    //校驗QQ號碼(5-15位,0不能開頭)
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入QQ號碼:");
        String s = sc.next();
        String regx="[1-9][0-9]{4,14}";
        boolean b=s.matches(regx);
        if (b){
            System.out.println("QQ號碼格式正確!!!");
        }else{
            System.out.println("QQ號碼格式錯誤!!!");
        }
    }
}
    //給定字串中的數字排序
    //字串:”91 27 46 38 50”
    //最終輸出結果是:”27 38 46 50 91”
    public static void main(String[] args) {
        String s = new String();
        s="91 27 46 38 50";
        String[] s1=s.split(" ");
        int[] ints = new int[s1.length];
        for (int i = 0; i <s1.length ; i++) {
            ints[i]=Integer.parseInt(s1[i]);
        }
        Arrays.sort(ints);
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i <s1.length ; i++) {
            stringBuffer.append(ints[i]).append(" ");
        }
        System.out.println(stringBuffer.toString().trim());
    }
}

正則表示式的替換功能:

    public static void main(String[] args) {
        String s = new String();
        s = "511641awafawfgawfwada";
        //將數字替換為"*"
        String regx = "[0-9]";
        String regx1 = "*";
        String s1 = s.replaceAll(regx, regx1);
        System.out.println(s1);
    }
}

2.Pattern和Matcher類

Pattern 模式器 用來封裝正則表示式 Matcher 匹配器 封裝要匹配的資料, 能夠去匹配 等一系列操作

//    獲取下面這個字串中由三個字元組成的單詞 正則表示式 \\b[a-z]{3}\\b
//    da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
       public static void main(String[] args) {
           String s = new String();
           String regx="\\b[a-z]{3}\\b";
           s="da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
           Pattern pattern = Pattern.compile(regx);
           Matcher matcher = pattern.matcher(s);
           while (matcher.find()){
               String s1=matcher.group();
               System.out.println(s1);
           }
       }
}

3.Math類

Math 類包含用於執行基本數學運算的方法 成員變數

  •   public static final double E : 		自然底數
      public static final double PI:		圓周率
    

成員方法

  •   public static int abs(int a)		取絕對值
      public static double ceil(double a)	向上取整
      public static double floor(double a)	向下取整
      public static int max(int a,int b)      獲取最大值
      public static int min(int a, int b)	獲取最小值
      public static double pow(double a,double b) 獲取a的b次冪
      public static double random()	獲取隨機數 返回帶正號的 double 值,該值大於等於 0.0 且小於 1.0。
      public static int round(float a) 四捨五入
      public static double sqrt(double a)獲取正平方根
    
    public static void main(String[] args) {
        double pi = Math.PI;
        System.out.println(pi);
        double e = Math.E;
        System.out.println(e);
        int max = Math.max(3, 4);
        System.out.println(max);
    }
}

4.Random類

此類用於產生隨機數如果用相同的種子建立兩個 Random 例項,則對每個例項進行相同的方法呼叫序列,它們將生成並返回相同的數字序列。

    public static void main(String[] args) {
        Random random = new Random();
        for (int j = 0; j <100 ; j++) {
            int i = random.nextInt(100);
            System.out.print(i+" ");
        }
    }
}

5.System類

System 類包含一些有用的類欄位和方法。它不能被例項化。

            System.exit(0);//退出java虛擬機器 0 為正常退出 非0為 異常退出
            System.exit(1);
            System.currentTimeMillis();//獲取當前時間的毫秒值

6.BigDecimal類

Java提供了BigDecimal是為了能精確的表示和計算浮點數,保證運算的精度; 構造方法

  •   public BigDecimal(String val)
    

成員方法

  •   public BigDecimal add(BigDecimal augend)//加
      public BigDecimal subtract(BigDecimal subtrahend)//減
      public BigDecimal multiply(BigDecimal multiplicand)//乘
      public BigDecimal divide(BigDecimal divisor)//除法
      public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)//scale 小數點後面保留幾位
    
    public static void main(String[] args) {
        BigDecimal a = new BigDecimal(3);
        BigDecimal b = new BigDecimal(6);
        BigDecimal sum = a.add(b);
        System.out.println(sum);
        BigDecimal divide = b.divide(a, 10,1);
        System.out.println(divide);
    }
}

7.Date類

類 Date 表示特定的瞬間,精確到毫秒 構造方法

  •   public Date()
      public Date(long date)  //把一個long型別的毫秒值轉換成一個日期物件
    

成員方法

  •  public long getTime():	獲取一個日期物件物件毫秒值
     public void setTime(long time):	給一個日期物件設定上指定的毫秒值 
    
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date.toString());
        long time = date.getTime();
        System.out.println(time);
        Date date2 = new Date(1000*10*60);
        System.out.println(date2);
    }
}

*Date ---- long 的轉換 呼叫getTime方法

*long — Date 的轉換 可以使用構造方法 setTime(long time)

8.SimpleDateFormat類

SimpleDateFormat: 可以把一個日期物件格式化成一個文字(字串) , 也可以把一個日期字串解析成一個日期物件 構造方法:

  •  public SimpleDateFormat():使用預設的模式來建立一個SimpleDateFormat物件
    
  •  public SimpleDateFormat(String pattern):使用指定的模式(規則比如yyyy:MM:dd HH:mm:ss)來建立一個SimpleDateFormat物件
    

成員方法: *

  •  public String format(Date date): 	把一個日期物件格式化成一個字串
    
  •  public Date parse(String dateStr):	把一個日期字串解析成一個日期物件 注意要以指定格式解析
    
    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入一個日期(20000101)");
        String s = sc.next();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = simpleDateFormat.parse(s);
        System.out.println(date);
        long time = date.getTime();
        long l = System.currentTimeMillis();
        System.out.println(time+"秒");
        System.out.println(l+"秒");
        if ((l-time)<0){
            long t=-(l-time);
            System.out.println("與今天相距"+t/1000/60/60/24+"天");
        }else{
            System.out.println("與今天相距"+(l-time)/1000/60/60/24+"天");
        }
    }
}

9.Calendar類

Calendar 類是一個抽象類,不能直接new物件,可以通過他的一個靜態成員方法getInstance()來獲取他的物件它為特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日曆欄位之間的轉換提供了一些方法,併為操作日曆欄位(例如獲得下星期的日期)提供了一些方法。 成員方法 public void add(int field,int amount) 根據日曆的規則,為給定的日曆欄位新增或減去指定的時間量 public final void set(int year,int month,int date) 設定日曆時間 年月日 成員方法 public static Calendar getInstance() 使用預設時區和語言環境獲得一個日曆物件 public int get(int field) 獲得給定日曆欄位對應的值 field通過Calendar提供的欄位來拿

    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        instance.set(2018,10,30);
        System.out.println(instance.get(Calendar.YEAR));
        System.out.println(instance.get(Calendar.MARCH));
        System.out.println(instance.get(Calendar.DATE));
        instance.add(Calendar.YEAR,1);
        instance.add(Calendar.MARCH,1);
        instance.add(Calendar.DATE,1);
        System.out.println(instance.get(Calendar.YEAR));
        System.out.println(instance.get(Calendar.MARCH));
        System.out.println(instance.get(Calendar.DATE));

    }
}