正则表达式
正则表达式就是符合一定规则的字符串。(就是个字符串,只不过符合了某种规则)
需求:校验qq号码. 1:要求必须是5-15位数字 2:0不能开头 3:必须都是数字
代码演示
public static void main(String[] args) {
System.out.println(checkQQ("012345")); //false
System.out.println(checkQQ("a1b345")); //false
System.out.println(checkQQ("123456")); //true
System.out.println(checkQQ("1234567890987654321"));//false
String regex = "[1-9]\\d{4,14}"; //用正则表达式
System.out.println("2553868".matches(regex)); //true
System.out.println("012345".matches(regex)); //false
System.out.println("2553868abc".matches(regex));//false
}
public static boolean checkQQ(String qq) {
boolean flag = true;//如果校验qq不符合要求就把flag置为false,如果符合要求直接返回
if(qq.length() >= 5 && qq.length() <= 15) {
if(!qq.startsWith("0")) {
char[] arr = qq.toCharArray(); //将字符串转换成字符数组
for (int i = 0; i < arr.length; i++) {
char ch = arr[i]; //记录每一个字符
if(!(ch >= '0' && ch <= '9')) {
flag = false; //不是数字
break;
}
}
}else {
flag = false; //以0开头,不符合qq标准
}
}else {
flag = false; //长度不符合
}
return flag;
}
那究竟符合什么规则呢?字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
[a-z&&[def]] d、e 或 f(交集)
[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
预定义字符
. 任何字符
\d 数字:[0-9] digit
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r] space
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9] word
\W 非单词字符:[^\w]
数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
组捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 ((A)(B(C))) 中,存在四个这样的组:
1 ((A)(B(C)))
2 (A)
3 (B(C))
4 (C)
\\1获取第一组
$1 获取第一组里面匹配到的内容
String利用正则表达式完成匹配功能
public boolean matches(String regex):告知此字符串是否匹配给定的正则表达式。
匹配功能:模式类Pattern和匹配器类Matcher 也可以做到
String s = "aaaab";
String regex = "a*b";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
boolean b = m.matches(); //true
String利用正则表达式完成切割功能
public String[] split(String regex); 按照正则表达式所匹配的字符将字符串切割成字符串数组
注:如果按照. 来切割的话 要转义 用\.
public static void main(String[] args) {
String s = "金三胖.郭美美.李dayone";
String[] arr = s.split("\\."); //通过正则表达式切割字符串
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]); // 金三胖 郭美美 李dayone
}
}
String利用正则表达式完成替换功能
String类的功能:public String replaceAll(String regex,String replacement)注意和我们以前学过的String类里面的一个public String replace(String str,String str1);区分
代码演示:
public static void main(String[] args) {
String s = "wo111ai222heima";
String regex = "\\d"; //\\d代表的是任意数字
String s2 = s.replaceAll(regex, "");
System.out.println(s2); //woaiheima
}
模式类Pattern和匹配器类Matcher用正则表达式完成匹配和查找获取功能
String s = "我的手机是18511866260,我曾用过18987654321,还用过18812345678";
String regex = "1[3578]\\d{9}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while(m.find()){ //查找功能
System.out.println(m.group()); //获取功能
}
Math类:类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
public static int abs(int a):求a的绝对值public static double ceil(double a):求天花板数 a向上取整 返回一个double的数 public static double floor(double a):求地板数 a向下取整 返回一个double的数public static int max(int a,int b):返回a和b中 较大的一个数public static int min(int a,int b):返回a和b中 较小的一个数public static double pow(double a,double b):返回a的b次方public static double random():返回0-1的随机数 包括0但是不包括1public static int round(float a) :把a四舍五入 并返回public static double sqrt(double a):返回a的平方根
构造方法public Random();public Random(int seed); 有种子的构造生成的对象,每次运行生成的随机数都和前一次一样
成员方法public int nextInt():生成int范围内的一个随机整数public int nextInt(int fanwei);生成 0-fanwei 内的一个随机整数
如何获取一个1-100之间的随机数?nextInt(100)+1;(int)(Math.random()*100 + 1)
public static void gc():运行垃圾回收器。public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。public static long currentTimeMillis():返回以毫秒为单位的当前时间pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) :从src数组的srcPos位置开始复制 复制length个 数,到dest数组中,复制的内容 从destPos位置开始往后罗列
可以让超过Integer范围内的数据进行运算A:构造方法public BigInteger(String val)
B:成员方法public BigInteger add(BigInteger val):两个BigInteger相加public BigInteger subtract(BigInteger val):两个BigInteger相减public BigInteger multiply(BigInteger val):两个BigInteger相乘public BigInteger divide(BigInteger val):两个BigInteger相除public BigInteger[] divideAndRemainder(BigInteger val):取除数和余数
由于在运算的时候,float类型和double很容易丢失精度,所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal。不可变的、任意精度的有符号十进制数。A:构造方法public BigDecimal(String val)B:成员方法public BigDecimal add(BigDecimal augend)public BigDecimal subtract(BigDecimal subtrahend)public BigDecimal multiply(BigDecimal multiplicand)public BigDecimal divide(BigDecimal divisor)
构造方法public Date();:获取当前时间public Date(Long times);:把创建出来的Date对象 设置成times代表的时间
成员方法public long getTime() :和 System.currentTimeMillis();一样 获取当前时间的毫秒值public void setTime(Long times);:把当前Date对象设置成times代表的时间
代码演示:
public static void main(String[] args) {
demo1();
demo2();
Date d1 = new Date();
d1.setTime(1000); //设置毫秒值,改变时间对象
System.out.println(d1); //Thu Jan 01 08:00:01 CST 1970
}
public static void demo2() {
Date d1 = new Date();
System.out.println(d1.getTime()); //通过时间对象获取毫秒值
System.out.println(System.currentTimeMillis());//通过系统类的方法获取当前时间毫秒值
}
public static void demo1() {
Date d1 = new Date(); //如果没有传参数代表的是当前时间
System.out.println(d1); //会打印出当前时间
Date d2 = new Date(0); //如果构造方法中参数传为0代表的是1970年1月1日
System.out.println(d2); //Thu Jan 01 08:00:00 CST 1970 通过毫秒值创建时间对象
}
DateFormat是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat成员方法:public final String format(Date date):将一个没有格式的时间对象格式化成一个有格式的代表时间的一个字符串public Date parse(String source):将一个有格式的代表时间的一个字符串 解析 成一个时间对象
Date -->String Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String time = sdf.format(d);String --> Date String s ="2016年" SimpleDateFormat sdf = new SimpleDateFormat("yyyy年"); Date d = sdf.parse(s);
Calendar类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
public static Calendar getInstance(): 根据你的电脑所在的时区 获取Calendar的一个对应当前时区的子类对象public int get(int field):返回给定日历字段的值。public void add(int field,int amount):根据日历的规则,为给定的日历字段添加或减去指定的时间量。public final void set(int year,int month,int date):设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。
代码演示:
public static void main(String[] args) {
//demo1();
Calendar c = Calendar.getInstance(); //父类引用指向子类对象
//c.add(Calendar.MONTH, -1); //对指定的字段进行向前减或向后加
//c.set(Calendar.YEAR, 2000); //修改指定字段
c.set(2000, 7, 8);
}
public static void demo1() {
Calendar c = Calendar.getInstance(); //父类引用指向子类对象
//System.out.println(c); //java.util.GregorianCalendar[time=1454155709073,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=0,WEEK_OF_YEAR=5,WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=30,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=8,SECOND=29,MILLISECOND=73,ZONE_OFFSET=28800000,DST_OFFSET=0]
System.out.println(c.get(Calendar.YEAR)); //通过字段获取年
System.out.println(c.get(Calendar.MONTH)); //通过字段后期月,但是月是从0开始编号的
System.out.println(c.get(Calendar.DAY_OF_MONTH)); //月中的第几天
System.out.println(c.get(Calendar.DAY_OF_WEEK)); //周日是第一天,周六是最后一天
}
想了解更多学习知识,请关注微信公众号“阿Q说”,获取更多学习资料吧!你也可以后台留言说出你的疑惑,阿Q将会在后期的文章中为你解答。每天学习一点点,每天进步一点点。