常见对象部分案例总结
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;
B:数组复制
int[] arr1 = {......};
int[] arr2 = {......};
System.arraycopy(arr1,2,arr2,1,3);
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);
10.正则表达式
A:邮箱校验
String email = ".....";
//定义邮箱规则
String regex = "[a-zA-Z_0_9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+";
改进为
String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+";
//使用String类的public boolean matches(String regex)判断
boolean flag = email.matches(regex);
B:按照不同规则分割数据
a:
String s1 = "18-25";
String regex1 = "-";
String[] strArray1 = s1.split(regex1);
for(int x = 0;x < strArray1.length; x++){
int start = Integer.parseInt(strArray1[0]);
int end = Integer.parseInt(strArray1[1]);
}
b:
String s2 = "aa bb cc dd";
String regex2 = " +";
String[] strArray2 = s2.split(regex3);
c:
String s3 = "E:\\Java\\JavaSE\\a.txt"
String regex3 = "\\\\";
String[] strArray3 = s3.split(regex3)
C:把论坛中的数组替换为*
String s = "hello qq:123456789 world987762626 java";
String regex = "\\d";
String ss = "*";
String result = s.replaceAll(regex,ss);
D:获取字符串中由三个字符组成的单词
String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
String regex = "\\b\\w{3}\\b";
//把规则编译成模式对象
Pattern p = Pattern.compile(regex);
//通过模式对象得到匹配器对象
Matcher m = p.matcher(s);
//调用匹配器对象的功能得到满足条件的数据
while(m.find()){
System.out.println(m.group());
}
|
|