System、Runtime、Date、Math、Random类

System、Runtime、Date、Math、Random

一、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表示异常退出jvm。
  • gc() 建议jvm赶快启动垃圾回收期回收垃圾。
  • getenv(String name) 根据环境变量的名字获取环境变量。
  • getProperty(key)
  • finalize() 如果一个对象被垃圾回收 器回收的时候,会先调用对象的finalize()方法。
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 类主要描述的是应用程序运行的环境。

常用方法:

  • getRuntime() : 返回当前应用程序的运行环境对象。
  • exec(String command) :启动一个字符串命令进程,根据指定的路径执行对应的可执行文件。
  • freeMemory() :返回 Java 虚拟机中的空闲内存量。以字节为单位
  • maxMemory() :返回 Java 虚拟机试图使用的最大内存量。
  • totalMemory() :返回 Java 虚拟机中的内存总量
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 数学类,主要是提供了很多的数学公式。

常用方法:

  • abs(double a) 获取绝对值
  • ceil(double a) 向上取整
  • floor(double a) 向下取整
  • round(float a) 四舍五入
  • random() 产生一个随机数. 大于等于 0.0 且小于 1.0 的伪随机 double 值
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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值