JAVA study notes 05-API (2)

API

1. Object class
equals method: compare whether the address values ​​of two objects are equal

public class demo01 {
    
    
    public static void main(String[] args) {
    
    
        Person p1=new Person("Kobe",24);
        Person p2=new Person("James",23);
        p2=p1;
        System.out.println(p1.equals(p2));	//true
    }
}

Rewrite equals:

    public boolean equals(Object obj){
    
    
        if(obj==null){
    
    
            return false;
        }
        if(obj==this){
    
    
            return true;
        }
        if(obj instanceof  Person){
    
    
            Person p=(Person)obj;
            boolean b=this.name.equals(p.name)&&this.age==p.age;
            return b;
        }
        return false;
    }

2.Date class
Date(): Get the current system time

import java.util.Date;

public class demo02 {
    
    
    public static void main(String[] args) {
    
    
        method1();
    }
    
    private static void method1() {
    
    
        Date date=new Date();
        System.out.println(date);
        //Sat Jun 20 10:11:52 CST 2020
    }
}

DateFormat class:
the java.text.DateFormat date / time formatting subclasses of abstract classes, we complete the conversion between text and date by the class
format: in the specified format, the conversion from the object Date String object
analysis: as specified The format is converted from String to Date object.
Member method:
String format(Date date) Format the Date date into a string conforming to the format according to the specified format
Date parse(String source) Parse the string conforming to the pattern into Date date

public class demo03 {
    
    
    public static void main(String[] args) throws ParseException {
    
    
        method1();
        method2();
    }

    private static void method2() throws ParseException {
    
    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date date=sdf.parse("2020年06月20日 10时37分01秒");
        //Sat Jun 20 10:37:01 CST 2020
    }

    private static void method1() {
    
    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date date=new Date();
        String d=sdf.format(date);
        sdf.format(date);
        System.out.println(date);   //Sat Jun 20 10:37:01 CST 2020
        System.out.println(d);   //2020年06月20日 10时37分01秒
    }
}

3.Calendar class
Abstract class, provides methods to manipulate calendar fields

Calendar c= Calendar.getInstance();

Common member methods:
public int get(int field): return the value of a given calendar field
public void set(int field, int value): set the given calendar field to a given value
public abstract void add(int field, int amount): According to the rules of the calendar, add or subtract the specified amount of time for a given calendar field
public Date getTime(): return a Date object representing the time value of this Calendar

public class demo04 {
    
    
    public static void main(String[] args) {
    
    
        method01();
        method02();
    }

    private static void method02() {
    
    
        Calendar c=Calendar.getInstance();
        c.set(Calendar.YEAR,2019);
        c.set(2020,5,20);   //设置日期
    }

    private static void method01() {
    
    
        Calendar c=Calendar.getInstance();
        int year=c.get(Calendar.YEAR);
        System.out.println(year);   //2020
        int month=c.get(Calendar.MONTH);
        System.out.println(month);  //5
    }
}

Guess you like

Origin blog.csdn.net/qq_44708714/article/details/106868201