【JAVADAY9、常见的8个类的使用】

三种注解

override

放在方法前面,表明此方法是一个重写的方法

deprecated

修饰类,表明该类已过时,修饰字段,字段过时,修饰方法,方法过时...

suppresswarning

抑制编译警告:写在你想要不提示警告信息的地方,里面的参数,如果你写all那么就是全部不提示警告。

JDK的元注解(了解即可)

在这里插入图片描述

课堂作业

static的应用

  • 如果你用static修饰变量,那么这个类会把这个变量的值保存起来。
    在这里插入图片描述
上图会输出9.0 red
100.0 red

static的应用2

在这里插入图片描述

package exmap;

@SuppressWarnings({"all"})
public class HomeWork02 {
    public static void main(String[] args) {
    //体会是不是每次都是加100
        System.out.println(Frock.getNextNum());
        System.out.println(Frock.getNextNum());
        Frock frock1 = new Frock();
        Frock frock2 = new Frock();
        Frock frock3 = new Frock();
        System.out.println(frock1.getSeriaiNumber());
        System.out.println(frock2.getSeriaiNumber());
        System.out.println(frock3.getSeriaiNumber());

    }
}
@SuppressWarnings({"all"})
class Frock{
    private static int cuttentNum=100000;//序号起始值
    int seriaiNumber;

    public int getSeriaiNumber() {
        return seriaiNumber;
    }

    public Frock() {
//创建对象就会调用该构造器,该构造器又会调用getNextNum方法,然后静态变量就会自加,然后静态变量的值就会被赋给变量seriaiNumber        
        seriaiNumber=getNextNum();
    }

    public static int getNextNum() {
        cuttentNum+=100;
        return cuttentNum;
    }
}

抽象类的应用

在这里插入图片描述

package exmap;

public class HomeWork03 {
    public static void main(String[] args) {
        Animal cat=new Cat();
        cat.shout();
        Animal dog=new Dog();
        dog.shout();
    }
}
abstract class Animal{
    //抽象方法没有方法体,后续类想要继承该类,必须重写该方法
    abstract public void shout();
    }
class Cat extends Animal{
    @Override
    public void shout() {
        System.out.println("猫会喵喵叫");
    }
}
class Dog extends Animal{
    @Override
    public void shout() {
        System.out.println("狗会汪汪叫");
    }
}

异常

空指针异常

![package Exception012.Arithmetic;

public class yichang1 {
    public static void main(String[] args) {
        String name="";
        System.out.println(name.length());
    }
}
报错:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "name" is null
	at Exception012.Arithmetic.yichang1.main(yichang1.java:6)

进程已结束,退出代码1

数学运算异常

package Exception012.Arithmetic;

import javax.swing.*;

public class Exception01 {
    public static void main(String[] args) {
        int num=10;
        int num2=0;
        try {
            int e=num/num2;
        } catch (Exception ex) {
            System.out.println("异常类型"+ex.getMessage());
        }
        //程序会报错,java提供了一个异常处理机制try catch
        //如果你认为e这里可能会出现错误就选中
        //使用CTRL+ALT+T
        //捕获之后,即使那里出现了异常
        //程序仍然会向下执行
        System.out.println("程序退出");
    }
}
结果:
异常类型/ by zero
程序退出

进程已结束,退出代码0

数组下标越界异常

package Exception012.Arithmetic;

public class yichang2 {
    public static void main(String[] args) {
        int arr[]={1,2,3};
        for (int i = 0; i < 4; i++) {
            System.out.println(arr[i]);
        }
    }
}
结果:
**Exception** in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
	at Exception012.Arithmetic.yichang2.main(yichang2.java:7)
1
2
3

进程已结束,退出代码1

类型转换异常

在这里插入图片描述

把b转c肯定是不行,因为b和c本身没有任何关系,会异常。

数字格式不正确异常

比如 string n=“同学”;
//把string转换成Int 显然是不行的 会抛出异常
    int a=Integer.parseInt(n);

编译异常

在这里插入图片描述
只要有finally 即使catch了异常但是程序仍然要把catch执行完毕。
在这里插入图片描述
虽然有retrun 3 但是它不会返回,因为程序还要执行finally所以最终返回4


在这里插入图片描述
没有return会临时创建变量保存3,等程序运行完在返回

用异常来迫使用户输入你想要用户输入的类型

在这里插入图片描述


1、内部类

Inner Class:内部类

一个类的内部再定义一个完整的类;

class waibulei{
class neibulei{

}

}
为什么要定义内部类?

例如身体和手,手是身体的一部分;
内部类也会生成class文件:外部类名&内部类名.class

特点:

编译之后可生成独立的字节码文件(class文件)
内部类可直接访问外部类的私有成员,而不破坏封装;
可为外部类提供必要的内部功能组件;

1.1、成员内部类

在类的内部定义,与实例变量、实例方法同级别的类
外部类的一个实例部分,创建内部类对象时,必须依赖外部类对象;
当外部类、内部类存在重名属性时,会优先访问内部类属性;
成员内部类不能定义静态成员,可以包含静态常量final;
//外部类
public class class01 {
private String name = “tom”;
private int age = 20;

//内部类
class Inner{
    private String name = "jeck";

    private String address = "西天";
    private String phone = "1234567890";

    //方法
    public void show(){

        System.out.println(class01.this.name);
        System.out.println(age);

        System.out.println(name);
        System.out.println(address);
        System.out.println(phone);
    }
}

}
public class Test01 {
public static void main(String[] args) {
//1、创建外部类对象
class01 class01 = new class01();
//2、创建内部类对象
com.feng.InnerClass.class01.Inner inner = class01.new Inner();

    //以上两步可一步到位
    com.feng.InnerClass.class01.Inner inner1 = new class01().new Inner();

    inner.show();
    inner1.show();
}

}

1.2、静态内部类 (static)

不依赖外部类对象,可直接创建或通过类名访问,可声明静态成员;(不需要先创建外部类)
可包含静态成员;(相当于一个外部类)
只能直接访问外部类的静态成员(实例成员需实例化外部类对象)
只有内部类才能用static修饰,外部类不行;
public class class02 {
private String name = “tom”;
private int age = 20;

//静态内部类,和外部类相同
static class Inner{
    private String address = "上海";
    private String phone = "123456";
    //静态成员
    private static int count = 1000;

    public void show(){
        //调用外部类的属性,不能直接调用外部类属性;
        //1、先创建外部类对象
        class02 class02 = new class02();
        //2、调用外部类的属性
        System.out.println(class02.name);
        System.out.println(class02.age);

        //调用静态内部类的属性和方法
        System.out.println(address);
        System.out.println(phone);
        //调用静态内部类的静态属性
        System.out.println(Inner.count);
    }
}

}
public class Test02 {
public static void main(String[] args) {
//直接创建静态内部类对象
class02.Inner inner = new class02.Inner();
//调用方法
inner.show();
}
}

1.3、局部内部类

把一个类定义在方法的内部,这个类就叫内部类

定义在外部类方法中,作用范围和创建对象范围仅限于当前方法;
局部内部类访问外部类当前方法中的局部变量时,因为无法保障变量的生命周期与自身相同,变量必须修饰为final;
限制类的使用范围;
不能定义静态变量;
public class class03 {
private String name = “tom”;
private int age = 20;

//如果show()方法变成static,访问外部类的属性要用class03.属性名;
public void show(){

    //定义局部变量
    String address = "北京";

    //局部内部类:注意不能加任何访问修饰符
    class Inner{
        private String phone = "123456";
        private String email = "1hjfui9034";

        public void show2(){
            //访问外部的属性
            System.out.println(name);
            System.out.println(age);
            //访问局部类变量,jdk1.7之前要加final,jdk1.8之后他默认一个final在前面
            System.out.println(address);

            System.out.println(phone);
            System.out.println(email);
        }
    }
    //创建局部内部类对象
    Inner inner = new Inner();
    inner.show2();
}

}
public class Test03 {
public static void main(String[] args) {
//要在show方法里创建局部内部类对象,不然没有返回任何结果,因为show()只是创建了一个值而已;
class03 class03 = new class03();
class03.show();

}

}

1.4、匿名内部类

没有名字的内部类

没有类名的局部内部类(一切特征都与局部内部类相同)
必须继承一个父类或者实现一个接口;
定义类、实现类、创建对象的语法合并,只能创建一个该类的对象;
优点:减少代码量

缺点:可读性较差

public interface Usb {
void service();//前面默认了一个public
}
public class Mouse implements Usb {
@Override
public void service() {
System.out.println(“连接电脑成功,鼠标开启工作…”);
}
}
public class TestUsb {
public static void main(String[] args) {
//创建接口类型的变量
// Usb usb = new Mouse();
// usb.service();

    //局部内部类

// class Fan implements Usb{
// @Override
// public void service() {
// System.out.println(“连接电脑成功,鼠标开启工作…”);
// }
// }
// //使用局部内部类创建对象
// Usb usb1 = new Fan();
// usb1.service();

    //使用匿名内部类优化(像上面这个类,只用了一次,感觉很浪费,所以有了这个)
    Usb usb2 = new Usb() {
        @Override
        public void service() {
            System.out.println("连接电脑成功,鼠标开启工作......");
        }
    };
    usb2.service();

}

}

2、Object类

所以类的父类,我们写的所有类,在特殊情况下都是默认继承了object类

超类、基类,所有类的直接或间接父类,位于继承树的最顶层;
任何类,如没有书写extends显示继承某个类,都默认直接继承object类,否则为间接继承;
object类中所定义的方法,是所有对象都具备的方法;
object类型可以存储任何对象
作为参考,可接受任何对象
作为返回值,可返回任何对象

3、Object类常用方法

1、getClass()方法

public final Class<> getClass(){}
//返回引用中存储的实际对象类型
//应用:通常用于判断两个引用中实际存储对象类型是否一致

2、hashCode()方法

public int hashCode(){}
//返回该对象的哈希码值
//哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值
//一般情况下相同对象返回相同哈希码

3、toString()方法

public String toString(){}
//返回该对象的字符串表示(表示形式)
//可以根据程序需求覆盖该方法,如:展示对象各个属性值

4、equals()方法

public Boolean equals(Object obj){}
//默认实现为(this == obj),比较两个对象地址是否相同
//可进行覆盖,比较两个对象的内容是否相同
//比较两个引用是否指向同一个对象
//判断obj是否为null
//判断两个引用指向的实际对象类型是否一致
//强制类型转换
//依次比较各个属性值是否相同

5、finalize()方法

//当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列;
//垃圾对象:没有有效引用指向此对象时,为垃圾对象;
//垃圾回收:有GC销毁垃圾对象,释放数据存储空间;
//自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象;
//手动回收机制:使用System.gc();通知JVM执行垃圾回收;

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;
    }

    //重写toString方法
    public String toString() {
        return name+":"+age;
    }

    //将equals进行覆盖,比较两个对象的内容是否相同
    @Override   //Override:覆盖
    public boolean equals(Object obj){
        //判断两个对象是否是同一个引用
        if (this == obj){
            return true;
        }
        //判断obj是否为null
        if (this == null){
            return false;
        }
        //判断对象类型是否一致
//        if (this.getClass() == obj.getClass()){
//            return true;
//        }
        //instanceof:判断对象是否是某种类型
        if (obj instanceof Student){
            //强制类型转换
            Student s = (Student) obj;
            //依次比较各个属性值是否相同
            if (this.name.equals(s.getName()) && this.age == s.getAge()){
                return true;
            }
        }

        return false;
    }


    @Override
    protected void finalize() throws Throwable {
        System.out.println(this.name+"对象被回收了!");
    }
}
public class TestStudent {
    public static void main(String[] args) {
        Student student1 = new Student("aa",19);
        Student student2 = new Student("bb",20);
```java
        //1、getClass()方法;判断student1和student2是不是同一个类型;使用getClass()方法
        Class class1 = student1.getClass();
        Class class2 = student2.getClass();

        if (class1 == class2){
            System.out.println("是同一个类型");
        }else{
            System.out.println("not是同一个类型");
        }


        //2、hashCode方法
        System.out.println(student1.hashCode());
        System.out.println(student2.hashCode());

        Student student3 = student1;
        System.out.println(student3.hashCode());


        //3、toString()方法;
        // 返回的数值是十进制的;这些数字对我们没有用,
        // 所以我们需要重写tostring方法来让他输出的是姓名年龄
        //按住Ctrl点击toString方法进入查看,然后在我们的类中重写
        //或者直接在类中Alt+ins重写toString方法
        System.out.println(student1.toString());
        System.out.println(student2.toString());


        //equals()方法,判断两个值是否相同
        System.out.println(student1.equals(student2));
        Student s1 = new Student("cc",22);
        Student s2 = new Student("cc",22);
        System.out.println(s1.equals(s2));

    }
}
public class TestStudent2 {
    public static void main(String[] args) {
//        Student student1= new Student("aaa",20);
//        Student student2 = new Student("bbb",20);
//        Student student3 = new Student("ccc",20);
//        Student student4 = new Student("ddd",20);
//        Student student5 = new Student("eee",20);
```java
        new Student("aaa",20);
        new Student("bbb",20);
        new Student("ccc",20);
        new Student("ddd",20);
        new Student("eee",20);

        System.gc();
        System.out.println("垃圾回收");
    }
}
## 4、包装类
packing:包装

八大基本类型,每个类型都对应一个包装类(byteshortintlongfloatdoublecharBoolean)



基本数据类型所对应的引用数据类型;
Object可统一所有数据,包装类的默认值是null;
装箱和拆箱

```java
public class Demo01 {
    public static void main(String[] args) {
        //转换类型:装箱,基本类型转换成引用类型的过程
        //基本类型
        int num1 = 20;
        //使用Integer类创建对象
        Integer integer1 = new Integer(num1);
        //valueOf() 方法用于返回给定参数的原生 Number 对象值,参数可以是原生数据类          型, String等。
		//该方法是静态方法。该方法可以接收两个参数一个是字符串,一个是基数。
        Integer integer2 = Integer.valueOf(num1);
        System.out.println("装箱");
        System.out.println(integer1);
        System.out.println(integer2);

        //类型转换:拆箱,引用类型转换成基本类型
        Integer integer3 = new Integer(1000);
        //intValue:此方法的意思是:输出int数据。
        int num2 = integer3.intValue();
        System.out.println("拆箱");
        System.out.println(num2);

        //以上是jdk1.5之前的方法

        //jdk1.5之后,提供自动装箱和拆箱
        int age = 20;
        //自动装箱
        Integer integer4 = age;
        System.out.println("自动装箱");
        System.out.println(integer4);
        //自动拆箱
        int age2 = integer4;
        System.out.println("自动拆箱");
        System.out.println(age2);


        System.out.println("====================================");

        //基本类型和字符串之间转换
        //基本类型转换成字符串
        int n1 = 15;
        //1.1使用+号
        String s1 = n1 + "";
        //1.2使用Integer中的toString()方法
        String s2 = Integer.toString(n1,16);//16是表示十六进制

        System.out.println(s1);
        System.out.println(s2);

        //字符串转换成基本类型
        String str = "200";
        //使用Integer.parseXXX()方法
        int n2 = Integer.parseInt(str);
        System.out.println(n2);


        //Boolean字符串形式转换成基本类型,“true”----》true  “true”---》false
        String str2 = "false";
        boolean b1 = Boolean.parseBoolean(str2);
        System.out.println(b1);

    }
}
整数缓冲区
java预先创建了256个常用的整数包装类型对象;
在实际应用当中,对已创建的对象进行服用;
public class Demo02 {
    public static void main(String[] args) {

        //面试题
        Integer integer1 = new Integer(100);
        Integer integer2 = new Integer(100);
        //结果是false;integer1和2是放在栈空间中的,而100是放在堆空间中的,
        // 他比较的是栈中值,因为栈中1和2的地址不同(两个地址),所以1 != 2;
        System.out.println(integer1 == integer2);


        Integer integer3 = 100;//自动装箱:integer.valueOf()
        Integer integer4 = 100;
        System.out.println(integer3 == integer4);//true

        Integer integer5 = 200;//自动装箱:integer.valueOf()大于等于127就装箱为new Integer()类型
        Integer integer6 = 200;
        System.out.println(integer5 == integer6);//false

    }
}
## 5String类
平时使用最高的类
创建字符串方法
//字符串是常量,创建之后不可以改变
//字符串字面值存储在字符串池中,可以共享
String s = "hello";//产生一个对象,字符串池中存储
String s = new String("hello");//产生两个对象,堆、池各存储一个


```java
public class Demo01 {
    public static void main(String[] args) {
        String name = "hello";//hello常量存储在字符池中
        name = "zhangsan";//zhangsan赋值给name变量,给字符串赋值时,并没有修改数据,而是重新开劈一个空间(不可变性)
        String name2 = "zhangsan";//在字符串池中寻找有没有zhangsan这个值,有的话,直接赋给name2,这时name和name2栈空间地址相同(字符串共享)
       //字符串的另一种创建方式
        String str = new String("java");
        String str2 = new String("java");
        System.out.println(str == str2);//false
        System.out.println(str.equals(str2));//true
    }
}

String常用方法

public class Demo01 {
    public static void main(String[] args) {
        String name = "hello";//hello常量存储在字符池中
        name = "zhangsan";//zhangsan赋值给name变量,给字符串赋值时,并没有修改数据,而是重新开劈一个空间(不可变性)
        String name2 = "zhangsan";//在字符串池中寻找有没有zhangsan这个值,有的话,直接赋给name2,这时name和name2栈空间地址相同(字符串共享)
        //字符串的另一种创建方式
        String str = new String("java");
        String str2 = new String("java");
        System.out.println(str == str2);//false
        System.out.println(str.equals(str2));//true

        System.out.println("=============================");


        //字符串方法的使用
        //1、length();返回字符串的长度
        //2、charAt(int index);返回某个位置的字段
        //3、contains(String str);判断是否包含某个字符串
        String content = "java是世界上最好的编程语言java";
        System.out.println(content.length());
        System.out.println(content.charAt(content.length()-1));
        System.out.println(content.contains("java"));
        System.out.println(content.contains("php"));

        System.out.println("=============================");

        //4、toCharArray();返回字符串对应的数组
        //5、indexOf();返回字符串首次出现的位置
        //6、lastIndexOf();返回字符串最后一次出现的位置
        System.out.println(content.toCharArray());
        System.out.println(Arrays.toString(content.toCharArray()));//数组编程字符串
        System.out.println(content.indexOf("语言"));
        System.out.println(content.indexOf("java",14));//从下标14开始找
        System.out.println(content.lastIndexOf("java"));

        System.out.println("=========================");

        //7、trim();去掉字符串前后的空格
        //8、toUpperCase();把小写转换成大小;toLowerCase();把大写转换成小写
        //9、endWith(str);判断是否已str结尾,starWith(str);判断是否已str开头
        String content2 = "    Hello World    ";
        String content3 = "Hello World";
        System.out.println(content2);
        System.out.println(content2.trim());
        System.out.println(content2.toUpperCase());
        System.out.println(content2.toLowerCase());
        System.out.println(content3.endsWith("World"));
        System.out.println(content3.startsWith("Hello"));

        System.out.println("=================================");

        //10、replace(char old,char new);用新的字符或字符串替换旧的字符或字符串
        //11、split();对字符串进行拆分
        System.out.println(content.replace("java","php"));
        System.out.println(content.replace("java","easy java"));//在java面前插入一个easy

        String say = "java is teh best    programing language,java";
//        String[] arr = say.split(" ");//表示遇到空格进行拆分
        //String[] arr = say.split("[ ,]");//表示空格和逗号
        String[] arr = say.split("[ ,]+");//表示空格和逗号可以出现多个
        System.out.println(arr.length);
        for (String string : arr){
            System.out.println(string);
        }


        //补充两个方法equals、compareTo();比较大小
        System.out.println("=======================");
        String s1 = "hello";
        String s2 = "HELLO";
        System.out.println(s1.equalsIgnoreCase(s2));//equalsIgnoreCase:忽略大小写比较大小

        String s3 = "abc";//a:97
        String s4 = "xyz";//x:120
        System.out.println(s3.compareTo(s4));//-23

        String s5 = "abc";//abc:3个
        String s6 = "abcxyz";//abcxyz:6个
        System.out.println(s5.compareTo(s6));//-3
    }
}
public class Demo02 {
    public static void main(String[] args) {
        String str = "this is a text";
        //将str中的单词单独获取出来
        String[] arr = str.split(" ");
        for (String s : arr){
            System.out.println(s);
        }
        //将str中间的text替换成java;replace:代替
        System.out.println("==========================");
        String str2 = str.replace("text","java");
        System.out.println(str2);
        //在text前面插入easy
        System.out.println("=========================");
        String str3 = str.replace("text","easy text");
        System.out.println(str3);
        //将每个单词的首字母改为大写
        System.out.println("==========================");
        for (int i = 0; i < arr.length; i++) {
            char first = arr[i].charAt(0);
            //把第一个字符转换成大写
            char upperfirst = Character.toUpperCase(first);

            String news = upperfirst + arr[i].substring(1);
            System.out.print(news+" ");
        }
    }
}
StringBufferStringBuilder
public class Demo03 {
    public static void main(String[] args) {

        //StringBuffer和StringBuilder的使用,
        //和String的区别(1)效率比String高(2)比String节省内存
        //StringBuild功能和StringBuffer一样,将下面这个StringBuffer换成StringBuild就行

        StringBuffer sb = new StringBuffer();

        //1、append();追加
        sb.append("java是最好的编程语言");
        System.out.println(sb.toString());
        sb.append("java真好");
        System.out.println(sb.toString());
        sb.append("hello world");
        System.out.println(sb.toString());
        System.out.println("============================");

        //2、inset();添加
        sb.insert(0,"aaa");
        System.out.println(sb.toString());
        System.out.println("============================");

        //3、replace();替换
        sb.replace(0,5,"javascript");
        System.out.println(sb.toString());
        System.out.println("============================");

        //4、delete();删除
        sb.delete(0,5);
        System.out.println(sb.toString());
        System.out.println("============================");

        //5、清空
        sb.delete(0,sb.length());
        System.out.println(sb.toString());
    }
}
public class Demo04 {
    public static void main(String[] args) {
        //验证StringBuilder效率高于String

        //开始时间
        long start = System.currentTimeMillis();

        //String
//        String string = "";
//        for (int i = 0; i < 99999; i++) {          //用时:46881
//            string += i;
//        }
//        System.out.println(string);

        //StringBuilder
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 99999; i++) {             //用时:100
            sb.append(i);
        }
        System.out.println(sb.toString());

        long end = System.currentTimeMillis();
        System.out.println("用时:"+(end-start));
    }
}
6BigDecimal类
需求精度比较高是使用的类





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

        double b1 = 1.0;
        double b2 = 0.9;
        System.out.println(b1-b2);

        System.out.println("=======================");

        //面试题
        double result = (1.4-0.5)/0.9;
        System.out.println(result);

        System.out.println("======================");

//        BigDecimal,大的浮点数精确计算
        BigDecimal db1 = new BigDecimal("1.0");
        BigDecimal db2 = new BigDecimal("0.9");
        //减法
        BigDecimal r1 = db1.subtract(db2);
        System.out.println(r1);
        //加法
        BigDecimal r2 = db1.add(db2);
        System.out.println(r2);
        //乘法
        BigDecimal r3 = db1.multiply(db2);
        System.out.println(r3);
        //除法
        BigDecimal r4 = new BigDecimal("1.4")
                .subtract(new BigDecimal("0.5"))
                .divide(new BigDecimal("0.9"));
        System.out.println(r4);

        System.out.println("========================");

        //当数除不尽时可能会报错
//        BigDecimal r5 = new BigDecimal("10").divide(new BigDecimal(3));
//        System.out.println(r5);
        BigDecimal r5 = new BigDecimal("10")
                .divide(new BigDecimal(3),2,BigDecimal.ROUND_HALF_UP);//保留2位小数,ROUND_HALF_UP四舍五入
        System.out.println(r5);

    }
}
补充类
Datapublic class Demo01 {
    public static void main(String[] args) {
        //创建date对象
        //今天
        Date date1 = new Date();
        System.out.println(date1.toString());
        System.out.println(date1.toLocaleString());
        System.out.println("=======================");

        //昨天
        Date date2 = new Date(date1.getTime()-(60*60*24*1000));
        System.out.println(date2.toLocaleString());

        //方法after before
        boolean b1 = date1.after(date2);
        System.out.println(b1);
        boolean b2 = date1.before(date2);
        System.out.println(b2);

        System.out.println("===========================");

        //比较compareTo();
        int d = date2.compareTo(date1);
        System.out.println(d);

        //比较是否相等equals
        boolean b3 = date1.equals(date2);
        System.out.println(b3);
    }
}
Calendarpublic class Demo02 {
    public static void main(String[] args) {
        //创建Calendar对象
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getTime().toLocaleString());
        System.out.println(calendar.getTimeInMillis());
        System.out.println("===============================");

        //获取时间信息
        //获取年
        int year = calendar.get(Calendar.YEAR);
        //月
        int month = calendar.get(Calendar.MONTH);//MONTH是0~11,0代表1月
        //日
        int day = calendar.get(Calendar.DAY_OF_MONTH);//Date
        //小时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);//HOUR是12小时的;HOUR_OF_DAY是24小时的
        //分
        int minute = calendar.get(Calendar.MINUTE);
        //秒
        int second = calendar.get(Calendar.SECOND);

        System.out.println(year+"年"+(month+1)+"月"+day+"日"+"\n"+hour+":"+minute+":"+second);

        System.out.println("=====================");

        //修改时间
        Calendar calendar2 = calendar.getInstance();
        calendar2.set(Calendar.DAY_OF_MONTH,5);
        System.out.println(calendar2.getTime().toLocaleString());

        //add方法修改时间
        calendar2.add(Calendar.HOUR,1);//加一个小时,减少时间就-
        System.out.println(calendar2.getTime().toLocaleString());

        System.out.println("====================================");

        //补充方法
        int max = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
        int min = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH);
        System.out.println(max);
        System.out.println(min);

    }
}
SimpleDateFormatpublic class Demo03 {
    public static void main(String[] args) throws Exception {
        //创建SimpleDateFormat对象 y年M月
        SimpleDateFormat simpleDateFormat = new  SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        //创建Date
        Date date = new Date();
        //格式化date(把日期转成字符串)
        String str = simpleDateFormat.format(date);
        System.out.println(str);
        System.out.println("=============================");

        //解析(把字符串转成日期)
        Date date2 = simpleDateFormat.parse("1949/10/01 10:01:00");//这里要按照simpleDateFormat那里的格式
        System.out.println(date2);

    }
}
Systempublic class Demo04 {
    public static void main(String[] args) {

        //1、arraycopy:数组的复制
        //src:源数组
        //srcPos:从那个位置开始复制0
        //dest:目标数组
        //destPos:目标数组的位置
        //length:复制的长度

        int[] arr = {20,18,4,9,39,64,100,98};
        int[] dest = new int[8];
        System.arraycopy(arr,4,dest,0,4); //length的值只能小于等于srcPos的值;destPos从哪个位置开始存数据
        for (int i = 0; i < dest.length; i++) {
            System.out.println(dest[i]);
        }
        //Arrays.copyOf(original,newLength);
        System.out.println("===========================");


        //currentTimeMillis:可以计算程序运行的时间
        System.out.println(System.currentTimeMillis());
        //2、获取毫秒数
        Long start = System.currentTimeMillis();
        for (int i = 0; i < 999999; i++) {
            for (int j=0;j<99999;j++){
                int re = i+j;
            }
        }
        Long end = System.currentTimeMillis();
        System.out.println("用时:"+(start-end));

        System.out.println("=========================");


        //类正在被使用,不回收
//        Demo05 s1 = new Demo05("aaa",20);
//        Demo05 s2 = new Demo05("bbb",20);
//        Demo05 s3 = new Demo05("ccc",20);

        //类没使用,被回收了
        new Demo05("aaa",20);
        new Demo05("bbb",20);
        new Demo05("ccc",20);

        //3、System.gc();告诉垃圾回收
        System.gc();

        System.out.println("===========================");

        //4、推出JVM
        System.exit(0);
        //下面就不会被执行了
        System.out.println("程序结束了......");

    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Keyle777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值