Java开发手册(高级)


前言

九天阊阖开宫殿,万国衣冠拜冕旒。   — 王维《和贾舍人早朝大明宫之作》

在这里插入图片描述

1.正则表达式

1.1 正则表达式概述

       正则表达式是一种规则,用来验证各种字符串的规则。

  • 例子:验证QQ号码,号码规则如下:

    • QQ号码必须是5–15位长度
    • 而且必须全部是数字
    • 而且首位不能为0
  • 代码示例:

package com.syh;

/**
 * 正则表达式验证qq号
 */
public class Esther {
    public static void main(String[] args) {
        String qq = "1234567890";
        System.out.println(qq.matches("[1-9]\\d{5,19}"));
    }
}

1.2 正则表达式语法

1.2.1 字符类

  • 语法示例:

       1. [abc]:代表a或者b,或者c字符中的一个。

       2. [^abc]:代表除a,b,c以外的任何字符。

       3. [a-z]:代表a-z的所有小写字符中的一个。

  • 代码示例:
package com.syh;

/**
 * 正则表达式字符匹配
 */
public class Esther {
    public static void main(String[] args) {
        // 1.[abc],只能是abc中任意一个字符
        System.out.println("-----------1-------------");
        System.out.println("a".matches("[abc]"));  // true
        System.out.println("z".matches("[abc]"));  // false

        // 2.[^abc],不能出现abc字符
        System.out.println("-----------2-------------");
        System.out.println("a".matches("[^abc]")); // false
        System.out.println("z".matches("[^abc]")); // true

        // 3.[a-z],a-z之间的任意一个字符
        System.out.println("-----------3-------------");
        System.out.println("a".matches("[a-z]"));  // true
        System.out.println("0".matches("[a-z]"));  //false
    }
}

1.2.2 逻辑运算符

  • 语法示例:

         1. &&:并且

         2. | :或者

  • 代码示例

package com.syh;

/**
 * 正则表达式逻辑运算符匹配
 */
public class Esther {
    public static void main(String[] args) {
        String str = "had";
        // 1.要求字符串是小写辅音字符开头,后跟ad
        String regex = "[a-z&&[^aeiou]]ad";
        System.out.println(str.matches(regex));

        // 2.要求字符串是aeiou中的某个字符开头,后跟ad
        regex = "[a|e|i|o|u]ad";
        System.out.println(str.matches(regex));
    }
}

1.2.3 预定义字符

  • 语法示例

         1. “.” : 匹配任意一个字符。

         2. “\d”:任何数字[0-9]的简写;

         3. “\D”:任何非数字[^0-9]的简写;

         4. “\w”:单词字符:[a-zA-Z_0-9]的简写

         5. “\W”:非单词字符:[^w]的简写

  • 代码示例:

package com.syh;

/**
 * 正则表达式预定义运算符匹配
 */
public class Esther {
    public static void main(String[] args) {
        // 1 "."表示任意一个字符
        System.out.println("a".matches("."));    //true
        System.out.println("ab".matches("."));   //false

        // 2 "\d" 表示任意的一个数字(代码中的\\相当于概念中的\)
        System.out.println("1".matches("\\d"));  //true
        System.out.println("11".matches("\\d")); //false

        // 3 "\D" 表示非"\d"
        System.out.println("a".matches("\\D"));  //true
        System.out.println("1".matches("\\D"));  //false

        // 4 "\w"只能是一位单词字符[a-zA-Z_0-9]
        System.out.println("a".matches("\\w"));  //true
        System.out.println("ab".matches("\\w")); //false

        // 5 "\W"表示非"\w"
        System.out.println("#".matches("\\W"));  //true
        System.out.println("a".matches("\\W"));  //false
    }
}

1.2.4 数量词

  • 语法示例:

         1. X? : 0次或1次

         2. X* : 0次到多次

         3. X+ : 1次或多次

         4. X{n} : 恰好n次

         5. X{n,} : 至少n次

         6. X{n,m}: n到m次(n和m都是包含的)

  • 代码示例:

package com.syh;

/**
 * 正则表达式数量词匹配
 */
public class Esther {
    public static void main(String[] args) {
        // 1."X?",匹配数字0次或1次
        System.out.println("".matches("[\\d]?"));      //true
        System.out.println("24".matches("[\\d]?"));    //false

        // 2."X*",匹配数字0次或多次
        System.out.println("".matches("[\\d]*"));      //true
        System.out.println("24x".matches("\\w{6,}"));  //false

        // 3."X+",匹配数字1次或多次
        System.out.println("24".matches("[\\d]+"));    //true
        System.out.println("".matches("[\\d]+"));      //false

        // 4."X{n}",匹配数字1次或多次
        System.out.println("123".matches("[\\d]{3}"));      //true
        System.out.println("".matches("[\\d]{3}"));         //false

        // 5."X{n,}",匹配数字至少n次
        System.out.println("123".matches("[\\d]{3,}"));     //true
        System.out.println("".matches("[\\d]{3,}"));        //false

        // 6."X{n,m}",匹配数字至少n次,至多m次
        System.out.println("123".matches("[\\d]{3,5}"));    //true
        System.out.println("123456".matches("[\\d]{3,5}")); //false
    }
}

1.3 爬取数据

       Pattern:表示正则表达式
       Matcher:文本匹配器,作用按照正则表达式的规则去读取字符串,从头开始读取。在大串中去找符合匹配规则的子串。

       代码示例:

package com.syh;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式爬取数据
 */
public class Esther {
    public static void main(String[] args) {
        String str = "Java1自从95年问世以来,经历了很多版本,目前企业中用的最多的是Java8和Java11," +
                "因为这两个是长期支持版本,下一个长期支持版本是Java17,相信在未来不久Java17也会逐渐登上历史舞台";
        
        // 1.正则表达式
        Pattern p = Pattern.compile("Java[\\d]*");
        // 2.文本匹配器,在str中差找符合p规则的子串
        Matcher m = p.matcher(str);
        // 3.利用循环获取
        while (m.find()) {
            String s = m.group();
            System.out.println(s);
        }
    }
}

2 Date类

2.1 Date概述

       Date类表示日期类,常见类如下:

JDK8时间类类名作用
Date年、月、日、时、分、秒、星期
LocalDate年、月、日
LocalTime时、分、秒、毫秒
LocalDateTime年、月、日、时、分、秒、毫秒

2.2 代码示例

package com.syh;

import java.time.*;
import java.util.Date;

public class Esther {
    public static void main(String[] args) {
        // 1.Date类
        Date date = new Date();
        System.out.println("现在的标准日期" + date);

        // 2.LocalDate类
        LocalDate nowDate = LocalDate.now();
        System.out.println("现在的日期:" + nowDate);

        // 3.LocalTime类
        LocalTime nowTime = LocalTime.now();
        System.out.println("现在的时间:" + nowTime);

        // 4.LocalDateTime类
        LocalDateTime nowDateTime = LocalDateTime.now();
        System.out.println("现在的日期和时间:" + nowDateTime);
    }
}

3 Calendar类

3.1 概述

Calendar类表示一个“日历类”,可以进行日期运算。它是一个抽象类,不能创建对象,我们可以使用它的子类:GregorianCalendar类。

  • 有两种方式可以获取GregorianCalendar对象:
    • 直接创建GregorianCalendar对象;
    • 通过Calendar的静态方法getInstance()方法获取GregorianCalendar对象

3.2 常用方法

方法名说明
public static Calendar getInstance()获取一个它的子类GregorianCalendar对象。
public int get(int field)Calendar.YEAR : 年
Calendar.MONTH :月
Calendar.DAY_OF_MONTH:月中的日期
Calendar.HOUR:小时
Calendar.MINUTE:分钟
Calendar.SECOND:秒
public void set(int field,int value)设置某个字段的值
public void add(int field,int amount)为某个字段增加/减少指定的值

3.3 get方法示例

package com.syh;

import java.util.Calendar;

public class Esther {
    public static void main(String[] args) {
        // 1.获取一个GregorianCalendar对象
        Calendar instance = Calendar.getInstance();

        // 2.获取属性
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;  //Calendar的月份值是0-11
        int day = instance.get(Calendar.DAY_OF_MONTH);

        int hour = instance.get(Calendar.HOUR);
        int minute = instance.get(Calendar.MINUTE);
        int second = instance.get(Calendar.SECOND);
        // 3.打印结果
        System.out.println(year + "年" + month + "月" + day + "日  " +
                hour + ":" + minute + ":" + second);
    }
}

3.4 set方法示例:

package com.syh;

import java.util.Calendar;

public class Esther {
    public static void main(String[] args) {
        // 1.获取一个GregorianCalendar对象
        Calendar c1 = Calendar.getInstance();   //获取当前日期
        // 2.设置属性
        c1.set(Calendar.YEAR, 1998);
        c1.set(Calendar.MONTH, 2);
        c1.set(Calendar.DAY_OF_MONTH, 18);
        // 3.打印结果
        System.out.print(c1.get(Calendar.YEAR)+"年");
        System.out.print(c1.get(Calendar.MONTH)+"月");
        System.out.print(c1.get(Calendar.DAY_OF_MONTH)+"日");
    }
}

3.5 add方法示例:

package com.syh;

import java.util.Calendar;

public class Esther {
    public static void main(String[] args) {
        // 1.获取一个GregorianCalendar对象
        Calendar c1 = Calendar.getInstance();
        // 2.调用add方法,日期加200天
        c1.add(Calendar.DAY_OF_MONTH, 200);
        // 3.获取属性
        int year = c1.get(Calendar.YEAR);
        int month = c1.get(Calendar.MONTH) + 1;
        int day = c1.get(Calendar.DAY_OF_MONTH);
        // 4.打印结果
        System.out.println("200天后是:" + year + "年" + month + "月" + day + "日");

    }
}

4 包装类

4.1 包装类概述

       Java数据类型分为基本类型与引用类型,包装类是把八种基本数据类型包装成引用数据类型。

基本类型对应的包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

       代码示例

package com.syh;


public class Esther {
    public static void main(String[] args) {
        // 1.Byte包装类
        Byte b1 = Byte.valueOf("10");
        System.out.println("Byte:" + b1);
        
        // 2.Short包装类
        Short s1 = Short.valueOf("10");
        System.out.println("Short:" + s1);
        
        // 3.Integer包装类
        Integer i1 = Integer.valueOf(10);
        System.out.println("Integer:" + i1);

        // 4.Long包装类
        Long l1 = Long.valueOf(10);
        System.out.println("Long:" + l1);

        // 5.Float包装类
        Float f1 = Float.valueOf(10.0f);
        System.out.println("Float:" + f1);
        
        // 6.Double包装类
        Double d1 = Double.valueOf(10.0);
        System.out.println("Double:" + d1);
        
        // 7.Character包装类
        Character c1 = Character.valueOf('a');
        System.out.println("Character:" + c1);
        
        // 8.Boolean包装类
        Boolean b2 = Boolean.valueOf(true);
        System.out.println("Boolean:" + b2);
    }
}

4.2 装箱与拆箱

       基本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“:

  • 装箱:从基本类型转换为对应的包装类对象。
  • 拆箱:从包装类对象转换为对应的基本类型。

       从Java 5(JDK 1.5)开始,基本类型与包装类的装箱、拆箱动作可以自动完成。

// 1.自动装箱
Integer i = 10;
// 2.自动拆箱
int b = i;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值