1.StringBuffer和StringBuilder用法一样,效率都高于String
StringBuffer sb = new StringBuffer(); //1.append();追加 sb.append("java世界第一"); System.out.println(sb.toString()); sb.append("java牛逼"); System.out.println(sb.toString()); //2.insert();添加;在第0个位置前添加“前面” sb.insert(0,"前面"); System.out.println(sb.toString()); //3.replace()指定位置的替换 sb.replace(0,2,"hee"); System.out.println(sb.toString()); //4.delete() 指定位置的删除 sb.delete(0,1); System.out.println(sb.toString());
2.Claender类,受保护的类,不能直接new
//创建Calendar对象 Calendar calendar=Calendar.getInstance(); //返回当前时间 System.out.println(calendar.getTime().toLocaleString()); //获取时间信息 //获取年 int year=calendar.get(Calendar.YEAR); //获取月 从0-11 int month=calendar.get(Calendar.MONTH); //获取日 int day=calendar.get(Calendar.DAY_OF_MONTH); //获取小时 int hour = calendar.get(Calendar.HOUR_OF_DAY);//24小时制 //获取分钟 int minute=calendar.get(Calendar.MINUTE); //获取秒 int second = calendar.get(Calendar.SECOND); System.out.println(year+""+(month+1)+""+day+""+hour+""+minute+""+second+""); //修改时间 Calendar calendar2=Calendar.getInstance(); calendar2.set(Calendar.DAY_OF_MONTH,5); System.out.println(calendar2.getTime().toLocaleString()); //add方法修改时间 calendar2.add(Calendar.HOUR,-1); System.out.println(calendar2.getTime().toLocaleString());
3.SimpleDateFormat类
//创建SimpleDateFormat对象,并设置格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/ HH:mm:ss"); //创建Date Date date = new Date(); //格式化date,把日期转成字符串 String str = sdf.format(date); System.out.println(str);
4.System类
//arraycopy:数组的复制 //src:源数组 //srcPos:从哪个位置开始复制 //dest:目标数组 //destPos:目标数组的位置 //length:复制的长度 //System.arraycopy(src,srcPos,dest,destPos,length); int[] arr={20,21,63,41,54,1}; int[] dest =new int[6]; System.arraycopy(arr,0,dest,0,arr.length); for (int i = 0; i < dest.length ; i++) { System.out.println(dest[i]); } //System.currentTimeMillis获取1970至今的时间(毫秒),可以用来计时 long start = System.currentTimeMillis(); for (int i = -222222; i <999999 ; i++) { for (int j = -222222; j < 6664478; j++) { int sum =i+j; } } long end = System.currentTimeMillis(); System.out.println("用时:"+(end-start));