---------------------- android培训、java培训、期待与您交流! ----------------------
System
类定义:
public final class System extends Object
说明:
System类是一个系统类。
|- 其内的方法和变量都是静态的。
|- 其构造方法被隐藏。
常用方法:
范例1:获得系统属性
import java.util.*; public class Demo { public static void main(String[] args) { String va = System.getProperty("os.name"); System.out.println("value=="+va); System.setProperty("key","value"); Properties prop = System.getProperties(); for(Object obj:prop.keySet()){ String value = (String)prop.get(obj); System.out.println(obj+":"+value); } } } |
范例2: 计算程序执行的时间。
package org.fm.test; public class Demo { public static void main(String[] args) throws Exception { String str = ""; long start = System.currentTimeMillis(); for(int i=0; i<20000; i++) str += i; long end = System.currentTimeMillis(); System.out.println("计算消耗时间---> "+((end-start)/1000)); } } |
语句解释:
· public static long currentTimeMillis() · 返回当前时间。 以毫秒为单位。 |
Runtime
类定义:
public class Runtime extends Object |
每一个JVM启动的时候实际上都对应一个Runtime类的实例,表示运行时。
构造方法:
· 此类的构造方法被私有化了。 · 可以通过 public static Runtime getRuntime() 取得此类的一个实例。 |
范例1: 运行本机上的exe程序
public class Demo { public static void main(String[] args) throws Exception { Runtime r = Runtime.getRuntime(); Process p = r.exec("sol.exe"); //打开纸牌游戏 Thread.sleep(2000); p.destroy(); //杀死进程 } } |
语句解释:
· public Process exec(String command) throws IOException · 使用exec方法执行本机上的exe程序。 · exec方法返回一个Process对象。 · 使用Process对象的destroy()方法可以关闭其所指向的应用程序。 · Thread.sleep(2000); 使程序等待2秒。 |
Date类
import java.util.Date; public class Demo { public static void main(String[] args) { Date time = new Date(); System.out.println(time); } } |
语句解释:
· Date类在java.util包中。 · 直接打印Date类的对象就可以得到当前时间。 · 但是得到的日期格式不符合国人的习惯,因此需要使用其他类对Date类的输出格式进行调整。 |
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.youkuaiyun.com/heima