System类中的方法和属性都是静态的
常用方法:
long currentTimeMillis();
获取当前时间的毫秒值
Proerties getProperties();
获取系统的属性信息,并存储到了Properties集合中。
Properties集合中存储的是String类型的键和值。
Set<String> stringPropertyNames();
方法可以把键返回到一个set集合中。
String getPropetry();
方法可以通过键取值
String getProperty(String Key);
方法可以获取指定键指示的系统属性。
//Properties 有自己的存取方法,当然也可用迭代器。
Properties prop = System.getProperties();
Set<String> nameSet = prop.stringPropertyNames();
for(String name:nameSet){
String value = prop.getProperty(name);
System.out.println(name+"::"+value);
}
Runtime类:使用了单例设计模式,构造函数不对外开放,该类提供静态的返回该类对象的方法。
Runtime getRuntime();
获取Runtime类的对象。
Process exec(String command);
执行命令
destroy();
杀掉子进程。
Math类:提供了操作数学运算的方法,都是静态的。
ceil();返回大于参数的最小整数。
floor();返回小于参数的最大整数。
round();返回四舍五入的整数。
pow(a,b,);a的b次方。
random();随机数,大于0小于1。
Random类是用于生成伪随机数的。
Date类:时间和日期的对象。
日期对象和毫秒值之间的转换。
毫秒值->日期对象。
1。通过Date对象的构造方法 new Date(Millis);
2。通过Date 中的setTime(Millis)方法设置。
日期对象->毫秒值.
getTime方法
对日期对象进行格式化。
日期对象->日期对象的字符串。
使用的是DateFormate类中的formate方法。
先获取日期格式化对象,然后将date作为参数对象传入formate函数,转为日期对象的字符串类型。
DateFormate是抽象类不能创建子类,所以使用getInstance方法获取对象。
日期格式的字符串->日期对象
使用的是DateFormate类中的parse方法。
SimpleDateFormat类可以对日期时间对象自定义格式。
//练习1:
// * "2012-3-17"到"2012-4-6"
// * 中间有多少天。
/*
* 1.首先把两个时间字符串转为时间对象。
* 2.将时间对象转为毫秒值。
* 3.两个毫秒值进行相减,得到两个毫秒值之差。
* 3.将毫秒值变为天数。
*
* */
public class DEMO {
public static void main(String[] args){
String str_date1 = "2012-3-17";
String str_date2 = "2012-4-6";
int day = getDay(str_date1,str_date2);
System.out.println(day);
}
public static int getDay(String str_date1, String str_date2) {
//1.将时间字符串转为时间对象。
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = dateFormat.parse(str_date1);
Date date2 = dateFormat.parse(str_date2);
//将时间对象转为毫秒值;
long time1 = date1.getTime();
long time2 = date2.getTime();
//得到毫秒值之差
long time = Math.abs(time2-time1);
//转为天数。
int day = transferDay(time);
return day;
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
private static int transferDay(long time) {
return (int)(time/1000/60/60/24);
}
}
Calendar类 可以操作时间日期的字段。
add(int field, int amount);
根据日历的规则,为给定的日历字段添加或者减去指定的时间量。
get(int field);
返回给定日历的字段值。
set();
设置年月日时间等。
它也是抽象类,要用getInstance方法获取对象。
/*
练习2;
得到每个月有多少天
*/
public class DEMO2 {
public static void main(String[] agrs){
//创建对字段操作的对象
Calendar cal = Calendar.getInstance();
int day = getDayOfMonth(2013,1);
// method(cal);
System.out.println(day);
}
//对时间字段进行的操作,主要是输出年月日和星期。
private static void method(Calendar cal) {
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DAY_OF_MONTH);
String week = getWeek(cal.get(Calendar.DAY_OF_WEEK));
System.out.println(year+"年"+month+"月"+day+"日"+week);
}
private static String getWeek(int i) {
String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};
return weeks[i];
}
//得到某一月的天数,
public static int getDayOfMonth(int year,int month) {
Calendar cal = Calendar.getInstance();
cal.set(year,month,1);
cal.add(Calendar.DAY_OF_MONTH,-1);
method(cal);
return cal.get(Calendar.DAY_OF_MONTH);
}