常用类(System,Runtime,date类, Math 数学类,Random 随机数类 )

System类

System.arraycopy(srcArr, 1, destArr, 0,4); 重要 数组间赋值 集合的时候会用到
System.exit(0)正常退出 非0 异常退出 一般都是tyr中exit(0) catch中 exit(非0)
System.currentTimeMillis()当前的系统时间 从1971开始 的毫秒单位 重要
System.gc() 建议jvm赶快启动垃圾回收期回收垃圾。 (调用回收机制前会启动对象的finalize的方法 )
Properties xx=System.getProperties(); 需要import java.util.Properties; xx.list(System.out);显示的是系统的属性 String
name=System.getProperty(“os.name”);
System.out.println(name);根据系统的属性名获取对应的属性值 String
name1=System.getenv(“JAVA_Home”);
System.out.println(name1);根据环境变量的名字获取环境变量。


Runtime
RunTime 该类类主要代表了应用程序运行的环境。

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类:
如果需要知道当前时间的某个时间段 那需要Calendar 没有构造函数 调用getInstance就行(是静态的函数)
本来是date.getYear()但是被Calendar给替代了
如果是获取时间段的一部分就用Calendar而不是date
例子:

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 数学类, 主要是提供了很多的数学公式。

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

随机数类
Random
需求: 编写一个函数随机产生四位的验证码。

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);

猜你喜欢

转载自blog.csdn.net/qq_38409944/article/details/80108249