程序员

System、Runtime、Date、Math、Random类

2019-01-11  本文已影响15人  我可能是个假开发

System、Runtime、Date、Math、Random

一、System

用于获取系统的属性

常用方法:

package com.hcx.string;

import java.util.Arrays;
import java.util.Properties;

public class Demo5 {
    
    public static void main(String[] args) {
        //获取系统属性
        Properties properties = System.getProperties();
        //输出系统属性
        properties.list(System.out);
        
        //获取操作系统名称
        String osName = System.getProperty("os.name");
        System.out.println(osName);//Windows 10
        
        //检测操作系统是否支持该软件
        if("Windows XP".equals(osName)){
            System.out.println("继续安装");
        }else{
            System.out.println("系统不兼容");
        }
        
        //获取path环境变量值
        System.out.println(System.getenv("path"));
        
        int[] srcArr = {2,4,6,8,10};
        //把srcArr的数组元素拷贝 到destArr数组中。
        int[] destArr = new int[4];
        System.arraycopy(srcArr, 1, destArr, 0,4);
        //System.exit(0); //jvm退出     注意: 0或者非0的 数据都可以退出jvm。对于用户而言没有任何区别。
        System.out.println("目标数组的元素:"+ Arrays.toString(destArr)); // [4, 6, 8, 10]
        System.out.println("当前的系统时间:" + System.currentTimeMillis());//1547106940898
        System.out.println("环境变量:"+System.getenv("JAVA_HOME"));//C:\Program Files\Java\jdk1.8.0_131
        
        for(int i = 0 ; i<4; i++){
            new Person("hcx"+i);
            System.gc(); //建议马上启动垃圾回收期
        }
    }
}

class Person{
    
    String name;

    public Person(String name) {
        this.name = name;
    }
    
    @Override
    public void finalize() throws Throwable {
        super.finalize();
        System.out.println(this.name+"被回收了..");
    }
}

二、Runtime

Runtime 类主要描述的是应用程序运行的环境。

常用方法:

public class Demo6 {
    
    public static void main(String[] args) throws IOException, InterruptedException {
        //获取应用运行环境的对象
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("C:\\Windows\\notepad.exe");
        Thread.sleep(5000); //让当前程序停止5秒。
        process.destroy();
        System.out.println(" Java虚拟机中的空闲内存量。"+runtime.freeMemory());// Java虚拟机中的空闲内存量。126930080
        System.out.println("Java 虚拟机试图使用的最大内存量:"+ runtime.maxMemory());//Java 虚拟机试图使用的最大内存量:1883242496
        System.out.println("返回 Java 虚拟机中的内存总量:"+ runtime.totalMemory());//返回 Java 虚拟机中的内存总量:128974848
    }
}

三、Date

Date 类封装的是系统的当前时间。
Calendar: 该类是一个日历的类,封装了年月日时分秒时区。

public class Demo {
    
    public static void main(String[] args) throws ParseException {
        /*Date date = new Date(); // 获取当前的系统时间
        System.out.println("年份:"+ date.getYear());*/
        /*
        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));
        
        // 显示 当前系统时间: 2019年1月10日  xx时xx分xx秒   
        
         *  日期格式化类    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 = "1994年12月18日   11:11:11";
        Date date2 = dateFormat.parse(birthday);  //注意: 指定的字符串格式必须要与SimpleDateFormat的模式要一致。
        System.out.println(date2);
        
        Date date21 =new Date();
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        String time2 =dateFormat.format(date21);
        String time21=dateFormat.format(date);
        System.out.println("当前的系统时间:"+time);
        String birthday1= "1994年12月18日   11:11:11";
        Date date22=dateFormat.parse(birthday1);
        System.out.println(date22);
    }
}

四、Math

Math 数学类,主要是提供了很多的数学公式。

常用方法:

System.out.println("绝对值:"+Math.abs(-3));//绝对值:3
System.out.println("向上取整:"+Math.ceil(3.14));//向上取整:4.0
System.out.println("向下取整:"+Math.floor(-3.14)); //向下取整:-4.0
System.out.println("四舍五入:"+Math.round(3.54));//四舍五入:4
System.out.println("随机数:"+Math.random());//随机数:0.23240663573556708
上一篇 下一篇

猜你喜欢

热点阅读