工具类全部都是静态方法
获取系统属性信息
static Properties
getProperties()
static String
getProperty(String key)
设置自定义属性信息
static String
setProperty(String key, String value)
命令行设置自定义属性信息
1: import java.util.*;
2: class SystemDemo
3: {
4: public static void main(String[] args)
5: {
6: Properties prop = System.getProperties();
7:
8: //设置系统的自定义信息
9: //System.setProperty("mykey","myvalue");
10:
11: //获取指定属性
12: String name = System.getProperty("os.name");
13:
14: System.out.println("os name = "+name);
15:
16: //Properties是Hashtable
17: for(Object obj : prop.keySet())
18: {
19: String value = (String)prop.get(obj);
20: System.out.println(obj+"::"+value);
21: }
22:
23: }
24: }
单例模式设计的类,通过getRuntime静态方法获取唯一的对象
获取唯一Runtime对象
static Runtime
getRuntime()
执行程序
1: class RuntimeDemo
2: {
3: public static void main(String[] args) throws Exception
4: {
5: //获取runtime对象
6: Runtime r = Runtime.getRuntime();
7:
8: //执行打开记事本命令,该程序的检索目录来自于path环境变量
9: Process p = r.exec("notepad.exe");
10:
11: Thread.sleep(4000);
12:
13: //结束记事本进程
14: p.destroy();
15:
16: //执行notepad,并用notepad打开一个文件
17: Process p1 = r.exec("notepad.exe d:\\code\\java_interview\\Test17.java");
18:
19:
20: }
21: }
Date为日期类
SimpleDateFormat为时间格式类
例子
1: import java.util.*;
2: import java.text.*;
3:
4: class DateDemo
5: {
6: public static void main(String[] args)
7: {
8: //获取时间对象
9: Date d = new Date();
10: //打印未格式化的时间
11: System.out.println(d);
12:
13: //设置格式为 "2013-10-23 星期三 11:15:15"
14: SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd E HH:mm:ss");
15:
16: //格式化时间对象,得到格式化后的时间
17: String time = sdf.format(d);
18:
19: //打印时间
20: System.out.println(time);
21: }
22: }
Date类中很多类都过时了,被Calendar日历类取代
获取对象
static Calendar
getInstance()
获取时间
int
get(int field)
增减时间
abstract void
add(int field, int amount)
设置时间
void
set(int field, int value)
void
set(int year, int month, int date)
两个例子:
1: import java.util.*;
2:
3:
4: class CalendarDemo
5: {
6: public static void main(String[] args)
7: {
8: Calendar c = Calendar.getInstance();
9:
10: showTime(getYestTime());
11:
12: //System.out.println(getFebDays(2011));
13: }
14:
15: //获取随机年的二月有几天
16: private static int getFebDays(int year)
17: {
18: Calendar c = Calendar.getInstance();
19:
20: //设置时间为某年的3月1日
21: c.set(year,2,1);
22: //天数减一,得到2月最后一天
23: c.add(Calendar.DAY_OF_MONTH,-1);
24: //返回2月的最后一天
25: return c.get(Calendar.DAY_OF_MONTH);
26: }
27:
28: //获取昨天现在这个时刻
29: private static Calendar getYestTime()
30: {
31: Calendar c = Calendar.getInstance();
32:
33: //天数减一
34: c.add(Calendar.DAY_OF_MONTH,-1);
35:
36: return c;
37:
38: }
39:
40: //打印时间
41: private static void showTime(Calendar c)
42: {
43: //月份编码表
44: String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
45: //星期编码表
46: String[] days = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
47:
48: //获取对应月份和星期的角标
49: int mon_index = c.get(Calendar.MONTH);
50: int day_index = c.get(Calendar.DAY_OF_WEEK);
51:
52: //打印时间
53: System.out.println(c.get(Calendar.YEAR)+"年"
54: +mons[mon_index]+
55: +c.get(Calendar.DAY_OF_MONTH)+"日 "
56: +days[day_index]+" "
57: +c.get(Calendar.HOUR_OF_DAY)+":"
58: +c.get(Calendar.MINUTE));
59: }
60: }
数学方法相关类
小于指定数的最大整数
static double
floor(double a)
大于指定数的最小整数
static double
ceil(double a)
四舍五入求值
static long
round(double a)
幂次计算
static double
pow(double a, double b)
获取随机数
static double
random()
当random方法第一次被调用时,系统会常见一个Random类,获取的随机数取值是大于等于0小于1的double型