Common classes (API) in Java

1. Regular expressions

A regular expression (regex) is a string that uses some special symbols to formulate a rule, and then uses this rule to match the string. It is a pattern matching syntax.

Regular expressions in the matches function:

"\d" matches digits equivalent to [0-9]

"\w" matches letters and numbers and underscore

"\s" matches whitespace characters

"[3-9]" Numbers from 3 to 9

"[357]" only allows 3,5,7 to appear

"[^357]" does not allow 3,5,7 to appear

"[Az]" all uppercase and lowercase letters

* The number of occurrences allowed is 0 or more

+ occurs one or more times

? 0 times or once

{n} appears exactly n times

{n,} appears at least n times

{n,m} At least n times, at most m times

"." matches any character except newline

2.StringBuffer class and String class

Functions in String class

● Replacement function
String replace(char old,char new)
String replace(String old,String new)
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
● Remove two spaces from the string
String trim() //Remove Space at the end of the string

StringBuffer class

● Overview of the StringBuffer class.
If we splice strings, a new String
object will be constructed each time, which is time-consuming and wastes space. StringBuffer can solve this problem.
Thread-safe variable character sequence

● Construction method
public StringBuffer()
public StringBuffer(String str)

● Add function
public StringBuffer append(String str)
public StringBuffer insert(int offset,String str)
● Delete function
public StringBuffer deleteCharAt(int index)
public StringBuffer delete(int start,int end)
● Replace function
public StringBuffer replace(int start, int end,String str)
● Reverse function
public StringBuffer reverse()

● Interception function
public String substring(int start)
public String substring(int start, int end)
● Interception function is different from the previous functions.
The return value type is String type, itself has not changed
● StringBuilder class function is completely consistent with StringBuffer function , StringBuffer is thread
safe

public class StringBufferdemo1 {
    public static void main(String[] args) {
        //StringBuffer b=new StringBuffer("ABC");
        //StringBuffer b=new StringBuffer("");
        StringBuffer a=new StringBuffer(10);
        a.append("aa");
        a.append("bbbbbbbbbbb");//数组扩容 原来长度乘以2再加2 ,不会创建新的Stringbuffer对象
        a.delete(1,4);
        a.deleteCharAt(1);
        String ss=a.substring(0);//返回值类型是String类型,本身没有发生改变
        System.out.println(ss);
        a.replace(0,2,"11");
        System.out.println(a);
        System.out.println(a.reverse());
        StringBuilder c=new StringBuilder("aaa");
        c.append(1);
        System.out.println(c.reverse());
        System.out.println(c);
    }

String class StringBuffer class StringBuilder difference

● String: It is a character constant, suitable for a small number of string operations
● StringBuilder: Suitable for a large number of operations on the character buffer under a single thread
● StringBuffer: Suitable for a large number of operations on the character buffer under multi-threads

3.Math class

java.lang.Math provides a series of static methods for scientific calculations; the parameters and
return value types of its methods are generally double.
abs absolute value
sqrt square root
pow(double a, double b) b power of a
max(double a, double b)
min(double a, double b)
random() returns a random number from 0.0 to 1.0
long round(double a) Double type data a is converted to long type (rounded)

public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.PI);
        System.out.println(Math.abs(-9));
        System.out.println(Math.sqrt(2.0));
        System.out.println(Math.ceil(2.1));
        System.out.println(Math.floor(2.3));
        System.out.println(Math.pow(2,3));
        System.out.println(Math.round(2.2));
        System.out.println(Math.round(2.7));
        System.out.println(Math.random());
    }
}

4.Random class

● Overview of Random class
This class is used to generate random numbers
● Constructor method
public Random()
l Member method
public int nextInt()
public int nextInt(int n)

public class RandomDemo {
    public static void main(String[] args) {
        Random random=new Random();
        System.out.println(random.nextInt());
        System.out.println(random.nextDouble());
        System.out.println(random.nextInt(10));
        byte[] bytes=new byte[4];
        random.nextBytes(bytes);
        System.out.println(Arrays.toString(bytes));

    }
}

5.Date class/Calendar class/SimpleDateFormat class

 Date class

● Use Date class to represent the current system time
Date d = new Date();
Date d = new Date(long d);

public class DateDemo {
    public static void main(String[] args) {
        Date date=new Date();
        System.out.println(date);
        //java中的过期方法,不建议使用
        System.out.println(date.getDate());
        System.out.println(date.getMonth());
        System.out.println(date.getYear());
        Long e=date.getTime();
        for(int i=0;i<100000;i++){

        }
        Date date1=new Date();
        Long s=date1.getTime();
        System.out.println(s-e);
    }

}

Calendar class

● The Calendar class is an abstract class that implements objects of specific subclasses in actual use. The
process of creating objects is transparent to programmers, and they only need to use the getInstance method to create them
.
Calendar c1 = Calendar.getInstance();
c1.get(Calendar. YEAR);

public class Calendedemo {
    public static void main(String[] args) {
        Calendar calendar=new GregorianCalendar();
        Calendar calendar1=Calendar.getInstance();
        System.out.println(calendar.getTime());
        System.out.println(calendar.getTimeInMillis());
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));

    }


}

SimpleDateFormat date formatting class

● Construction method
SimpleDateFormat(format); // yyyy-MM-dd
● Convert date to string
Date now=new Date();
myFmt.format(now);
● Convert string to date
myFmt.parse(“2018-02-10 ");
The string date format must be consistent with the specified format
. For example: String s = "2018-03-15";
new SimpleDateFormat("yyyy-MM-dd")

public class SimpleDateFormatdemo {
   /* public static void main(String[] args) {
        //日期转字符串
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY年MM月dd日 HH:mm:ss E");
        String s=simpleDateFormat.format(date);
        System.out.println(s);
    }
*/
    //字符串转日期对象
   public static void main(String[] args) throws ParseException {
       String s="2002-07-01";
       SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd");
       Date date=simpleDateFormat.parse(s);
       System.out.println(date.getMonth());
   }
}

Guess you like

Origin blog.csdn.net/weixin_64189867/article/details/130893461
Recommended