java日常学习:System系统类

java代码块


System系统类

System系统类 : 主要的作用是用于获取系统的一个参数。

System类需要掌握的方法:
arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 拷贝数组
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。

currentTimeMillis() 获取当前的系统时间
exit(int status) 退出jvm,0表示正常退出jvm,非0 表示异常退出。

gc() 建议jvm尽快的启动垃圾回收器回收垃圾。

System类需要掌握的方法:
getenv(String name) 获取环境变量
getProperties() 获取系统属性。
static String getProperty(String key)

Runtime类:代表了当前程序的运行环境。
Runtime对象需要掌握方法:
exec(String command) 执行指定路径下的可执行文件。

Date 日期类:

Calendar 日期类

SimpleDateFormat 日期格式化类:
作用:
1. 可以将时间对象转成指定格式的字符串。 format();
2. 可以把字符串转成日期对象。 parse();

Math 数学类 :

常用的方法:
abs(double a)  绝对值
ceil(double a) 向上取整 。
floor(double a)
round(double a)
random()


练习一

package day02;

import java.util.Properties;

public class Demo1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /*String path = System.getenv("path");
        System.out.println(path);*/

        Properties properties = System.getProperties();
        properties.list(System.out);  //获取所有的系统属性
        String osName = System.getProperty("os.name");
        System.out.println(osName);
        if ("Windows 10".equals(osName)) {
            System.out.println("版本正确");
        }else {
            System.out.println("版本错误");
        }

练习 二

package day02;

import java.io.IOException;

public class Demo2 {

    public static void main(String[] args) throws IOException, Exception {
        // TODO Auto-generated method stub
        Runtime runtime = Runtime.getRuntime();   //获取Runtime对象
        /*Process p = runtime.exec("C:\\Program Files (x86)\\EditPlus\\editplus.exe");
        Thread.sleep(3000);   //让当前程序暂停3秒钟
        p.destroy();   //杀死进程
        */
        System.out.println("试图使用的最大内存量:"+runtime.maxMemory());
        System.out.println("Java 虚拟机中的内存总量:"+runtime.totalMemory());
        System.out.println("当前空闲的内存:"+ runtime.freeMemory());
    }

}

练习三

package day02;

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

public class Demo3 {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        /*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 = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        /*//把日期对象转换指定格式的字符串  format
        String date = simpleDateFormat.format(new Date());
        System.out.println("日期是:"+date);*/

        //字符串----> 时间对象    字符串的格式必须要与SimpleDateformat指定的模式要一致,否则报错。
                String text = "1990年09月08日   07:01:00";
                Date date = simpleDateFormat.parse(text);
                System.out.println(date);

    }

}

练习四

package day02;

import java.util.Random;

public class Demo4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //产生四位随机验证码
        char[] arr = {'a','s','8','6','5','4','2','v','w',};
        //创建一个字符串缓冲区类
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        //产生四个随即的索引值
        for(int i=0;i<4;i++) {
            int index = random.nextInt(arr.length);
            char temp = arr[index];
            stringBuilder.append(temp);
        }

        System.out.println("验证码是:"+stringBuilder);
    }

}

用于平时自己复习回顾,如有错误,欢迎指正

猜你喜欢

转载自blog.csdn.net/yan_star/article/details/78526176