黑马程序员之System、Runtime、Math、Date、Calendar类

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

一、System类

import java.util.Properties;

/*
 * System类: 描述系统的一些信息
 * Properties getProperties()  获取系统属性信息
 * 
 */


public class SystemDemo1 {

	public static void main(String[] args) {

		//Properties是HashTable的子类, 也是Map集合的一个子类对象, 可以通过Map中的方法取出该集合中的原素
		//该集合中存储的都是字符串, 没有泛型定义
		Properties prop = System.getProperties();
		
		for(Object obj : prop.keySet()){
			String val = (String)prop.get(obj);
			System.out.println(obj + " -> " + val);
		}
		
		
		//向系统中设置值
		System.setProperty("mykey", "myvalue");
		
		//获取指定信息
		String val = System.getProperty("os.name");
		System.out.println("val -> " + val);
		
		
	}

}

二、Runtime类

import java.io.IOException;

public class RuntimeDemo1 {

	public static void main(String[] args) throws IOException {

		//返回该Runtime对象
		Runtime run = Runtime.getRuntime();
		//执行一个本机程序, 返回该程序的进程
		Process p = run.exec("notepad RuntimeDemo.java");
		
		p.destroy();	//杀死进程
		
	}

}

三、Math类

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;

public class MathDemo {

	public static void main(String[] args) {

		// 返回大于指定数据的最小整数
		double d = Math.ceil(32.1);

		// 小于指定数据的最大整数
		d = Math.floor(32.1);

		// 四舍五入
		long l = Math.round(32.1);

		// 次方
		d = Math.pow(2, 2);

		// 随机数[0,1)伪随机数
		d = Math.random();

		Random r = new Random();
		d = r.nextInt(10);

		// 保留2位小数, "#.00"表示2位, "#.000"表示3位
		DecimalFormat df = new DecimalFormat("#.000");
		String str = df.format(1.235);

		// 保留2位小数, 最后位是四舍五入
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(2); // 设置保留位数
		str = nf.format(1.236);

		System.out.println(str);

		System.out.println(d);

	}

}

四、Date类

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateDemo {

	public static void main(String[] args) {

		//返回当前的系统时间
		Date d = new Date();
		System.out.println(d);
		
		//格式化时间
		DateFormat df = new SimpleDateFormat("yyyy年MM月dd日    E hh:mm:ss" );
		String time = df.format(d);
		System.out.println(time);
	}

}

五、Calendar类

import java.util.Calendar;

public class CalendarDemo {

	public static void main(String[] args) {

		
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);   //获取年
		int month = cal.get(Calendar.MONTH);	//月
		int day = cal.get(Calendar.DAY_OF_MONTH);//日
		int week = cal.get(Calendar.DAY_OF_WEEK);//星期
		
		System.out.println(year + " : " + month + " : " + day + " : " + week);
		
		//设置时间, 注意月是[0,11], 所以2就是3月
		cal.set(2012,2,23);
		
		//时间量的偏移
		cal.add(Calendar.YEAR, 1);
		
	
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值