java common class Day12

Posted by iwanpattyn on Sat, 25 Dec 2021 02:05:04 +0100

SimpleDateFormat class

  • SimpleDateFormat is a concrete class that formats and parses dates in a locale dependent manner
  • Format (date - > text) and parse (text - > date)
  • Common time pattern letters (may be used when creating objects)
letterDate or timeExample
yyear2019
mMid year month08
dDays in the month10
hMedium and small hours in 1 day (0 ~ 23)22
mminute16
ssecond59
Smillisecond367
  • example

To output the time, we first create a SimpleDateFormat object

public class Demo01 {
    public static void main(String[] args) {
        //1. Create a SimpleDateFormat object
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day HH: mm: ss");//Brackets indicate how you want to output the time
    }
}

Then create a Date object

//2. Create Date
        Date date = new Date();

Some people may not understand why two objects are created. First of all, we should make it clear that the objects in the Date class are used to obtain the time. However, only obtaining the time is not enough. You can express it in many ways. For example, you can output "9:43:11 on August 10" or "9:43:11 on August 10"... The purpose of creating the SimpleDateFormat object is to format the Date and give it a predetermined format

//Format Date
        String s = sdf.format(date);

output

        System.out.println(s);

The result is

2021 August 10, 2009 09:47:55

If you want to change the output format, just change the SimpleDateFormat object, such as:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH: mm: ss");

Then the output will become

2021/08/10 09: 47: 55

Formatting date is to convert the date into a string, so you can also convert the string into a date. This process is called parsing

//Parsing: convert string to time
        Date date1=sdf.parse("2001 February 6, 2013 13:50:20");
        System.out.println(date1);

The output result is

Tue Feb 06 13:50:20 CST 2001

The output here is the default format of the time obtained by date

The complete code is as follows:

 package com.commonClass.simpleDateFormatclass;

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

public class Demo01 {
    public static void main(String[] args) throws Exception {
        
        //1. Create a SimpleDateFormat object
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day HH: mm: ss");//Brackets indicate how you want to output the time
        
        //2. Create Date
        Date date = new Date();
        
        //Format Date to convert time to string
        String s = sdf.format(date);
        System.out.println(s);
        
        //Parsing: convert string to time
        Date date1=sdf.parse("2001 February 6, 2013 13:50:20");
        System.out.println(date1);
    }
}

The output result is

2021 August 10, 2014 10:47:27
Tue Feb 06 13:50:20 CST 2001

System class

  • The System class is mainly used to obtain the attribute data and other operations of the System. The construction method is private, so there is no need to create objects when using it. The methods and properties are also static
Method nameexplain
static void arraycopy(......)Copy array
static long currentTimeMillis();Gets the current system time and returns the value in milliseconds
static void gc();It is recommended that the JVM quickly start the garbage collector to collect garbage
static void exit(int status);Exit the jvm. If the parameter is 0, it means to exit the jvm normally, and non-0 means to exit the jvm abnormally
  • Copy array
public class Demo01 {
    public static void main(String[] args) {

        //arraycopy
        // .arraycopy(Object src,int srcPos,Object dest,int destPos,int length);
        // . Arraycopy (source array, from which position to copy, target array, from which position to paste the target array, and the copied length);
        int[]arr={20,18,15,8,35,26,45,90};
        int[]dest=new int[8];
        System.arraycopy(arr,0,dest,0,arr.length);
        for (int i:dest){
            System.out.println(i);
        }
    }
}

The above is to copy an array with length 8 into another array with length 8, and the output result is

20
18
15
8
35
26
45
90

You can see that the array has been completely copied. We can also change the parameter value to achieve some operation purposes

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

        //arraycopy
        // .arraycopy(Object src,int srcPos,Object dest,int destPos,int length);
        // . Arraycopy (source array, from which position to copy, target array, from which position to paste the target array, and the copied length);
        int[]arr={20,18,15,8,35,26,45,90};
        int[]dest=new int[8];
        System.arraycopy(arr,2,dest,4,arr.length-5);//The length of the copy can also be a pure number
        for (int i:dest){
            System.out.println(i);
        }
    }
}

In this code, the original array will be copied from 2 (the third element), the copied length is the array length - 5 (i.e. 3), the target array will be pasted from 4 (the fifth element), and the output result is

0
0
0
0
15
8
35
0

It should be noted that whether copying or pasting, you must be optimistic about the upper limit of the array, otherwise the array will cross the boundary and report an error

  • garbage collection
//System.gc()
Student s1=new Student("Regeneration",19);
Student s2=new Student("Cao Heyu",20);
Student s3=new Student("revive",21);

System.gc();//Tell the garbage collector to recycle
package com.commonClass.systemClass;

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

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

    public Student() {

    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("Recycled"+name+"   "+age);
    }
}

If you run like this, you will find that nothing is output, indicating that it has not been recycled. The reason is that the three objects are still being used. If we modify it a little:

//System.gc()
new Student("Regeneration",19);
new Student("Cao Heyu",20);
new Student("revive",21);

System.gc();//Tell the garbage collector to recycle

The output result is

Recovered regeneration 19
 Recovered Su 21
 Recovered Cao Heyu 20

Description the object has been recycled by the recycler