Common classes (System, Runtime, date class, Math math class, Random random number class)

System class

System.arraycopy(srcArr, 1, destArr, 0,4); When assigning sets between important arrays,
System.exit(0) will be used to exit normally and non-zero exceptions are generally exit(0) in tyr. Non-0)
System.currentTimeMillis() The current system time in milliseconds since 1971. Important
System.gc() It is recommended that the jvm start the garbage collection period to collect garbage as soon as possible. (The finalize method of the object will be started before calling the recycling mechanism)
Properties xx=System.getProperties(); Need to import java.util.Properties; xx.list(System.out); Displays the properties of the system String
name=System. getProperty("os.name");
System.out.println(name); Get the corresponding property value according to the property name of the system String
name1=System.getenv("JAVA_Home");
System.out.println(name1);According to The name of the environment variable gets the environment variable.


Runtime
RunTime This class mainly represents the environment in which the application runs.

getRuntime()  返回当前应用程序的运行环境对象。
exec(String command)  根据指定的路径执行对应的可执行文件。
freeMemory()   返回 Java 虚拟机中的空闲内存量。。 以字节为单位
maxMemory()    返回 Java 虚拟机试图使用的最大内存量。
totalMemory()    返回 Java 虚拟机中的内存总量
Runtime runtime = Runtime.getRuntime();
//      Process process = runtime.exec("C:\\Windows\\notepad.exe");要抛出异常
//      Thread.sleep(3000); //让当前程序停止3秒。
//      process.destroy();
        System.out.println(" Java虚拟机中的空闲内存量。"+runtime.freeMemory());
        System.out.println("Java 虚拟机试图使用的最大内存量:"+ runtime.maxMemory());
        System.out.println("返回 Java 虚拟机中的内存总量:"+ runtime.totalMemory());

Date class:
If you need to know a certain time period of the current time, you need Calendar. There is no constructor to call getInstance (it is a static function).
It was originally date.getYear() but was replaced by Calendar.
If it is to get a part of the time period, use it Calendar instead of date
example:

Calendar calendar = Calendar.getInstance(); //获取当前的系统时间。
System.out.println("年:"+ calendar.get(Calendar.YEAR));
System.out.println("月:"+ (calendar.get(Calendar.MONTH)+1));
System.out.println("日:"+ calendar.get(Calendar.DATE));
“`
System.out.println(“时:”+ calendar.get(Calendar.HOUR_OF_DAY));
System.out.println(“分:” + calendar.get(Calendar.MINUTE));
System.out.println(“秒:”+ calendar.get(Calendar.SECOND));


如果要将指定的时间转换为指定的显示方式就用 SimpleDateFormat
 *  日期格式化类    SimpleDateFormat 
         *          作用1: 可以把日期转换转指定格式的字符串     format()
         *          作用2: 可以把一个 字符转换成对应的日期。    parse()   生日
         *      
         */

Date date = new Date(); //获取当前的系统时间。
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss") ; //使用了默认的格式创建了一个日期格式化对象。
    String time = dateFormat.format(date);  //可以把日期转换转指定格式的字符串
    System.out.println("当前的系统时间:"+ time);

    String birthday = "2000年12月26日   11:29:08";
    Date date2 = dateFormat.parse(birthday);  //注意: 指定的字符串格式必须要与SimpleDateFormat的模式要一致。
    System.out.println(date2);

“`


Math Math class, mainly provides a lot of mathematical formulas.

 abs(double a)  获取绝对值
 ceil(double a)  向上取整
 floor(double a)  向下取整
 round(float a)   四舍五入
 random()   产生一个随机数. 大于等于 0.0 且小于 1.0 的伪随机 double 值

Random number class
Random
requirements: Write a function to randomly generate a four-digit verification code.

Random random = new Random();
int randomNum = random.nextInt(10)+1; //产生 的 随机数就是0-10之间
System.out.println("随机数:"+ randomNum);

char[] arr = {'中','国','传','a','Q','f','B'};
StringBuilder sb = new StringBuilder();
Random random = new Random();
//需要四个随机数,通过随机数获取字符数组中的字符,
for(int i  = 0 ; i< 4 ; i++){
        int index = random.nextInt(arr.length);  //产生的 随机数必须是数组的索引值范围之内的。
sb.append(arr[index]);
}
System.out.println("验证码:"+ sb);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326006566&siteId=291194637