java--其他对象(System,Runtime,Date,Calendar,Math)

System:类中的方法和属性都是静态的。

out:标准输出,默认是控制台。
in:标准输入,默认是键盘。

描述系统一些信息。
获取系统属性信息:Properties getProperties();

import java.util.*;
public class Test
{
	public static void main(String[] args) 
	{
		Properties p=System.getProperties();
		//因为Properties是Hashtable的子类,也就是Map集合的一个子类对象。
		//那么可以通过map的方法取出该集合中的元素。
		//该集合中存储都是字符串。没有泛型定义。
		
		//如何在系统中自定义一些特有信息呢?
		System.setProperty("mykey","myvalue");
		System.out.println("mykey="+System.getProperty("mykey"));

		//获取指定属性信息。
		String value = System.getProperty("os.name");
		System.out.println("value="+value);

		//可不可以在jvm启动时,动态加载一些属性信息呢?
		String v = System.getProperty("haha");
		System.out.println("v="+v);
		
		System.out.println("------------------");
		
		//获取所有属性信息。
		for(Object o:p.keySet())
		{
			String value1=(String)p.get(o);
			System.out.println(o+"...."+value1);
		}
	}
}
结果:

mykey=myvalue
value=Windows 7
v=null
------------------

.....

Runtime对象
该类并没有提供构造函数。
说明不可以new对象。那么会直接想到该类中的方法都是静态的。
发现该类中还有非静态方法。
说明该类肯定会提供了方法获取本类对象。而且该方法是静态的,并返回值类型是本类类型。

由这个特点可以看出该类使用了单例设计模式完成。
该方式是static Runtime getRuntime();

public class Test
{
	public static void main(String[] args) throws Exception
	{
		Runtime r = Runtime.getRuntime();
		Process p = r.exec("F:\\Java\\EditPlus 3\\EditPlus.exe");//在单独的进程中执行指定的字符串命令。
		Thread.sleep(4000);//休息4秒
		p.destroy();//杀死子进程
	}
}

Date:

java.lang.Object
  |— java.text.Format:Format 是一个用于格式化语言环境敏感的信息(如日期、消息和数字)的抽象基类。
      |—  java.text.DateFormat:DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。
          |—  java.text.SimpleDateFormat:SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类。
SimpleDateFormat 使得可以选择任何用户定义的日期-时间格式的模式

import java.util.*;
import java.text.*;
class Test 
{
	public static void main(String[] args) 
	{
		Date d = new Date();
		System.out.println(d);//打印的时间看不懂,希望有些格式。

		//将模式封装到SimpleDateformat对象中。
		SimpleDateFormat sdf = new SimpleDateFormat("yyy年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("1:"+l);
		System.out.println("d1:"+d1);
	}
}
结果:

Sat Mar 29 10:39:10 CST 2014
time=2014年03月29日星期六 10:39:10
1:1396060750542
d1:Sat Mar 29 10:39:10 CST 2014

Calendar:
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

import java.util.*;
import java.text.*;
class Test 
{
	public static void main(String[] args) 
	{
		Calendar c = Calendar.getInstance();
		sop(c);
		sop("----------c-----------");
		printCalendar(c);
		sop("---SimpleDateFormat---");
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月d日E hh:mm:ss");
		String year = sdf.format(d);
		System.out.println(year);
		sop("--------c.set---------");
		c.set(2012,2,5);
		c.add(Calendar.DAY_OF_MONTH,-5);
		printCalendar(c);
	}
	public static void printCalendar(Calendar c)
	{
		String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
		String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};		
		int index = c.get(Calendar.MONTH);
		int index1 = c.get(Calendar.DAY_OF_WEEK);
		System.out.print(c.get(Calendar.YEAR)+"年");//sop((c.get(Calendar.MONTH)+1)+"月");
		System.out.print(mons[index]);  
		System.out.print(c.get(Calendar.DAY_OF_MONTH)+"日");//sop("星期"+c.get(Calendar.DAY_OF_WEEK));
		System.out.println(weeks[index1]);
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}
结果:

java.util.GregorianCalendar[time=1396069531826,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=2,WEEK_OF_YEAR=13,WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=88,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=5,SECOND=31,MILLISECOND=826,ZONE_OFFSET=28800000,DST_OFFSET=0]
----------c-----------
2014年三月29日星期六
---SimpleDateFormat---
2014年03月29日星期六 01:05:31
--------c.set---------
2012年二月29日星期三


Math:

数学方法类

import java.util.*;
class Test 
{
	public static void main(String[] args) 
	{
		Random r=new Random();
		sop((r.nextInt(10)+1));
	}
	public void mathRandom()
	{
		for(int i=0;i<10;i++)
		{
			sop((int)(Math.random()*10+1));//random()返回带正号的伪 double 值,该值大于等于 0.0 且小于 1.0。
		}
	}
	public static void show()
	{
		double d = Math.ceil(16.34);//ceil返回大于指定数据的最小整数。
		sop("d="+d);
		double d1 = Math.floor(12.34);//floor返回小于指定数据的最大整数。
		sop("d1="+d1);
		long l = Math.round(12.54);//返回最接近参数的 long,即四舍五入
		sop("l="+l);
		double d2 = Math.pow(2,3);//返回第一个参数的第二个参数次幂的值。
		sop("d2="+d2);
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}
结果(生成一个1-10的随机数):

6

你可能感兴趣的:(java--其他对象(System,Runtime,Date,Calendar,Math))