java basic API, date time calendar

preface

For example, with the continuous development of artificial intelligence, the technology of machine learning is becoming more and more important. Many people have started learning machine learning. This article introduces the basic content of machine learning.

1, Object

• class Object is the root class of the class hierarchy. Each class uses Object as its superclass. All objects (including arrays) implement the methods of this class
• functions to be mastered by the Object class: (both java also provide quick generation)
– equals (if it is not overridden, it is generally just that the comparison addresses are different; the String class has been overridden)
– toString (if it is not rewritten, it is generally just the class name + "@" + address; the String class has been rewritten)

  • Student s = new Student();
    System.out.println(s.toString()); // Equivalent to next line
    System.out.println(s);//.toString() is generally hidden by default

2, System

• the facilities provided by the System class include standard input, standard output and error input
Outflow; Access to externally defined attributes and environment variables; Load files and
Method of library; There are also practical ways to quickly copy parts of an array.
• functions to be mastered by System class
–arraycopy

  • static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
    *Copy array
    *Parameter 1: source array
    *Parameter 2: starting index position of source array
    *Parameter 3: target array
    *Parameter 4: starting index position of target array
    *Parameter 5: specify the number of accepted elements
    */
    int[] src = {1,2,3,4,5};
    int[] dest = new int[5];
    System.arraycopy(src, 1, dest, 3, 2);
    for (int i = 0; i < dest.length; i++) {
    System.out.print(dest[i]); //00023
    }

–exit
System.exit(0) is a normal exit program, and system Exit (1) or non-0 indicates abnormal exit from the program
Normal exit means that if the current program still has tasks under execution, it will exit after all tasks are completed; Abnormal exit is to stop the program as soon as the time is up, regardless of whether there are tasks still executing.

–currentTimeMillis
Returns the current time difference in milliseconds (1970-1-1 00:00:00) or a timestamp

3, Date, DateFormat, Calendar

• the class Date represents a specific moment, accurate to milliseconds.
• the DateFormat class is a class that formats dates. – It is an abstract class, so its subclass SimpleDateFormat is used when it must be used, and there are many pattern letters to remember in the subclass.

1. use the default mode to build objects

private static void method() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat();
		//Create date object
		Date date = new Date();
		//Format converts a date object to a string
		String s = sdf.format(date);
		System.out.println(s);//20:27 pm on June 3, 2022
		//Parsing converts a string to a date object
		Date d = sdf.parse("2022/6/3 20:27 PM");
		System.out.println(d.toLocaleString());//20:27:00 pm on June 3, 2022
	}

2. use the specified pattern to build objects

private static void method2() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day hh:mm:ss");
		//format
		Date date = new Date();
		String s = sdf.format(date);
		System.out.println(s);//June 3, 2022 22:27:01
		*/		
		//analysis
		Date d = sdf.parse("2022 June 3, 2001:01:01");
		System.out.println(d.toLocaleString());//June 3, 2022 01:01:01 am
	}

• the calendar class is a class that formats dates. – Calendar class is a calendar class, which is used to replace the use of Date class. It provides many functions to obtain some data of the calendar independently.

3. Use of calendar

	Calendar c = Calendar.getInstance();	
	//modify
	c.set(Calendar.YEAR, 2030);
	c.set(Calendar.MONTH,8);
	//Increase or decrease
	c.add(Calendar.HOUR_OF_DAY, 3);
	//obtain
	int year = c.get(Calendar.YEAR);
	int month = c.get(Calendar.MONTH)+1;
	int day = c.get(Calendar.DAY_OF_MONTH);
	int hour = c.get(Calendar.HOUR_OF_DAY);
System.out.println(year+"year"+month+"month"+day+"day"+hour+"spot");

Tags: Java

Posted by kbdrand on Fri, 03 Jun 2022 23:30:29 +0530