Java基础(上)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Java入门基础

1. 强制类型转换

package com.itheima.demo01;


public class HelloWorld {
    public static void main(String[] args) {
        short s = 10;
        // 错误 s + 33是int所以s提升成int导致把int赋值到short上面报错。
//        s = s + 3;
        // 正确强制类型转换
        s = (short)(s + 3);
        // 正确+=包含了强制类型转换相当于上面
        s += 3;
    }
}

2. 输入输出

package com.itheima.demo01;
import java.util.Scanner;


public class HelloWorld {
    public static void main(String[] args) {

        System.out.println("请录入您的年龄");
        Scanner sc = new Scanner(System.in);
        int age = sc.nextInt();
        System.out.println("age: " + age);
    }
}


3. 三目运算符

package com.itheima.demo01;
import java.util.Scanner;


public class HelloWorld {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("第一个身高");
        int h1 = sc.nextInt();
        System.out.println("第一个身高");
        int h2 = sc.nextInt();
        System.out.println("第一个身高");
        int h3 = sc.nextInt();

        int temp = h1 < h2 ? h2 : h1;
        int max = temp < h3 ? h3 : temp;
        System.out.println("max: " + max);
    }
}

4. if else语句

package com.itheima.demo01;


public class HelloWorld {
    public static void main(String[] args) {
        int time = 10;
        if (time > 0 && time <=8){
            System.out.println("早上好");
        } else if(time > 8 && time <= 17){
            System.out.println("下午好");
        } else {
            System.out.println("晚上好");
        }

    }
}

5. for循环

package com.itheima.demo01;


public class HelloWorld {
    public static void main(String[] args) {
        for (int i=1; i <= 5; i++){
            System.out.println(i);
        }
    }
}

6. while循环

package com.itheima.demo01;


public class HelloWorld {
    public static void main(String[] args) {

        int i = 0;
        while(i<5){
            System.out.println("hello world");
            i++;
        }
    }
}

7. 生成随机数

package com.itheima.demo01;


public class HelloWorld {
    public static void main(String[] args) {
        int num;
        for (int i = 1; i <= 5; i++){
            // Math.random();可以获取0.0 ~ 1.0之间所有的数据, 包左不包右.
            num = (int)(Math.random()*10+1);
            System.out.println(num);
        }

    }
}

8. 数组的定义方法

package com.itheima.demo01;


public class HelloWorld {
    public static void main(String[] args) {

        int[] arr1 = new int[3];
        int arr2[] = new int[3];

        int[] arr3 = new int[]{11, 22, 33};
        int[] arr4 = {11, 22, 33};

    }
}

9. 数组的基本用法

package com.itheima.demo01;


public class HelloWorld {
    public static void main(String[] args) {
        int[] arr1 = {11, 22, 33};
        System.out.println(arr1[1]);
        arr1[1] = 222;
        System.out.println(arr1[1]);
        // 222

        int[] arr = new int[5];
        // 初始化未赋值默认为0
        System.out.println(arr[2]);
        // 5
        System.out.println(arr.length);
    }
}

10. 数组易错地方 越界或访问空指针

package com.itheima.demo01;


public class HelloWorld {
    public static void main(String[] args) {
        int[] arr = {11, 22};
//        System.out.println(arr[2]);
        arr = null;
        System.out.println(arr[0]);

    }
}

11. 遍历数组获取最大值

package com.itheima.demo01;


public class HelloWorld {
    public static void main(String[] args) {
        int[] arr = {5, 15, 2000, 10000, 100, 4000};
        int max = arr[0];
        for (int i = 0; i < arr.length; i++){
            if (max < arr[i]){
                max = arr[i];
            }
        }
        System.out.println(max);
    }
}

12. 类方法定义

package com.itheima.demo03_method;


/*
    案例: 定义方法, 遍历数组.
 */
public class Demo02 {
    public static void main(String[] args){
        int[] arr = {11, 22, 33, 44, 55};
        arr = null;
        printArray(arr);
        System.out.println("-----------------");

        int[] arr2 = {3, 2, 1};
        printArray(arr2);
    }



    public static void printArray(int[] arr){
        if (arr==null || arr.length==0){
            System.out.println("[]");
            return;
        }
        for (int i=0; i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }

}

13. 类属性的定义

13.1 定义Phone类

package com.itheima.demo04_oop;

public class Phone {
    String brand;

    private double price;

    String color;

    public void setPrice(double price){
        if (price > 0)
            this.price = price;
    }

    public double getPrice(){
        return price;
    }

    public void call(String name){
        System.out.println(price);
        System.out.println("给" + name + "打电话");
    }

    public void sendMessage(String name){
        System.out.println("给" + name + "发信息");
    }


}

13.2 调用类

package com.itheima.demo04_oop;

import javax.swing.plaf.synth.SynthLookAndFeel;

public class Demo01 {
    public static void main(String[] args){
        Phone p = new Phone();
        // null
        System.out.println(p.brand);
        // 0.0
        System.out.println(p.getPrice());
        System.out.println(p.color);
        System.out.println("---------------");

        //3. 给属性赋值, 并重新打印.
        p.brand = "华为P50";
        //p.price = 999.9;
        //p.setPrice(-999.9);
        p.setPrice(3000);
        p.color = "黑色";


        System.out.println(p.brand); //华为P50
        //System.out.println(p.price); //999.9

        System.out.println(p.getPrice()); //999.9

        System.out.println(p.color); //黑色
        System.out.println("---------------");

        //4. 调用类中的成员方法.
        p.call("刘亦菲");
        p.sendMessage("高圆圆");

    }
}

14. 继承

package com.itheima.demo07_extends;

public class Father {
    int age = 30;
}

package com.itheima.demo07_extends;

public class Son extends Father {

}

package com.itheima.demo07_extends;


public class FatherTest {
    public static void main(String[] args){
        Son s = new Son();
        System.out.println(s.age);
    }

}

15. 类方法重写

package com.itheima.demo07_extends;

public class Animal {
    private String name;
    private int age;
    // 没有修饰符默认是public
    int num = 30;

    public Animal(){
    }

    public Animal(String name, int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    public int getAge(){return age;}

    public void setAge(int age){this.age=age;}

    public void eat(){
        System.out.println("动物会吃");
    }

}

package com.itheima.demo07_extends;

public class Cat extends Animal {

    public Cat(){
        super();
    }

    int num = 20;

    public void show(){
        int num = 10;
        System.out.println(num);
        System.out.println(this.num);
        System.out.println(super.num);
    }
    
    @Override
    public void eat() {
        System.out.println("猫吃鱼!");
    }

}

package com.itheima.demo07_extends;

public class Demo1 {
    public static void main(String[] args){
        Cat c = new Cat();
        //2. 给属性赋值, 并重新打印.
        c.setName("加菲猫");
        c.setAge(13);
        System.out.println(c.getName() + ", " + c.getAge());
        c.eat();

        c.show();
        System.out.println("----------------");

    }
}

16. 多态

package com.itheima.demo08_multi;


//动物类
public class Animal /*extends Object*/ {
    int age = 30;

    public void eat() {
        System.out.println("动物会吃!");
    }
}

package com.itheima.demo08_multi;

public class Cat extends Animal{
    int age = 20;

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }

    public void catchMouse(){
        System.out.println("捉老鼠");
    }

}

package com.itheima.demo08_multi;

public class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("狗吃肉!");
    }
}

package com.itheima.demo08_multi;

/*
* 多态
* */
public class Demo1 {
    public static void main(String[] args){
        //多态写法, 猫是动物。 向上转型
        Animal an = new Cat();
        // 30
        System.out.println(an.age);
        // 猫吃鱼
        an.eat();
        System.out.println("---------------");

        //多态弊端: 父类引用不能直接使用子类的特有成员
        //an.catchMouse();    //报错

        // 向下转型
        Cat c = (Cat) an;
        c.catchMouse();
        // 20
        System.out.println(c.age);
        c.eat();

    }

}

package com.itheima.demo08_multi;

public class Demo2 {
    public static void main(String[] args){
        Cat c = new Cat();
        printAnimal(c);

        Dog d = new Dog();
        printAnimal(d);


    }

    public static void printAnimal(Animal an){
        an.eat();
    }


}

17. 静态属性和final关键词

package com.itheima.demo09_final_static;

public class Student {
    String name;
    static String graduateFrom = "黑马";

    public static void show(){
        // 报错 public static void show 静态方法不能获取动态属性
//        System.out.println(name);
        // 正确可以获取静态成员
        System.out.println(graduateFrom);
    }
}

package com.itheima.demo09_final_static;

public class Demo1 {
    public static void main(String[] args){
        final int MAX_VALUE = 30;
        System.out.println(MAX_VALUE);
    }


}

package com.itheima.demo09_final_static;

public class Demo2 {
    public static void main(String[] args){
        Student.graduateFrom = "北影";

        Student s1 = new Student();
        Student s2 = new Student();
        // 北影
        System.out.println(s1.graduateFrom);

        s1.graduateFrom = "传智播客";
        // 传智播客
        System.out.println(s1.graduateFrom);
        // 传智播客
        System.out.println(s2.graduateFrom);

    }
}

18. 抽象类

package com.itheima.demo10_abstract;

public abstract class Animal {
    public abstract void eat();

}

package com.itheima.demo10_abstract;

public class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

package com.itheima.demo10_abstract;

public abstract class Dog extends Animal{
}

package com.itheima.demo10_abstract;

public class Demo1 {
    public static void main(String[] args){
        Animal an = new Cat();
        an.eat();
        // 报错 抽象类不能被实例化
//        Animal an2 = new Dog();
    }

}

19. 接口比抽象类更加抽象

package com.itheima.demo11_interface;

public interface Flying {
    public static final int age = 30;

    public abstract void eat();

}

package com.itheima.demo11_interface;

public class Cat implements Flying{

    @Override
    public void eat() {

    }
}

package com.itheima.demo11_interface;


/*
    案例: 演示接口的用法.

    接口:
        概述:
            比抽象类更加抽象, 里边有且只能有抽象方法和常量.
        特点:
            1. 接口用 interface 修饰.
            2. 类和接口之间是实现关系, 用 implements 修饰.
            3. 接口不能实例化.
            4. 接口的子类:
                如果是普通类: 必须重写父类中所有的抽象方法.
                如果是抽象类: 可以不用重写.

    类和接口的关系:
        类与类:
            继承关系, 只能单继承, 不能多继承, 但是可以多层继承.
        类与接口:
            实现关系, 可以单实现, 也可以多实现, 还可以在继承一个类的同时实现多个接口.
        接口与接口:
            继承关系, 可以单继承, 也可以多继承.

 */

public class Demo01 {
    public static void main(String[] args){
        System.out.println(Flying.age);
        // 报错 接口不能被实例化
//        Flying f = new Flying();

        // 接口多态
        Flying f = new Cat();

    }


}

20. 重写类的打印方法以及比较方法

package com.demo02_object;

public class Student {
    private String name;
    private int age;

    public Student(){

    }

    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //快捷键重写Object#toString()
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object obj) {
        if (this==obj) return true;

        if (obj==null || getClass()!=obj.getClass()) return false;

        Student student =  (Student) obj;
        if (age != student.age) return false;
        return name != null ? name.equals(student.name):student.name==null;
    }

}

package com.demo02_object;

public class Demo01 {
    public static void main(String[] args){

        Student s = new Student("刘亦菲", 33);
        System.out.println(s.toString());
        System.out.println(s);
        System.out.println("-------------------");

        Student s1 = new Student("高圆圆", 35);
        Student s2 = new Student("高圆圆", 35);
        boolean flag = s1.equals(s2);
        System.out.println(flag);

    }
}

21. 字符串比较和反转

package com.demo03_string;


/*
    案例: 演示String类的比较功能.

    String类简介:
        概述:
            它是java.lang包下的类, 可以直接使用, 无需导包, 它表示所有的字符串, 每一个字符串都是它的对象.
            String s1 = new String("abc");
            String s1 = "abc";      //免new, 语法糖.
        成员方法:
            public boolean equals(Object obj);
                这个是String重写了Object#equals()方法, 比较字符串的内容是否相同, 区分大小写.
            public boolean equalsIgnoreCase(String s)
                 这个是String独有的方法, 比较字符串的内容是否相同, 不区分大小写.

        需求:
            定义字符数组chs, 初始化值为: 'a, 'b', 'c', 这三个字符 .
            将其分别封装成s1, s2这两个字符串对象.
            直接通过""的方式创建两个字符串对象s3和s4.
            通过==分别判断s1和s2, s1和s3, s3和s4是否相同.
            通过equals()分别判断s1和s2, s1和s3, s3和s4是否相同.
            通过equalsIgnoreCase()判断字符串abc和ABC是否相同.

        细节(记忆):
            1. ==的作用
                比较基本类型: 比较的是 数值.        10 == 20    比较数值.
                比较引用类型: 比较的是 地址值.      s1 == s2    比较地址值.
            2. String#equals()区分大小写, equalsIgnoreCase()忽略大小写.
 */

public class Demo01 {
    public static void main(String[] args){
        char[] chs = {'a', 'b', 'c'};
        String s1 = new String(chs);
        String s2 = new String(chs);

        String s3 = "abc";
        String s4 = "abc";

        //通过==分别判断s1和s2, s1和s3, s3和s4是否相同.
        System.out.println(s1 == s2);   //false, 比较的是 地址值.
        System.out.println(s1 == s3);   //false, 比较的是 地址值.
        System.out.println(s3 == s4);   //true, 比较的是 地址值.
        System.out.println("----------------");

        System.out.println(s1.equals(s2)); //true, 比较的是 内容.
        System.out.println(s1.equals(s3)); //true, 比较的是 内容.
        System.out.println(s3.equals(s4)); //true, 比较的是 内容.
        System.out.println("----------------");


        //通过equalsIgnoreCase()判断字符串abc和ABC是否相同.
        System.out.println("abc".equals("ABC"));    //false, 区分大小写.
        System.out.println("abc".equalsIgnoreCase("ABC")); //true, 忽略大小写.

    }


}

package com.demo03_string;
import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入一个字符串: ");
        String s = sc.nextLine();
        System.out.println("反转前: " + s);

        StringBuilder newStr = new StringBuilder(s);
        newStr.reverse();
        System.out.println(newStr);

//        String newStr = reverse(s);
//        System.out.println("反转后: " + newStr);

    }

    public static String reverse(String s){
        String newStr = "";
        char[] chs = s.toCharArray();
        for (int i = chs.length - 1; i >= 0; i--){
            newStr += chs[i];
        }
        return newStr;

    }


}

22. 字符串遍历

package com.demo04_exercise;
import java.util.Scanner;
import java.util.Arrays;

public class Demo02 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入一个字符串: ");
        String s = sc.nextLine();

        for (int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            System.out.println(ch);
        }
        System.out.println("-------------------");

        char[] chs = s.toCharArray();
        for (int i = 0; i < chs.length; i++){
            char ch = chs[i];
            System.out.println(ch);
        }
        System.out.println("-------------------");

        System.out.println(Arrays.toString(s.toCharArray()));

    }

}

23. 数组排序

package com.demo04_exercise;
import java.util.Arrays;

public class Demo03 {
    public static void main(String[] args){
        String s = "91 27 45 38 50";
        String[] arrStr = s.split(" ");

        int[] arr = new int[arrStr.length];
        for (int i = 0; i < arrStr.length; i++){
            String str = arrStr[i];
            arr[i] = Integer.parseInt(str);
        }

        Arrays.sort(arr);
        String s2 = Arrays.toString(arr);
        String result = s2.substring(1, s2.length() - 1);
        System.out.println(result);

    }
}

24. StringBuilder

24.1 创建StringBuilder

package com.demo05_stringbuilder;

public class Demo01 {
    public static void main(String[] args){
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = sb1.append("abc");
        StringBuilder sb3 = sb1.append("123");

        System.out.println("sb1: " + sb1);
        System.out.println("sb2: " + sb2);  //abc123
        System.out.println("sb3: " + sb3);  //abc123
        System.out.println("-----------------------");
        System.out.println(sb1 == sb2); //true, 比较地址值.
        System.out.println(sb1 == sb3); //true, 比较地址值.
        System.out.println(sb2 == sb3); //true, 比较地址值.
        System.out.println("-----------------------");

        //方式2: 带参, 本质: 把String -> StringBuilder对象.
        StringBuilder sb4 = new StringBuilder("我爱黑马!");
        System.out.println("sb4: " + sb4);
    }
}

24.2 StringBuilder和String的相互转换

package com.demo05_stringbuilder;

public class Demo02 {
    public static void main(String[] args){
        String s1 = "abc";
        StringBuilder sb1 = new StringBuilder(s1);
        System.out.println("sb1: " + sb1);
        System.out.println("-----------------");

        String str = sb1.toString();
        System.out.println("str: " + str);
    }

}

24.3 反转字符串

package com.demo05_stringbuilder;
import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入一个字符串: ");
        String s = sc.nextLine();       //abc -> cba
        System.out.println("反转前: " + s);

        //方式1: 入门班.
        //1. 把 String -> StringBuilder.
        StringBuilder sb = new StringBuilder(s);
        //2. 调用StringBuilder#reverse()反转内容.
        sb.reverse();
        //3. 把StringBuilder -> String.
        String newStr = sb.toString();
        //4. 打印结果.
        System.out.println("反转后: " + newStr);
        System.out.println("-------------------------");

        String result = new StringBuilder(s).reverse().toString();
        System.out.println("反转后: " + result);
        System.out.println("-------------------------");
    }
}

25. Arrays

package com.demo06_arrays;
import java.util.Arrays;

public class Demo01 {

    public static void main(String[] args){
        int[] arr = {25, 69, 80, 57, 13};
        Arrays.sort(arr);

        String s = Arrays.toString(arr);
        System.out.println("排序后: " + s);

    }
}

26. Integer

26.1 字符串转Integer

package com.demo07_integer;

public class Demo02 {
    public static void main(String[] args){
        //演示: int -> String:
        String s = "" + 10;
        System.out.println("s: " + s);
        System.out.println("----------------");

        //String -> Int:
        String str = "1314521";
        int num = Integer.parseInt(str);
        System.out.println("num: " + num);
    }

}

26.2 装箱和拆箱

package com.demo07_integer;

/*
    案例: 演示自动拆装箱(JDK1.5的特性)

    装箱: int -> Integer, 即: 把基本类型 -> 包装类.
    拆箱: Integer -> Int, 即: 包装类 -> 基本类型.
 */
public class Demo03 {
    public static void main(String[] args) {
        // 装箱
        Integer i1 = new Integer(10);
        Integer i2 = 10;
        System.out.println("-------------------");

        // 拆箱
        int a = i2.intValue();
        int b = i2;
        System.out.println("-------------------");

        //观察如下代码, 发生了什么.
        Integer i3 = 10;
        i3 += 20;       //等价于 i3 = i3 + 20,  先拆箱, 后计算, 再装箱
    }
}

27. 日期时间

27.1 获取当前时间

package com.demo08_date;
import java.util.Date;

public class Demo01 {
    public static void main(String[] args){
        Date d = new Date();
        System.out.println(d); // Wed Apr 26 16:50:28 CST 2023
        System.out.println(d.getTime()); //1682499028354
        System.out.println("--------------------");

        Date d2 = new Date(1631180490798L);
        //d2.setTime(1631180490798L);
        System.out.println(d2); //Thu Sep 09 17:41:30 CST 2021
        System.out.println("--------------------");

        //获取当前时间毫秒值, 实际开发做法.
        long time = System.currentTimeMillis();
        System.out.println(time); //1682499028397
    }
}

27.2 Date和String互转

package com.demo08_date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Demo02 {
    public static void main(String[] args) throws ParseException {
        //演示: Date -> String:     格式化.
        Date d = new Date();
        System.out.println(d);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String result = sdf.format(d);
        System.out.println(result); // 2023/04/26 16:56:10
        System.out.println("-----------------------");

        String s = "2021年09月09日 17:50:02";
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date d2 = sdf2.parse(s);
        System.out.println(d2); //Thu Sep 09 17:50:02 CST 2021
    }
}
package com.demo08_date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo03 {

    public static void main(String[] args) throws ParseException {
        Date d = new Date();

        String s1 = DateUtils.date2String(d, "yyyy/MM/dd HH:mm:ss");
        System.out.println(s1);
        System.out.println("-----------------------");

        String s = "2021年09月09日 17:50:02";
        Date d2 = DateUtils.string2Date(s, "yyyy年MM月dd日 HH:mm:ss");
        System.out.println(d2);

    }
}

27.3 Calendar对象

package com.demo08_date;
import java.util.Calendar;

public class Demo04 {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        System.out.println(c.get(Calendar.YEAR));
        System.out.println(c.get(Calendar.MONTH));  //8, Java中月份范围: 0 ~ 11
        System.out.println(c.get(Calendar.DATE));   //9
        System.out.println(c.get(Calendar.DAY_OF_MONTH));   //9
        System.out.println(c.get(Calendar.DAY_OF_YEAR));    //252
        System.out.println("-----------------------------");

    }
}

28. 异常处理

package com.demo09_exception;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
    案例: 演示异常.

    异常简介:
        概述:
            Java中, 把所有的非正常情况统称为异常.
        分类:
            顶层类: Throwable
                Error:      错误, 大多数和代码无关, 不需要你处理, 绝大多数你也处理不了.
                    例如: 服务器宕机, 数据库崩溃.
                Exception:  这个才是我们常说的异常.
                    运行时异常: 指的是 RuntimeException及其子类.
                    编译期异常: 指的是 非RuntimeException及其子类.
        编译期异常和运行时异常的区别是什么?
            编译期异常: 必须处理, 才能通过编译.
            运行时异常: 不处理, 也能通过编译, 但是运行报错.

        JVM默认是如何处理异常的?
            1. 会把异常的类型, 原因, 位置打印到控制台上.
            2. 并终止程序的执行.

        异常的处理方式:
            方式1: 声明抛出异常, throws
                大白话翻译: 告诉调用这里, 我这里有问题, 谁调用, 谁处理.
                处理之后, 程序终止运行, JVM默认用的就是这个.

            方式2: 捕获异常, 处理之后, 程序继续执行.
                try {
                    可能出现问题的代码;
                } catch(Exception e) {
                    e.printStackTrace();  //打印异常的类型, 位置, 原因
                } finally {
                    释放资源的, 正常情况下, 里边的代码永远会执行.
                }

 */


public class Demo01 {
    public static void main(String[] args){
        //运行时异常
//        System.out.println(1 / 0);      //ArithmeticException(算术异常)
//        System.out.println("看看我执行了吗?");

//        编译期异常
//        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//        String s = "2021年09月09日 17:50:02";
//        Date d2 = sdf2.parse(s);

//        String s = "2021年09月09日 17:50:02";
//        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//        // java: 未报告的异常错误java.text.ParseException; 必须对其进行捕获或声明以便抛出
//        Date d2 = sdf2.parse(s);
//        System.out.println(d2); //Thu Sep 09 17:50:02 CST 2021

        try{
            show();
        } catch (Exception e){
            e.printStackTrace();
        } finally {

        }

    }


    public static void show() throws ParseException {
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        //SimpleDateFormat#parse()方法抛出异常, 交给调用者处理.
        String s1 = "2021年09月09日 17:50:02";
        String s2 = "2021年09月09日";
        Date d1 = sdf2.parse(s2);
        System.out.println(d1);
    }


}

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java