(JAVA): Common time methods, date methods, random number methods, and basic knowledge must be known.

content

1. Swastika The current time of the system

1, Swastika currentTimeMillis () Method:

2. The NanoTime() method:

3. Note: 

 Second, ☠☠ date operation class 

(1) ☠Date class:

(2) ☠Calendar class:

(3) ☠DateFormat class:

(4) ☠Code example: 

3. ✉✉ random numbers 

(1) ✉ Random class:

(2) ✉ Code example:


 

1. 卍 ​​​​​​​​The current time of the system

1, Swastika currentTimeMillis () Method:

(1) Function:

        Get the current time of the system in milliseconds

(2) Code:

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

        long start = System.currentTimeMillis();//获取系统当前时间

        for (int i = 0; i < 10000; i++) {
            String str = "" + i;
        }

        long end = System.currentTimeMillis();//获取系统当前时间

        System.out.println("程序耗时:" + (end - start) + "毫秒!");

    }
}

2. The NanoTime() method:

 (1) Function:

        Get the current time of the system, unit: nanoseconds

(2) Code:

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

        long start = System.nanoTime();//获取系统当前时间

        for (int i = 0; i < 10000; i++) {
            String str = "" + i;
        }

        long end = System.nanoTime();//获取系统当前时间

        System.out.println("程序耗时:" + (end - start) + "纳秒!");
    }
}

3. Note: 

       Generally, currentTimeMillis() can be used to meet the needs, which is the millisecond time offset of the current time to the first year, and is a long type value. But when the time needs to be more precise, we can use nanoseconds to calculate that is nanoTime(). The nanosecond calculation is also the nanosecond time offset of the first year, and it will return a long type value.


 Second, ☠☠ date operation class 

(1)Date class:

        The Date class object is used to represent time and date. This class provides a series of methods for manipulating the components of time and date. The most used in the Date class is to obtain the current date and time of the system. Such as: Date date = new Date(); this code is to use the current system time to create a date object.

(2)Calendar class:

       The Calendar class is also a class for manipulating dates and times, and it can be seen as an enhanced version of the Date class. Calendar provides a set of methods that allow converting a time in milliseconds to years, months, days, hours, minutes, and seconds. Calendar can be regarded as a perpetual calendar, the default display is the current time, of course, you can also view other times.

         The Calendar class is an abstract class, and the object of the Calendar class can be obtained through the static method getInstance(). In fact, the object obtained is the object of its subclass.

        ▶▶ Some methods provided by this class are as follows:

        ▷   int get(int field): Returns the value of the given calendar field.

        ▷   YEAR: Indicates the year

        ▷   MONTH: indicates the month

        ▷   DAY_OF_MONTH: Indicates the day of the month

        ▷   DAY_OF_WEEK: Indicates the day of the week

(3)DateFormat class:

        This class is a class for formatting date and time. It is an abstract class under the Java.text package that provides a variety of methods for formatting and parsing time. Formatting refers to converting dates to text, and parsing refers to converting text to date format. The most used is its subclass SimpleDateFormat. SimpleDateFormat is a specific class that formats and parses dates in a locale-dependent manner. For example, "yyyy-MM-dd HH:mm:ss" is the specified one. date and time format.

(4) ☠Code example: 

        1. Example of Calendar class:

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

        Calendar time = Calendar.getInstance();

        System.out.print("今天是" + time.get(Calendar.YEAR) + "年"); //输出年
        System.out.print( (time.get(Calendar.MONTH)+1) + "月");     //输出月
        System.out.print(time.get(Calendar.DAY_OF_MONTH) + "日");   //输出日


        System.out.println();
        //输出星期
        System.out.println("今天是星期" + (time.get(Calendar.DAY_OF_WEEK)-1));
    }
}

         2. Example of DateFormat class:

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

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

        Date date = new Date(); //获取当前时间

        SimpleDateFormat mat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化

        System.out.println("当前时间为:" + mat.format(date)); //输出时间

    }
}

3. ✉✉ random numbers 

(1) ✉ Random class:

        1. Common syntax:

        int nextInt()

        ▶ int nextInt(int n)

        ▷ The former returns the next pseudorandom number, which is a uniformly distributed int value in this random number generator's sequence

        ▷ The latter returns the next pseudorandom number, which is an int value taken from this random number generator sequence, uniformly distributed between 0 (inclusive) and the specified value n (exclusive)

(2) ✉ Code example:

import java.util.Random;

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

        //创建一个Random对象
        Random random = new Random();

        //生成随机数
        for (int i = 0; i < 10; i++) {

            int num = random.nextInt(10);

            System.out.println("第" + (i + 1) + "个随机数是" + num);
        }

    }
}

おすすめ

転載: blog.csdn.net/yzh2776680982/article/details/124076158
おすすめ