4-java常见对象-常用类的介绍及使用

14.11_常见对象(Math类概述和方法使用)

  • A:Math类概述
    • Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
  • B:成员方法
    • public static int abs(int a)
    • public static double ceil(double a)
    • public static double floor(double a)
    • public static int max(int a,int b)
    • public static double pow(double a,double b)
    • public static double random()
    • public static int round(float a)
    • public static double sqrt(double a)

/**
	 * @param args
	 * * A:Math类概述
		* Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。 
	* B:成员方法
		* public static int abs(int a)
		* public static double ceil(double a)
		* public static double floor(double a)
		* public static int max(int a,int b)
		* public static double pow(double a,double b)
		* public static double random()
		* public static int round(float a) 
		* public static double sqrt(double a)
	 */
	public static void main(String[] args) {
		
		System.out.println("//打印结果是:");

		System.out.println(Math.PI);
		System.out.println(Math.abs(-10));  			//取绝对值
		
		//ceil天花板
		/*
		 * 13.0
		 * 12.3
		 * 12.0
		 */
		System.out.println(Math.ceil(12.3));			//向上取整,但是结果是一个double
		System.out.println(Math.ceil(12.99));
		
		System.out.println("-----------");
		//floor地板
		/*
		 * 13.0
		 * 12.3
		 * 12.0
		 */
		System.out.println(Math.floor(12.3));			//向下取整,但是结果是一个double
		System.out.println(Math.floor(12.99));
		
		//获取两个值中的最大值
		System.out.println(Math.max(20, 30));
		
		//前面的数是底数,后面的数是指数
		System.out.println(Math.pow(2, 3));				//2.0 ^ 3.0
		
		//生成0.0到1.0之间的所以小数,包括0.0,不包括1.0
		System.out.println(Math.random());
		
		//四舍五入
		System.out.println(Math.round(12.3f));
		System.out.println(Math.round(12.9f));
		
		//开平方
		System.out.println(Math.sqrt(4));
		System.out.println(Math.sqrt(2));
		System.out.println(Math.sqrt(3));
	
	}
	//打印结果是:
	3.141592653589793
	10
	13.0
	13.0
	-----------
	12.0
	12.0
	30
	8.0
	0.5499534369777632
	12
	13
	2.0
	1.4142135623730951
	1.7320508075688772


14.12_常见对象(Random类的概述和方法使用)

  • A:Random类的概述
    • 此类用于产生随机数如果用相同的种子创建两个 Random 实例,
    • 则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
  • B:构造方法
    • public Random()
    • public Random(long seed)
  • C:成员方法
    • public int nextInt()
    • public int nextInt(int n)

	/**
	 * * A:Random类的概述
			* 此类用于产生随机数如果用相同的种子创建两个 Random 实例,
			* 则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
		* B:构造方法
			* public Random()
			* public Random(long seed)
		* C:成员方法
	       //返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
			* public int nextInt() 
		    //返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
			* public int nextInt(int n)
	 */
	public static void main(String[] args) {
		
		System.out.println("------------Random--nextInt()----------");
		Random r1 = new Random();
		//返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
		int x = r1.nextInt();
		System.out.println(x);
		
		System.out.println("------Random----nextInt(int n) ------------");
		
		Random r2 = new Random();
		for(int i = 0; i < 10; i++) {
			//System.out.println(r.nextInt());
			System.out.println(r2.nextInt(100));			//要求掌握,生成在0到n范围内的随机数,包含0不包含n
		}
		
		/*
		 * -1244746321
			1060493871
			
			-1244746321
			1060493871

		 */
		System.out.println("------Random(long seed)----nextInt(int n) ------------");
		Random r3 = new Random(1001);//使用单个 long 种子创建一个新的随机数生成器。
		
		int a = r3.nextInt();
		int b = r3.nextInt();
		
		System.out.println(a);
		System.out.println(b);
	}
	//打印结果如下:
	------------Random--nextInt()----------
	562474177
	------Random----nextInt(int n) ------------
	97
	0
	60
	22
	81
	12
	93
	79
	88
	33
	------Random(long seed)----nextInt(int n) ------------
	-1245131070
	-2078988849


14.13_常见对象(System类的概述和方法使用)

  • A:System类的概述
    • System 类包含一些有用的类字段和方法。它不能被实例化。
  • B:成员方法
    • public static void gc()
    • public static void exit(int status)
    • public static long currentTimeMillis()
    • pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
  • C:案例演示
    • System类的成员方法使用

	public class DemoSystem {
	
		@Test
		public void testSystemGc(){
			
			System.out.println("--------System.gc()----------");
			for(int i= 0;i<2;i++){
				new Person();
				System.gc();//运行垃圾回收器,相当于呼喊保洁阿姨
			}
		}
		
		@Test
		public void testSystemExit(){
			System.out.println("-------System.exit(1)-----------");
			System.exit(1);							//非0状态是异常终止,退出jvm
			System.out.println("11111111111");
		}
		
		@Test
		public void testSystemCurrentTime(){
			System.out.println("--------System.currentTimeMillis()----------");
			long time = System.currentTimeMillis();
			System.out.println(String.valueOf(time));
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String currentTime = sdf.format(new Date(time));
			
			System.out.println(currentTime);
		}
		
		
		@Test
		public void testSystemCopyArray(){
			System.out.println("--------System.arraycopy----------");
			int [] array1 = {12,9,4,1,55,76};
			System.out.println("数组1:"+Arrays.toString(array1));
			
			int [] array2 = new int [array1.length];
			System.out.println("数组2:"+Arrays.toString(array2));
			
			/**
			 *  src - 源数组。
				srcPos - 源数组中的起始位置。
				dest - 目标数组。
				destPos - 目标数据中的起始位置。
				length - 要复制的数组元素的数量。 
			 */
			System.arraycopy(array1, 0, array2, 0, array1.length);
			
			System.out.println("拷贝后的数组:"+Arrays.toString(array2));
		}
		
	}
	
	 class Person{   //在一个源文件中不允许定义两个用public修饰的类
	
		 @Override
		protected void finalize() throws Throwable {
			// TODO Auto-generated method stub
			System.out.println("资源被销毁了");
		}
		
	}

	//打印结果如下:
	--------System.gc()----------
	资源被销毁了
	资源被销毁了
	--------System.currentTimeMillis()----------
	1470032925284
	2016-08-01 14:28:45
	-------System.exit(1)-----------


14.14_常见对象(BigInteger类的概述和方法使用)

  • A:BigInteger的概述
    • 可以让超过Integer范围内的数据进行运算
  • B:构造方法
    • public BigInteger(String val)
  • C:成员方法
    • public BigInteger add(BigInteger val)
    • public BigInteger subtract(BigInteger val)
    • public BigInteger multiply(BigInteger val)
    • public BigInteger divide(BigInteger val)
    • public BigInteger[] divideAndRemainder(BigInteger val)

	public static void main(String[] args) {
		//long num = 123456789098765432123L;
		//String s = "123456789098765432123";
		
		BigInteger bi1 = new BigInteger("100");
		BigInteger bi2 = new BigInteger("2");
		
		System.out.println(bi1.add(bi2)); 				//+
		System.out.println(bi1.subtract(bi2));			//-
		System.out.println(bi1.multiply(bi2)); 			//*
		System.out.println(bi1.divide(bi2));    		///(除)
		
		BigInteger[] arr = bi1.divideAndRemainder(bi2);	//取除数和余数
		
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}
	//打印结果如下:
	102
	98
	200
	50
	50
	0


14.15_常见对象(BigDecimal类的概述和方法使用)

  • A:BigDecimal的概述
    • 由于在运算的时候,float类型和double很容易丢失精度,演示案例。

    • 所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal

    • 不可变的、任意精度的有符号十进制数。

  • B:构造方法
    • public BigDecimal(String val)
  • C:成员方法
    • public BigDecimal add(BigDecimal augend)
    • public BigDecimal subtract(BigDecimal subtrahend)
    • public BigDecimal multiply(BigDecimal multiplicand)
    • public BigDecimal divide(BigDecimal divisor)
  • D:案例演示
    • BigDecimal类的构造方法和成员方法使用

	/**
	 * * A:BigDecimal的概述
			* 由于在运算的时候,float类型和double很容易丢失精度,演示案例。
			* 所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
		
			* 不可变的、任意精度的有符号十进制数。
		* B:构造方法
			* public BigDecimal(String val)
		* C:成员方法
			* public BigDecimal add(BigDecimal augend)
			* public BigDecimal subtract(BigDecimal subtrahend)
			* public BigDecimal multiply(BigDecimal multiplicand)
			* public BigDecimal divide(BigDecimal divisor)
		* D:案例演示
			* BigDecimal类的构造方法和成员方法使用
		十进制表示1/3
		0.3333333333333333333333333333333333333333 
			 */
	public static void main(String[] args) {
		System.out.println(2.0 - 1.1);
		BigDecimal bd1 = new BigDecimal(2.0);		//这种方式在开发中不推荐,因为不够精确
		BigDecimal bd2 = new BigDecimal(1.1);
		System.out.println(bd1.subtract(bd2));
		
		BigDecimal bd3 = new BigDecimal("2.0");		//通过构造中传入字符串的方式,开发时推荐
		BigDecimal bd4 = new BigDecimal("1.1");
		
		System.out.println(bd3.subtract(bd4));
		
		BigDecimal bd5 = BigDecimal.valueOf(2.0);	//这种方式在开发中也是推荐的
		BigDecimal bd6 = BigDecimal.valueOf(1.1);
		
		System.out.println(bd5.subtract(bd6));
	}
	//打印结果如下:
	0.8999999999999999
	0.899999999999999911182158029987476766109466552734375
	0.9
	0.9


14.16_常见对象(Date类的概述和方法使用)

  • A:Date类的概述
    • 类 Date 表示特定的瞬间,精确到毫秒。
  • B:构造方法
    • public Date()
    • public Date(long date)
  • C:成员方法
    • public long getTime()
    • public void setTime(long time)

	@Test
	public void testDate1(){

		Date d1 = new Date();					//如果没有传参数代表的是当前时间
		System.out.println(d1);

		Date d2 = new Date(0);					//如果构造方法中参数传为0代表的是1970年1月1日
		System.out.println(d2);					//通过毫秒值创建时间对象
	}

	@Test
	public void testDate2(){

		Date d1 = new Date();
		//      返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
		System.out.println(d1.getTime());
		//      当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)。
		System.out.println(System.currentTimeMillis());

	}
	
	@Test
	public void testDate3(){

		Date d1 = new Date();
		d1.setTime(1000);
        System.out.println(d1);
	}
	//打印结果如下:
	Mon Aug 01 15:22:32 CST 2016
	Thu Jan 01 08:00:00 CST 1970
	1470036152585
	1470036152586
	Thu Jan 01 08:00:01 CST 1970


14.17_常见对象(SimpleDateFormat类实现日期和字符串的相互转换)

  • A:DateFormat类的概述
    • DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat
  • B:SimpleDateFormat构造方法
    • public SimpleDateFormat()
    • public SimpleDateFormat(String pattern)
  • C:成员方法
    • public final String format(Date date)
    • public Date parse(String source)

	@Test
	public  void demo4() {
		//demo1();
		//demo2();
		//demo3();
		
		//将时间字符串转换成日期对象
		String str = "2000年08月08日 08:08:08";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		Date d;
		try {
			d = sdf.parse(str);
			System.out.println(d);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}						//将时间字符串转换成日期对象
		
	}
	@Test
	public  void demo3() {
		Date d = new Date();							//获取当前时间对象
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//创建日期格式化类对象
		System.out.println(sdf.format(d));				//将日期对象转换为字符串
	}
	@Test
	public  void demo2() {
		Date d = new Date();							//获取当前时间对象
		SimpleDateFormat sdf = new SimpleDateFormat();	//创建日期格式化类对象
		System.out.println(sdf.format(d));	 			//88-6-6 下午9:31
	}
	@Test
	public  void demo1() {
		//DateFormat df = new DateFormat();				//DateFormat是抽象类,不允许实例化
		//DateFormat df1 = new SimpleDateFormat();
		DateFormat df1 = DateFormat.getDateInstance();	//相当于父类引用指向子类对象,右边的方法返回一个子类对象
	}
	//打印结果如下:
	16-8-1 下午4:43
	2016/08/01 16:43:20
	Tue Aug 08 08:08:08 CST 2000


14.18_常见对象(你来到这个世界多少天案例)

  • A:案例演示
    • 需求:算一下你来到这个世界多少天?

	@Test
	public void testMyLifeDays(){
		
		String birthday= "1990-02-12 05:30:00";
		
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
	    try {
			Date date = sdf.parse(birthday);
			long timeBegin = date.getTime();
			System.out.println("出生时间:"+timeBegin);
			
			long timeEnd = System.currentTimeMillis();
			System.out.println("当前时间:"+timeEnd);
			
			long time = timeEnd - timeBegin;
			System.out.println("时  间  差:"+time);
			
			long day = time / (1000*60*60*24);
			System.out.println(day);
//			837907200000
//			837948780537
			
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		}
	//打印结果如下:
	出生时间:632093400000
	当前时间:1470043577804
	时  间  差:837950177804
	9698


14.19_常见对象(Calendar类的概述和获取日期的方法)

  • A:Calendar类的概述
    • Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
  • B:成员方法
    • public static Calendar getInstance()
    • public int get(int field)

	@Test
	public void testCalendar(){

		Calendar c = Calendar.getInstance();

		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;//月份是从0开始
		int day = c.get(Calendar.DAY_OF_MONTH);

		System.out.println(year+"-"+month+"-"+day);
		//打印结果如下:
//		2016-8-2

	}

	@Test
	public void testCalendar2(){

		Calendar c = Calendar.getInstance();

		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		int day_of_month = c.get(Calendar.DAY_OF_MONTH);
		int day_of_week = c.get(Calendar.DAY_OF_WEEK);

		//****年**月**日  星期*

		System.out.println(year+"年"+month+"月"+getNum(day_of_month)+"日  "+getWeek(day_of_week));

        //打印结果如下:
//		2016年8月02日  星期三

	}

	private String getWeek(int day_of_week) {

		String [] arr_week = {
				"星期日",
				"星期一",
				"星期二",
				"星期三",
				"星期四",
				"星期五",
				"星期六"
		};

		return arr_week[day_of_week];
	}

	private String  getNum(int day_of_month) {

		return day_of_month > 9 ? ""+day_of_month:"0"+day_of_month;

	}

14.20_常见对象(Calendar类的add()和set()方法)(掌握)

  • A:成员方法
    • public void add(int field,int amount)
    • public final void set(int year,int month,int date)
  • B:案例演示
    • Calendar类的成员方法使用

	@Test
	public  void testCalendarAdd() {
		//demo1();
		Calendar c = Calendar.getInstance();			//父类引用指向子类对象
		c.add(Calendar.MONTH, -1);					//对指定的字段进行向前减或向后加
		c.set(Calendar.YEAR, 2000);					//修改指定字段
		
		System.out.println(c.get(Calendar.YEAR)+"-"+c.get(Calendar.MONDAY));
		System.out.println("-------------------------");
		
		Calendar c2 = Calendar.getInstance();
		c2.set(2016, 8, 2);
        System.out.println(c2.get(Calendar.YEAR)+"-"+c2.get(Calendar.MONTH)+"-"+c2.get(Calendar.DAY_OF_MONTH));
		
	}
    //打印结果如下:
	2000-6
	-------------------------
	2016-8-2


14.21_常见对象(如何获取任意年份是平年还是闰年)(掌握)

  • A:案例演示
    • 需求:键盘录入任意一个年份,判断该年是闰年还是平年

	/**
	 * 1.键盘录入Scanner
	 * 2.获取Calendar实例
	 * 3.设置为那一年的3月1日
	 * 4.将日期向前减去1
	 * 5.判断日期是多少天,如果等于29就返回true否则返回false
	 * @param args
	 */
	public static void main(String [] args){
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份:");
		
		String line = sc.nextLine();
		int year = Integer.parseInt(line);
		System.out.println(year);
		
		boolean  b= getYear(year);
		System.out.println("---是否是闰年---"+b);
	}

	/**
	 * 判断当前年份是否是闰年
	 * @param year
	 * @return
	 */
	private static boolean getYear(int year) {

		//1.创建Calendar实例
		Calendar c = Calendar.getInstance();
		//2.设置为那一年的3月1日
		c.set(year, 2, 1);
		//3.将日期向前减去1
		c.add(Calendar.DAY_OF_MONTH, -1);
		
		return c.get(Calendar.DAY_OF_MONTH) == 29;
	}
	//打印结果如下:
	请输入年份:
	2016
	2016
	---是否是闰年---true


posted on 2016-08-02 11:03  天涯游者  阅读(548)  评论(0)    收藏  举报

导航