Java基础之正则表达式与常见对象

​###14.01_常见对象(正则表达式的概述和简单使用)
* A:正则表达式
检验String字符串的是不是满足一定的规则,这个规则就是正则表达式。
* 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。
* 作用:比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个限制长度的事情就是正则表达式做的
作用:专门用于操作字符串
特点:用一些特定的符号来表示一些代码的操作.这样就简化书写.
所以学正则表达式,就是学习一些特殊符号的使用.
好处:可以简化对字符串的操作
弊端:符号定义越多,正则越长,阅读性越差
* B:案例演示
* 需求:校验qq号码.
* 1:要求必须是5-15位数字
* 2:0不能开头
* 3:必须都是数字

* a:非正则表达式实现
* b:正则表达式实现

    String regex = "[1-9]\\d{4,14}";
    System.out.println("2553868".matches(regex));
}

14.02_常见对象(字符类演示)

  • A:字符类(在java.util.regex包中的Pattern类中)

    • [abc] a、b 或 c(简单类)
      [^abc] 任何字符,除了 a、b 或 c(否定)
      [a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
      [a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
      [a-z&&[def]] d、e 或 f(交集)
      [a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
      [a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)

      String regex = “[a-z&&[^m-p]]”;//a-z,除了m-p
      System.out.println(“m”.matches(regex));
      }

14.03_常见对象(预定义字符类演示)

  • A:预定义字符类
    /**

    • . 任何字符
      \d 数字:[0-9]
      \D 非数字: [^0-9]
      \s 空白字符:[ \t\n\x0B\f\r] //空格,\t:制表符,\n:换行,\x0B:垂直制表符,\f:翻页,\r:回车
      \S 非空白字符:[^\s]
      \w 单词字符:[a-zA-Z_0-9]
      \W 非单词字符:[^\w]

      */

      System.out.println(“\”);//要打印出,并需再加一个\进行转义
      String regex = “\W”;
      System.out.println(“a”.matches(regex));

14.04_常见对象(数量词)

/**
 * Greedy 数量词 
    X? X,一次或一次也没有 
    X* X,零次或多次 
    X+ X,一次或多次 
    X{n} X,恰好 n 次 
    X{n,} X,至少 n 次 
    X{n,m} X,至少 n 次,但是不超过 m 次 
 */

    String regex = "[abc]{5,15}";
    System.out.println("abcba".matches(regex));

14.05_常见对象(正则表达式的分割功能)

  • A:正则表达式的分割功能
    • String类的功能:public String[] split(String regex)
  • B:案例演示

    • 正则表达式的分割功能
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      public static void main(String[] args) {
      //演示时先按照空格切割
      //再按点(.)切割,但是点(.)在正则表达式中代表任意字符,它具有特殊的含义
      //所以需要将点进行转义前面加,但是要想表示出一个,需要在前面再加一个,
      //这样就成了\.
      String s = “金三胖.郭美美.李dayone”;
      String[] arr = s.split(“\.”); //通过正则表达式切割字符串

    for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
    }
    }

14.06_常见对象(把给定字符串中的数字排序)

  • A:案例演示

    • 需求:我有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      /**
    • 分析:
    • 1,将字符串切割成字符串数组
    • 2,将字符串转换成数字并将其存储在一个等长度的int数组中
    • 3,排序
    • 4,将排序后的结果遍历并拼接成一个字符串
      */
      public static void main(String[] args) {
      String s = “91 27 46 38 50”;
      //1,将字符串切割成字符串数组
      String[] sArr = s.split(” “);
      //2,将字符串转换成数字并将其存储在一个等长度的int数组中
      int[] arr = new int[sArr.length];
      for (int i = 0; i < arr.length; i++) {
      arr[i] = Integer.parseInt(sArr[i]); //将数字字符串转换成数字
      }

    //3,排序
    Arrays.sort(arr);

    //4,将排序后的结果遍历并拼接成一个字符串27 38 46 50 91
    /*String str = “”;
    for (int i = 0; i < arr.length; i++) {
    if(i == arr.length - 1) {
    str = str + arr[i]; //27 38 46 50 91
    }else {
    str = str + arr[i] + ” “; //27 38 46 50
    }
    }

    System.out.println(str);*/

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < arr.length; i++) {
    if(i == arr.length - 1) {
    sb.append(arr[i]);
    }else {
    sb.append(arr[i] + ” “);
    }
    }

    System.out.println(sb);
    }

14.07_常见对象(正则表达式的替换功能)

  • A:正则表达式的替换功能
    • String类的功能:public String replaceAll(String regex,String replacement)
  • B:案例演示
    • 正则表达式的替换功能
      案例:

1
2
3
4
5
6
7
public static void main(String[] args) {
String s = “wo111ai222heima”;
String regex = “\d”; //\d代表的是任意数字

String s2 = s.replaceAll(regex, "");
System.out.println(s2);

}

14.08_常见对象(正则表达式的分组功能)

  • A:正则表达式的分组功能

    • 捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 ((A)(B(C))) 中,存在四个这样的组:


      • 1 ((A)(B(C)))
        2 (A
        3 (B(C))
        4 (C)

      组零始终代表整个表达式。

  • B:案例演示
    a:切割
    需求:请按照叠词切割: “sdqqfgkkkhjppppkl”;
    b:替换
    需求:我我….我…我.要…要要…要学….学学..学.编..编编.编.程.程.程..程
    将字符串还原成:“我要学编程”。
    (.)\1+的含义:表示第二个位置和(.)一样,但是第二个位置出现了1次或多次
    第一个字符是任意字符,但是第二位和第一位相同,即第二位用第一位的结果,所以将第一位的点(.)用括号()括起来(将要重用的部分用括号括起来,叫组),每组都有一个编号(从第一组开始)
    (.)通过”\”形式反向引用前面的组,\1表示第一组的内容在\1重新出现,可以1次或多次

案例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.heima.regex;

public class Demo7_Regex {

public static void main(String[] args) {
    //demo1();
    demo2();       
    //demo3();
}

private static void demo3() {
    /*
     * 需求:我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程
            将字符串还原成:“我要学编程”。
     */
    String s = "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程";
    String s2 = s.replaceAll("\\.+", "");
    String s3 = s2.replaceAll("(.)\\1+", "$1"); //$1代表第一组中的内容
    System.out.println(s3);
}

public static void demo2() {
    //需求:请按照叠词切割: "sdqqfgkkkhjppppkl";
    String s = "sdqqfgkkkhjppppkl";
    //String regex = "(.)\\1";
    String regex = "(.)\\1+";                   //+代表第一组出现一次到多次
    String[] arr = s.split(regex);

    for (int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
    }
    System.out.println("---------------");
}

public static void demo1() {
    //叠词 快快乐乐,高高兴兴
    /*String regex = "(.)\\1(.)\\2";                    //\\1代表第一组又出现一次 \\2代表第二组又出现一次
    System.out.println("快快乐乐".matches(regex));
    System.out.println("快乐乐乐".matches(regex));
    System.out.println("高高兴兴".matches(regex));
    System.out.println("死啦死啦".matches(regex));*/

    //叠词 死啦死啦,高兴高兴
    String regex2 = "(..)\\1";//第一组有两个字符,然后让第一组再出现一次
    System.out.println("死啦死啦".matches(regex2));
    System.out.println("高兴高兴".matches(regex2));
    System.out.println("快快乐乐".matches(regex2));
}

}

14.09_常见对象(Pattern和Matcher的概述)

  • A:Pattern和Matcher的概述
  • B:模式和匹配器的典型调用顺序

    • 通过JDK提供的API,查看Pattern类的说明

    • 典型的调用顺序是

    • Pattern p = Pattern.compile(“a*b”);
    • Matcher m = p.matcher(“aaaaab”);
    • boolean b = m.matches();
      案例:

1
2
3
4
5
6
//”a*b”:表示a出现的是0次或多次,后面跟一个b
Pattern p = Pattern.compile(“a*b”); //获取到正则表达式
Matcher m = p.matcher(“aaaaab”); //获取匹配器
boolean b = m.matches(); //看是否能匹配,匹配就返回true
System.out.println(b);
System.out.println(“aaaaab”.matches(“a*b”)); //与上面的结果一样

14.10_常见对象(正则表达式的获取功能)

  • A:正则表达式的获取功能
    • Pattern和Matcher的结合使用
  • B:案例演示
    • 需求:把一个字符串中的手机号码获取出来
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      package com.heima.regex;

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

public class Demo8_Pattern {

public static void main(String[] args) {
    //demo1();
    //demo2();
    demo3();
}
//匹配查找邮箱
private static void demo3() {//[1][\\d&&[012678]][\\d]{9}
    String s = "我的邮箱是smhjx2006@163.com,我曾经用过hmsykt2015@sina.com.cn,我还用过hmsykt0902@qq.com";
    //String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";//较为精确的匹配。
    String regex = "\\w+@\\w+(\\.\\w+)+";
    Pattern p = Pattern.compile(regex);
    Matcher matcher = p.matcher(s);
    while(matcher.find()){
        System.out.println(matcher.group());           
    }
}
//匹配查找手机号
private static void demo2() {
    String s = "我的手机是18511866260,我曾用过18987654321,还用过18812345678";
    String regex = "1[3578]\\d{9}";


    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(s);

    /*boolean b1 = m.find();
    System.out.println(b1);
    System.out.println(m.group());

    boolean b2 = m.find();
    System.out.println(b2);
    System.out.println(m.group());*/

    while(m.find())
        System.out.println(m.group());
}

}

14.11_常见对象(Math类概述和方法使用)

  • A:Math类概述
    • Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
  • B:成员方法
    • 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) min自学
    • public static double pow(double a,double b)
    • public static double random()
    • public static int round(float a) 参数为double的自学
    • public static double sqrt(double a)
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      package com.heima.otherclass;

public class Demo1_Math {
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.abs(-10)); //取绝对值

    //ceil天花板
    /*
     * 13.0
     * 12.3
     * 12.0
     */
    System.out.println(Math.ceil(12.3));            //向上取整,但是结果是一个double
    System.out.println(Math.ceil(12.99));

    System.out.println("-----------");
    //floor地板
    /*
     * 13.0
     * 12.3
     * 12.0
     */
    System.out.println(Math.floor(12.3));           //向下取整,但是结果是一个double
    System.out.println(Math.floor(12.99));

    //获取两个值中的最大值
    System.out.println(Math.max(20, 30));

    //前面的数是底数,后面的数是指数
    System.out.println(Math.pow(2, 3));             //2.0 ^ 3.0

    //生成0.0到1.0之间的所以小数,包括0.0,不包括1.0
    System.out.println(Math.random());

    //四舍五入
    System.out.println(Math.round(12.3f));
    System.out.println(Math.round(12.9f));

    //开平方
    System.out.println(Math.sqrt(4));
    System.out.println(Math.sqrt(2));
    System.out.println(Math.sqrt(3));
}

}

14.12_常见对象(Random类的概述和方法使用)

  • A:Random类的概述
    • 此类用于产生随机数如果用相同的种子创建两个 Random 实例,
    • 则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
  • B:构造方法
    • public Random()
    • public Random(long seed)
  • C:成员方法
    • public int nextInt()
    • public int nextInt(int n)(重点掌握)
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      package com.heima.otherclass;

import java.util.Random;

public class Demo2_Random {

public static void main(String[] args) {
    Random r = new Random();
    /*int x = r.nextInt();

    System.out.println(x);*/

    for(int i = 0; i < 10; i++) {
        //System.out.println(r.nextInt());
        System.out.println(r.nextInt(100));         //要求掌握,生成在0到n范围内的随机数,包含0不包含n
    }

    /*
     * -1244746321
        1060493871

        -1244746321
        1060493871

     */
    /*Random r2 = new Random(1001);

    int a = r2.nextInt();
    int b = r2.nextInt();

    System.out.println(a);
    System.out.println(b);*/
}

}

14.13_常见对象(System类的概述和方法使用)

  • A:System类的概述
    • System 类包含一些有用的类字段和方法。它不能被实例化。
  • B:成员方法
    • public static void gc()
    • public static void exit(int status)
    • public static long currentTimeMillis()
    • pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
  • C:案例演示
    • System类的成员方法使用
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      package com.heima.otherclass;

import java.util.Arrays;

public class Demo3_System {

public static void main(String[] args) {
    //demo1();
    //demo2();
    //demo3();

    demo4();
}

private static void demo4() {
    int[] src = {11,22,33,44,55};
    int[] dest = new int[8];
    for (int i = 0; i < dest.length; i++) {
        System.out.println(dest[i]);
    }

    System.out.println("--------------------------");
    System.arraycopy(src, 0, dest, 0, src.length);      //将数组内容拷贝

    for (int i = 0; i < dest.length; i++) {
        System.out.println(dest[i]);
    }
    System.out.println("=========================");
    int[] newDest = Arrays.copyOf(src, dest.length);
    for (int i = 0; i < newDest.length; i++) {
        System.out.println(dest[i]);
    }
}

public static void demo3() {
    long start = System.currentTimeMillis();        //1秒等于1000毫秒
    for(int i = 0; i < 1000; i++) {
        System.out.println("*");
    }
    long end = System.currentTimeMillis();          //获取当前时间的毫秒值

    System.out.println(end - start);
}

public static void demo2() {
    System.exit(1);                         //非0状态是异常终止,退出jvm
    //因为在上面已经退出了java虚拟机,所以下面的语句不会执行
    System.out.println("11111111111");
}

public static void demo1() {
    //产生垃圾的for循环
    for(int i = 0; i < 100; i++) {
        new Demo();
        System.gc();        //运行垃圾回收器,相当于呼喊保洁阿姨,但是不一厅呼喊一次就执行,有可能需要呼喊多次才执行
    }
}

}

class Demo { //在一个源文件中不允许定义两个用public修饰的类

@Override
public void finalize() {
    System.out.println("垃圾被清扫了");
}                          

}

14.14_常见对象(BigInteger类的概述和方法使用)

  • A:BigInteger的概述
    • 可以让超过Integer范围内的数据进行运算
  • B:构造方法
    • public BigInteger(String val)
  • C:成员方法
    • public BigInteger add(BigInteger val) //+(加)
    • public BigInteger subtract(BigInteger val)//-(减)
    • public BigInteger multiply(BigInteger val)//*(乘)
    • public BigInteger divide(BigInteger val)// /(除)
    • public BigInteger[] divideAndRemainder(BigInteger val)
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      package com.heima.otherclass;

import java.math.BigInteger;

public class Demo4_BigInteger {

public static void main(String[] args) {
    //long num = 123456789098765432123L;
    //String s = "123456789098765432123";

    BigInteger bi1 = new BigInteger("100");
    BigInteger bi2 = new BigInteger("2");

    System.out.println(bi1.add(bi2));               //+
    System.out.println(bi1.subtract(bi2));          //-
    System.out.println(bi1.multiply(bi2));          //*
    System.out.println(bi1.divide(bi2));            ///(除)

    BigInteger[] arr = bi1.divideAndRemainder(bi2); //取除数和余数

    for (int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
    }
}

}

14.15_常见对象(BigDecimal类的概述和方法使用)

  • A:BigDecimal的概述

    • 由于在运算的时候,float类型和double很容易丢失精度,演示案例。
    • 所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal

    • 不可变的、任意精度的有符号十进制数。

  • B:构造方法
    • public BigDecimal(String val)
  • C:成员方法
    • public BigDecimal add(BigDecimal augend)
    • public BigDecimal subtract(BigDecimal subtrahend)
    • public BigDecimal multiply(BigDecimal multiplicand)
    • public BigDecimal divide(BigDecimal divisor)
  • D:案例演示
    • BigDecimal类的构造方法和成员方法使用
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      package com.heima.otherclass;

import java.math.BigDecimal;

public class Demo5_BigDecimal {

/**
    十进制表示1/3
    0.3333333333333333333333333333333333333333
         */
public static void main(String[] args) {
    //System.out.println(2.0 - 1.1);
    /*BigDecimal bd1 = new BigDecimal(2.0);     //这种方式在开发中不推荐,因为不够精确
    BigDecimal bd2 = new BigDecimal(1.1);

    System.out.println(bd1.subtract(bd2));*/

    /*BigDecimal bd1 = new BigDecimal("2.0");       //通过构造中传入字符串的方式,开发时推荐
    BigDecimal bd2 = new BigDecimal("1.1");

    System.out.println(bd1.subtract(bd2));*/

    BigDecimal bd1 = BigDecimal.valueOf(2.0);   //这种方式在开发中也是推荐的
    BigDecimal bd2 = BigDecimal.valueOf(1.1);

    System.out.println(bd1.subtract(bd2));
}

}

14.16_常见对象(Date类的概述和方法使用)(掌握)

  • A:Date类的概述(是java.util包下的,别导错了)
    • 类 Date 表示特定的瞬间,精确到毫秒。
  • B:构造方法
    • public Date()
    • public Date(long date)
  • C:成员方法
    • public long getTime()
    • public void setTime(long time)
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      package com.heima.otherclass;

import java.util.Date;

public class Demo6_Date {

public static void main(String[] args) {
    //demo1();
    //demo2();
    Date d1 = new Date();  
    d1.setTime(1000);                               //设置毫秒值,改变时间对象
    System.out.println(d1);
}

public static void demo2() {
    Date d1 = new Date();  
    System.out.println(d1.getTime());               //通过时间对象获取毫秒值
    System.out.println(System.currentTimeMillis()); //通过系统类的方法获取当前时间毫秒值
}

public static void demo1() {
    Date d1 = new Date();                   //如果没有传参数代表的是当前时间
    System.out.println(d1);

    Date d2 = new Date(0);                  //如果构造方法中参数传为0代表的是1970年1月1日
    System.out.println(d2);                 //通过毫秒值创建时间对象
}

}

14.17_常见对象(SimpleDateFormat类实现日期和字符串的相互转换)(掌握)

  • A:DateFormat类的概述
    • DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat
  • B:SimpleDateFormat构造方法
    • public SimpleDateFormat()
    • public SimpleDateFormat(String pattern)
  • C:成员方法
    • public final String format(Date date)
    • public Date parse(String source)
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      package com.heima.otherclass;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo7_SimpleDateFormat {

public static void main(String[] args) throws ParseException {
    //demo1();
    //demo2();
    //demo3();

    demo4();
}

private static void demo4() throws ParseException {
    //将时间字符串转换成日期对象
    String str = "2000年08月08日 08:08:08";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    Date d = sdf.parse(str);                        //将时间字符串转换成日期对象
    System.out.println(d);
}

public static void demo3() {
    Date d = new Date();                            //获取当前时间对象
    //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM年-dd日 HH:mm:ss");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//创建日期格式化类对象
    System.out.println(sdf.format(d));              //将日期对象转换为字符串
}

public static void demo2() {
    Date d = new Date();                            //获取当前时间对象
    SimpleDateFormat sdf = new SimpleDateFormat();  //创建日期格式化类对象
    System.out.println(sdf.format(d));              //88-6-6 下午9:31
}

public static void demo1() {
    //DateFormat df = new DateFormat();             //DateFormat是抽象类,不允许实例化
    //DateFormat df1 = new SimpleDateFormat();
    DateFormat df1 = DateFormat.getDateInstance();  //相当于父类引用指向子类对象,右边的方法返回一个子类对象
}

}

14.18_常见对象(你来到这个世界多少天案例)(掌握)

  • A:案例演示
    • 需求:算一下你来到这个世界多少天?
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      package com.heima.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test2 {

/**
 * * A:案例演示
 * 需求:算一下你来到这个世界多少天?
 * 分析:
 * 1,将生日字符串和今天字符串存在String类型的变量中
 * 2,定义日期格式化对象
 * 3,将日期字符串转换成日期对象
 * 4,通过日期对象后期时间毫秒值
 * 5,将两个时间毫秒值相减除以1000,再除以60,再除以60,再除以24得到天
 * @throws ParseException
 */
public static void main(String[] args) throws ParseException {
    //1,将生日字符串和今天字符串存在String类型的变量中
    String birthday = "1983年07月08日";
    String today = "2015年9月22日";
    //2,定义日期格式化对象
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
    //3,将日期字符串转换成日期对象
    Date d1 = sdf.parse(birthday);
    Date d2 = sdf.parse(today);
    //4,通过日期对象后期时间毫秒值
    long time = d2.getTime() - d1.getTime();
    //5,将两个时间毫秒值相减除以1000,再除以60,再除以60,再除以24得到天
    System.out.println(time / 1000 / 60 / 60 / 24 );
}

}

14.19_常见对象(Calendar类的概述和获取日期的方法)(掌握)

  • A:Calendar类的概述
    • Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
  • B:成员方法
    • public static Calendar getInstance()
    • public int get(int field)
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      package com.heima.otherclass;

import java.util.Calendar;

public class Demo9_Calendar {

public static void main(String[] args) {
    demo1();       
}

public static void demo1() {
    Calendar c = Calendar.getInstance();            //父类引用指向子类对象
    //System.out.println(c);
    System.out.println(c.get(Calendar.YEAR));       //通过字段获取年
    System.out.println(c.get(Calendar.MONTH));      //通过字段后期月,但是月是从0开始编号的
    System.out.println(c.get(Calendar.DAY_OF_MONTH));//月中的第几天
    System.out.println(c.get(Calendar.DAY_OF_WEEK));//周日是第一天,周六是最后一天

    System.out.println(c.get(Calendar.YEAR) + "年" + getNum((c.get(Calendar.MONTH)+1))
            + "月" + getNum(c.get(Calendar.DAY_OF_MONTH)) + "日" + getWeek(c.get(Calendar.DAY_OF_WEEK)));
}

/*
 * 将星期存储表中进行查表
 * 1,返回值类型String
 * 2,参数列表int week
 */

public static String getWeek(int week) {
    String[] arr = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

    return arr[week];
}

/*
 * 如果是个数数字前面补0
 * 1,返回值类型String类型
 * 2,参数列表,int num
 */
public static String getNum(int num) {
    /*if(num > 9) {
        return "" + num;
    }else {
        return "0" + num;
    }*/
    return num > 9 ? "" + num : "0" + num;
}

}

14.20_常见对象(Calendar类的add()和set()方法)(掌握)

  • A:成员方法
    • public void add(int field,int amount)
    • public final void set(int year,int month,int date)
  • B:案例演示
    • Calendar类的成员方法使用
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      package com.heima.otherclass;

import java.util.Calendar;

public class Demo9_Calendar {

public static void main(String[] args) {

    Calendar c = Calendar.getInstance();            //父类引用指向子类对象
    //c.add(Calendar.MONTH, -1);                    //对指定的字段进行向前减或向后加
    //c.set(Calendar.YEAR, 2000);                   //修改指定字段
    c.set(2000, 7, 8);

    System.out.println(c.get(Calendar.YEAR) + "年" + getNum((c.get(Calendar.MONTH)+1))
            + "月" + getNum(c.get(Calendar.DAY_OF_MONTH)) + "日" + getWeek(c.get(Calendar.DAY_OF_WEEK)));

}  

}

14.21_常见对象(如何获取任意年份是平年还是闰年)(掌握)

  • A:案例演示
    • 需求:键盘录入任意一个年份,判断该年是闰年还是平年
      案例:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      package com.heima.test;

import java.util.Calendar;
import java.util.Scanner;

public class Test3 {

/**
 * * A:案例演示
 * 需求:键盘录入任意一个年份,判断该年是闰年还是平年
 * Calendar c = Calendar.getInstance();
 *
 * 分析:
 * 1,键盘录入年Scanner
 * 2,创建Calendar c = Calendar.getInstance();
 * 3,通过set方法设置为那一年的3月1日
 * 4,将日向前减去1
 * 5,判断日是多少天,如果是29天返回true否则返回false
 */
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入年份,判断该年份是闰年还是平年:");
    //int year = sc.nextInt();

    String line = sc.nextLine();                //录入数字字符串
    int year = Integer.parseInt(line);          //将数字字符串转换成数字
    boolean b = getYear(year);                  //这里输入的是年份
    System.out.println(b);
}

private static boolean getYear(int year) {
    //2,创建Calendar c = Calendar.getInstance();
    Calendar c = Calendar.getInstance();
    //设置为那一年的3月1日
    c.set(year, 2, 1);
    //将日向前减去1
    c.add(Calendar.DAY_OF_MONTH, -1);
    //判断是否是29天
    return c.get(Calendar.DAY_OF_MONTH) == 29;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值