java其他对象

1.System
System不能被实例化,它的方法都是静态成员
static PrintStream err “标准”错误输出流。 
static InputStream in  “标准”输入流。 
static PrintStream out “标准”输出流。 


//获取系统属性和加入系统属性

		//获取用指定键描述的系统属性
		Properties prop = System.getProperties();
		for(Map.Entry<Object,Object> entry:prop.entrySet()){
			System.out.println(entry.getKey()+"::" +entry.getValue());
		}
		
		//设定自定义系统属性
		//System.setProperty("MyKey", "MyValue");
		//如果不加前一句就会显示null
		System.out.println("Mykey:"+System.getProperty("MyKey"));
		
		//获取电脑的操作系统
		System.out.println("os.name:"+System.getProperty("os.name"));

动态加入系统属性
可以在cmd下 使用java -D<键>=<值> 程序名的形式加载
例如 java -D<myKey>=<myValue> OtherObject


2.Runtime
每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过 getRuntime 方法获取当前运行时。 
应用程序不能创建自己的 Runtime 类实例。
该类没有提供构造函数,不可以new对象,那么会想到该类的方法都是静态的,但是发现该类中含有非静态方法,
说明该类肯定会提供方法获取本类对象,该方法是静态的,并返回的是本类类型。由这个特点可以看出该类使用了单例设计模式。

		Runtime runtime = Runtime.getRuntime();
		try {
			//执行某个程序,可以用这个执行某个dos命令,返回一个进程
			//Process p = runtime.exec("D:\\Program Files\\Baofeng\\StormPlayer\\StormPlayer.exe");
			runtime.exec("notepad.exe OtherObject.java");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//杀死进程
			//p.destroy();
		} catch (IOException e) {
			e.printStackTrace();
		} 

3.Date和Calendar

类 Date 表示特定的瞬间,精确到毫秒。Calendar类获取操作时间,获取任意的时间格式


		Date date = new Date();
		//将模式封装到SimpleDateFormat对象中
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日ahh时mm分ss秒");
		//条用format方法格式化Date对象
		String dateString = simpleDateFormat.format(date);
		System.out.println(dateString);
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, 4);//加四年
		//获取日期
		System.out.println(calendar.get(Calendar.MONTH)+1);//0~11代表1-12月份


4.Math和Random


		System.out.println(Math.ceil(1.2));//2.0返回最接近指定数大于等于的整数(double)
		System.out.println(Math.floor(1.0));//1.0返回最接近指定数小于等于的整数(double)
		
		
		System.out.println(Math.random());//返回0.0-1.0之间的伪随机数,包括0,不包括1
		//Random类也可以实现
		Random random = new Random();
		System.out.println(random.nextInt(10));//返回0-10之间的伪随机数,包括0,不包括10





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值