5.Character类
A:统计字符串中大写字符、小写字符、数字的个数
String s = "....";
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
char[] chs = s.toCharArray();
for(int x = 0;x < chs.length; x++){
char ch = chs[x];
if(Character.isUpperCase(ch)){
bigCount++;
}else if(Character.isLowerCase(ch)){
samallCount++;
}else if(Character.isDigit(ch)){
numberCount++;
}
}
6.Math类
A:猜数字游戏
B:获取任意范围的随机数
public static int getRandom(int start,int end){
int number = (int)(Math.random()*(end - start + 1)) + start;
return number;
}
7.System类
A:统计程序运行时间
long start = System.currentTimeMillis();
for(int x = 0; x < 10000; x++){
System.out.println("hello"+x);
}
long end = System.currentTimeMillis();
long result = end - start;
8.Date/DateFormat
A:Date --- String 之间的相互转换
a:Date --- String(格式化)
public final String format(Date date)
//创建日期对象
Date d = new Date();
//创建格式化对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(d);
改进版:
Date d = new Date();
String format = "yyyy-MM-dd HH:mm:ss";
String s = new SimpleDateFormat(format).format(d);
b:String --- Date(解析)
public Date parse(String source)
String s = "2015-12-12 10:36:8"
//创建格式化对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(s);
改进版:
String s = "2015-12-12 10:36:8"
String format = "yyyy-MM-dd HH:mm:ss";
Date d = new SimpleDateFormat(format).parse(s);
B:计算从某天开始到现在的天数
String str = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
Date d = sdf.parse(str);
//获取开始时间的毫秒值
long startTime = d.getTime();
//获取当前时间的毫秒值
long newTime = System.currentTimeMillis();
long time = newTime - startTime;
//把毫秒值换算为天
long day = time/1000/60/60/24;
9.Calendar类
A:操作日历时间并获取时间
//创建当前时间日历对象
Calendar c = Calendar.getInstance();
//分别对年月日进行加减设置
c.add(Calendar.YEAR,-3);
c.add(Calendar.MONTH,15);
c.add(Calendar.Date,-65);
//设置日历时间
c.set(2008,8,8);
//分别获取年月日
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
B:计算任意一年的二月有多少天
int year = ...;
Calendar c = Calendar.getInstance();
//设置日期为该年的三月一日
c.set(year,2,1);
c.add(Calendar.DATE,-1);
int date = c.get(Calendar.DATE);