System类
System:类中的方法和属性都是静态的。
out:标准输出,默认是控制台。
in:标准输入,默认是键盘。
描述系统一些信息。
获取系统属性信息:Properties getProperties();
Properties prop = System.getProperties();
因为Properties是Hashtable的子类,也就是Map集合的一个子类对象。
那么可以通过map的方法取出该集合中的元素。
该集合中存储都是字符串。没有泛型定义。
Runtime类
Runtime对象
该类并没有提供构造函数。
说明不可以new对象。那么会直接想到该类中的方法都是静态的。
发现该类中还有非静态方法。
说明该类肯定会提供了方法获取本类对象。而且该方法是静态的,并返回值类型是本类类型。
由这个特点可以看出该类使用了单例设计模式完成。
该方式是static Runtime getRuntime();
class RuntimeDemo
{
public static void main(String[] args) throws Exception
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("notepad.exe SystemDemo.java");
//Thread.sleep(4000);
//p.destroy();
}
}
Date类
import java.util.*;
import java.text.*;
class DateDemo
{
public static void main(String[] args)
{
Date d = new Date();
System.out.println(d);//打印的时间看不懂,希望有些格式。
//将模式封装到SimpleDateformat对象中。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日E hh:mm:ss");
//调用format方法让模式格式化指定Date对象。
String time = sdf.format(d);
System.out.println("time="+time);
long l = System.currentTimeMillis();
Date d1 = new Date(l);
System.out.println("d1:"+d1);
}
}
Calender类
Calendar类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
import java.util.*;
import java.text.*;
class CalendarDemo
{
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();//该方法返回一个Calendar类对象
String[] mons = {"一月","二月","三月","四月" //查表法获得月份
,"五月","六月","七月","八月"
,"九月","十月","十一月","十二月"};
String[] weeks = {
"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",
};
int index = c.get(Calendar.MONTH);
int index1 = c.get(Calendar.DAY_OF_WEEK);
sop(c.get(Calendar.YEAR)+"年");
//sop((c.get(Calendar.MONTH)+1)+"月");
sop(mons[index]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
//sop("星期"+c.get(Calendar.DAY_OF_WEEK));
sop(weeks[index1]);
//Date类获取年的方法 自定义格式只有年"yyyy"
/*
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String year = sdf.format(d);
System.out.println(year);
*/
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
Math类
import java.util.*;
class MathDemo
{
public static void main(String[] args)
{
Random r = new Random();
for(int x=0; x<10; x++)
{
int i = r.nextInt(10)+1;
//int i = (int)(Math.random()*10+1);
sop(i);
}
/*
double d = Math.ceil(132.0);//ceil返回大于指定数据的最小整数
double d1 = Math.floor(15.54);//floor返回小于指定数据的最大整数
long l = Math.round(165.42);//四舍五入
double l1 = Math.pow(2,3);//2的3次幂
sop("l1 = "+l1);
sop("l = "+l);
sop(d);
sop(d1);
*/
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}