黑马程序员-常用类

------- android培训 java培训 、期待与您交流! ----------



毕向东老师第18天课笔记


System类:

import java.util.Properties;


public class SystemDemo {
	public static void main(String[] args) {
		//得到系统属性,存入Properties(HashTable的子类,值键集合),也是Map集合的子类对象,可以使用map的方法取出该集合中的元素
		Properties prop = System.getProperties();
		
		//在系统中自定义一些特有信息
		prop.setProperty("myKey", "myValue");
		
		//遍历
		for(Object key : prop.keySet()){
			System.out.println(key + "::" + prop.get(key));
		}
		
		//动态指定键值
		System.out.println("hh = " + prop.getProperty("hh"));
		
		//获取指定属性信息
		System.out.println("操作系统 = " + prop.getProperty("os.name"));
	}
}


Runtime类


public class RuntimeDemo {
	public static void main(String[] args) throws Exception {
		//实例化
		//无构造函数-->>不能直接创建对象,可能都是静态方法-->>有非静态方法-->>有静态的获取本类对象的方法-->>一般是单例模式
		Runtime r = Runtime.getRuntime();
		//D:\\金山打字\\Typeeasy\\TypeEasy.exe
		Process p = r.exec("notepad.exe");//可以“notepad.exe RuntimeDemo.java”用记事本打开
		//暂停3秒
		Thread.sleep(3000);
		//销毁
		p.destroy();
	}
}


Date类

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);
		
		//将模式封装到SimpleDateFormat对象中
		SimpleDateFormat sdf = new SimpleDateFormat("D");
		//调用format方法,让模式格式化指定的日期对象,得到一年中的第几天
		String s = sdf.format(d);
		System.out.println("一年中的第几天?  " + s);
		
		//按指定格式打印日期
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 E hh:mm:ss");
		System.out.println(sdf1.format(d));
		
	}
}


Calendar类

import java.util.Calendar;


public class CalendarDemo {
	public static void main(String[] args) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, 1);//设置指定字段的值
		c.add(Calendar.YEAR, 1);//在指定字段的值上增加或减少
		c.add(Calendar.MONTH, -2);//在指定字段的值上增加或减少
		printCalendar(c);
	}
	
	static void sop(Object o){
		System.out.println(o);
	}
	
	//技巧:查表法
	static void printCalendar(Calendar c){
		//打印指定对象的日期中文格式
		String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
		int index = c.get(Calendar.MONTH);
		
		String[] weeks = {"","星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
		//对应的值分别为1,2,3,4,5,6,7
		int indexx = c.get(Calendar.DAY_OF_WEEK);
		
		sop(c.get(Calendar.YEAR) + "年" + mons[index] + c.get(Calendar.DAY_OF_MONTH) + "日" + weeks[indexx]);
	}
}


Math类

import java.util.Random;


public class MathDemo {
	public static void main(String[] args) {
		//ceil方法 返回大于等于指定数据的最小整数
		double d = Math.ceil(1.2);
		sop(d);
		
		//floor方法返回小于等于指定数据的最大整数
		double d2 = Math.floor(1.2);
		sop(d2);
		
		//round方法  四舍五入
		long l = Math.round(1.4);
		sop(l);
		sop(Math.round(1.5));
		
		//pow方法返回a参数的b次幂
		double d3 = Math.pow(2, 3);
		sop(d3);
		
		//random方法 产生随机数   0.0 <= Math.random() < 1.0
		sop(Math.random());
		//产生1 ~ 10 的十个随机数;
		for(int i = 0; i < 10; i ++){
			sop((int)(Math.random() * 10) + 1);
		}
		//util包中的Random类
		sop("============");
		for(int i = 0; i < 10; i ++){
			Random r = new Random();
			int ii = r.nextInt(10) + 1;//得到1~10的随机数
			sop(ii);
		}
	}
	
	
	static void sop(Object o){
		System.out.println(o);
	}
}


课堂练习题

import java.util.Calendar;
import java.util.Date;


/**
 * 1.打印指定年二月的天数;
 * 2.获取昨天的现在得这个时刻
 * @author xdy
 *
 */
public class Exam1 {
	
	public static void main(String[] args) {
		test(2013);
		Calendar c = Calendar.getInstance();
		test2(c);
	}
	//1.获取指定年的二月的天数
	static void test(int year){
		//得到日历对象
		Calendar c = Calendar.getInstance();
		//设置时间为指定年的3月1日
		c.set(year, 2, 1);
		//在上面设定时间的基础上减掉一天,得到二月的最后一天
		c.add(Calendar.DAY_OF_MONTH, -1);
		//输出二月最后一天对应的‘号’即是指定年的二月的天数
		System.out.println(c.get(Calendar.DAY_OF_MONTH));
	}
	
	//2.获取昨天的现在得这个时刻
	static void test2(Calendar c){
		c.add(Calendar.DAY_OF_YEAR, -1);
		CalendarDemo.printCalendar(c);
		System.out.println();
		//date方式获得前一天
		Date d = new Date();
		d.setDate(d.getDate() - 1);
		System.out.println(d.toLocaleString());
		
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值