1.1 Math 数学类
//产生一个0-1之间的随机小数
Math.random() 获取a-b之间的随机整数a-b
(int)(Math.random()*(b-a))+a
Math.abs()// 绝对值
Math.sin() Math.cos() Math.tan() Math.acos() Math.asin()
Math.atan()//三角函数
Math.cbrt(double a)//立方根
Math.sqrt()//平方根
Math.ceil();//向上取整 3.14 4
Math.floor()//向下取整 3.14 3
Math.max()//返回较大值
Math.min()//返回较小值
Math.pow(double a,
double b) // 返回a的b次方的值
Math.round() //四舍五入
1.2 String 类
char c = fileName.charAt(0);//返回指定位置的字符
String newStr = fileName.concat(“abc”);//拼接字符串
boolean bool = fileName.contains(“dfa”);//判断是否包含指定的字符串
boolean bool2 = fileName.endsWith(".jpg");//判断字符串是否以指定的字符串结尾
boolean bool4 = fileName.startsWith(“df”);//判断字符串是否以指定的字符串开头
boolean bool3 = fileName.equals(“asd”);//判断字符串是否相同
byte [] bytes = fileName.getBytes();//将字符串转换为byte数组
int index = fileName.indexOf(“dfa”);//判断指定字符串在该字符串中首次出现的位置 如果没有则返回-1
int index3 = fileName.indexOf(“f”, 4);//从指定位置开始判断指定字符串在该字符串中首次出现的位置 如果没有则返回-1
int index4 = fileName.indexOf(102, 4);//102是f的ascii码
int index2 = fileName.lastIndexOf(“dfa”);//判断指定字符串在该字符串中最后一次出现的位置 如果没有则返回-1
int index5 = fileName.lastIndexOf(“f”, 2);//从指定位置开始寻找(从右往左找)判断指定字符串在该字符串中最后一次出现的位置 如果没有则返回-1
String s = fileName.trim();//取出首尾空格
String s1 = fileName.toUpperCase();//转换为大写
String s2 = fileName.toLowerCase();//转换为小写
String s3 = fileName.substring(8);//截取字符串 从8截取到末尾
String s4 = fileName.substring(8, 10);//截取字符串从8开始到10结束
boolean bool5 = fileName.isEmpty();//判断是否为空字符串 “”
int length = fileName.length();//字符串长度
1.3 Clendar 日历类
Calendar calendar = Calendar.getInstance();//返回当前时间的日历对象
calendar.set(Calendar.YEAR,2000);//设置年份 其他的也可以设置
calendar.add(Calendar.HOUR_OF_DAY,
-15);//对应时间加上对应值 可以为正负数
int year = calendar.get(Calendar.YEAR);//返回年份
int month = calendar.get(Calendar.MONTH);//返回月份 从0开始
int day = calendar.get(Calendar.DAY_OF_MONTH);//返回日
int hour = calendar.get(Calendar.HOUR_OF_DAY);//小时
int minute = calendar.get(Calendar.MINUTE);//分
int second = calendar.get(Calendar.SECOND);//秒
int week = calendar.get(Calendar.DAY_OF_WEEK);//秒
Date d = calendar.getTime();//返回date
System.out.println(year+“年”+month+“月”+day+“日”+hour+“时”+minute+“分”+second+“秒”+“星期”+week);
1.4 System 类
long start = System.currentTimeMillis();当前时间的时间戳
System.gc();//垃圾回收
System.exit(0);//退出程序 0正常退出 -1异常退出
1.5 Date 类
描述日期和时间
Date date = new Date(); 创建一个date描述了当前时间
//Fri Dec 27 11:20:05 CST 2019
Date date = new Date();
date.getTime();//返回date的对应的时间戳
System.out.println(date);
//2019-12-27 11:21:00
//日期格式化器
SimpleDateFormat dateFormat = new
SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String s = dateFormat.format(date);//格式化日期
//异常捕获
try {
Dated = dateFormat.parse(“2019-12-27 11:23:08”);//将字符串转换为date
}
catch (Exception e) {
e.printStackTrace();
}