工具类,Array工具类.md

Data:日期类

Data:数据类,他表示的二进制的数据
		Date date = new Date();
		System.out.println("当前的时间:"+date);
		
		//long型的时间
		long time = System.currentTimeMillis();//获取的是当前的时间,单位是毫秒
		System.out.println(time);
		
		//先实现将long型时间转成Date型
		Date date2 = new Date(time);
		System.out.println("date2:"+date2);
		
		//将date转型long型时间
		System.out.println(date2.getTime());

Format:格式化器


		//1.使用系统默认提供的格式-DateFormat,默认的格式有:short,long,full,default
		//第一个参数:设置日期的格式   第二个参数:设置时间的格式
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
		String dateStr = dateFormat.format(new Date());
		System.out.println(dateStr);
		//2.使用自定义的格式:例子:1998/19/12 13:29:34---SimpleDateFormat
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");//HH表示二十四小时  hh:表示12小时制
		String date1Str = simpleDateFormat.format(new Date());
		System.out.println(date1Str);
	}

Math:数学计算类


		System.out.println(Math.abs(-4));//求绝对值
		System.out.println(Math.floor(3.4));//向下取整
		System.out.println(Math.ceil(3.04));//向上取整
		System.out.println(Math.random());//取随机数  [0,1)
		
		//实例:取[0,10)之间的整数
		System.out.println((int)Math.floor(Math.random()*10));
		
		//Random类
		//实例:取[0,100)之间的整数
		Random random = new Random();
		System.out.println(Math.abs(random.nextInt()%100));
		System.out.println(random.nextInt(100));
	

程序运行时间


	long startTime = System.currentTimeMillis();//获取当前时间
	//doSomeThing();   //要运行的java程序
	long endTime = System.currentTimeMillis();
	System.out.println("程序运行时间:"+(endTime-startTime)+"ms");

Arrays工具类

数组工具类,内部封装了大量操作数组的方法,包括排序,查找,求最大值等

简单数据类型转换成字符串

方便查看内容
		int[] arr1 = new int[]{3,5,8,9,3};
    	System.out.println(arr1.toString());//[I@1db9742,默认打印的是数组的地址
    	String string = Arrays.toString(arr1);
    	System.out.println(string);//[3, 5, 8, 9, 3],数组被转化成了字符串

数组转集合

集合的方法多,操作便捷
		int[] arr2 = new int[]{3,5,8,9,3};
    	List<int[]> list = Arrays.asList(arr2);
    	System.out.println(list.size());//1,说明对于简单数据类型的数组,asList方法会将整体当做集合的一个元素处理
    	//引用数据类型转集合
    	String[] strings = {"a","b","c"};
    	List<String> list2 = Arrays.asList(strings);
    	System.out.println(list2.size());//3,说明对于引用数据类型的数组,asList方法会将数组的一个元素作为集合的一个元素
    	
    	//数组转集合的注意点:转成的集合的元素个数是固定的,所以不能进行添加或删除,可以进行修改,遍历
    	//list2.add("java"); //运行报异常UnsupportedOperationException
    	list2.set(0, "d");
    	System.out.println(list2);

二分查找

数组必须是有序的
		int[] arr3 = {2,4,6,8,90};
    	int index = Arrays.binarySearch(arr3, 6);
    	System.out.println(index);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值