今日内容介绍
1、正则表达式的定义及使用
2、date类的用法
3、calendar类的用法
==========================================第一阶段========================================
* a: 正则表达式匹配练习 * a: 案例代码 public class regexdemo { public static void main(string[] args) { checktel(); } /* * 检查手机号码是否合法 * 1开头 可以是34578 0-9 位数固定11位 */ public static void checktel(){ string telnumber = "1335128005"; //string类的方法matches boolean b = telnumber.matches("1[34857][\\d]{9}"); system.out.println(b); } /* * 检查qq号码是否合法 * 0不能开头,全数字, 位数5,10位 * 123456 * \\d \\d匹配不是数字 */ public static void checkqq(){ string qq = "123456"; //检查qq号码和规则是否匹配,string类的方法matches boolean b = qq.matches("[1-9][\\d]{4,9}"); system.out.println(b); } }
* a: 正则表达式切割练习 * a: 案例代码 public class regexdemo1 { public static void main(string[] args) { split_1(); split_2(); split_3(); } /* * string类方法split对字符串进行切割 * 192.168.105.27 按照 点切割字符串 */ public static void split_3(){ string ip = "192.168.105.27"; string[] strarr = ip.split("\\."); system.out.println("数组的长度"+strarr.length); for(int i = 0 ; i < strarr.length ; i++){ system.out.println(strarr[i]); } } /* * string类方法split对字符串进行切割 * 18 22 40 65 按照空格切割字符串 */ public static void split_2(){ string str = "18 22 40 65"; string[] strarr = str.split(" +"); system.out.println("数组的长度"+strarr.length); for(int i = 0 ; i < strarr.length ; i++){ system.out.println(strarr[i]); } } /* * string类方法split对字符串进行切割 * 12-25-36-98 按照-对字符串进行切割 */ public static void split_1(){ string str = "12-25-36-98"; //按照-对字符串进行切割,string类方法split string[] strarr = str.split("-"); system.out.println("数组的长度"+strarr.length); for(int i = 0 ; i < strarr.length ; i++){ system.out.println(strarr[i]); } } }
* a: 正则表达式替换练习 * a: 案例代码 public class regexdemo1 { public static void main(string[] args) { replaceall_1(); } /* * "hello12345world6789012"将所有数字替换掉 * string类方法replaceall(正则规则,替换后的新字符串) */ public static void replaceall_1(){ string str = "hello12345world6789012"; str = str.replaceall("[\\d]+", "#"); system.out.println(str); } }
* a: 正则表达式邮箱地址验证 * a: 案例代码 public class regexdemo2 { public static void main(string[] args) { checkmail(); } /* * 检查邮件地址是否合法 * 规则: * 1234567@qq.com * mym_ail@sina.com * nimail@163.com * wodemail@yahoo.com.cn * * @: 前 数字字母_ 个数不能少于1个 * @: 后 数字字母 个数不能少于1个 * .: 后面 字母 * */ public static void checkmail(){ string email ="abc123@sina.com"; boolean b = email.matches("[a-za-z0-9_]+@[0-9a-z]+(\\.[a-z]+)+"); system.out.println(b); } }
* a: date类的构造方法 * a: 空参构造 * public date() * b: 带参构造 * public date(long times)
==============================第二阶段====================================
* a: calendar类_2 * a: 成员方法 * gettime() 把日历对象,转成date日期对象 * get(日历字段) 获取指定日历字段的值 * b: 代码演示 calendar c = calendar.getinstance(); // 获取年份 int year = c.get(calendar.year); // 获取月份 int month = c.get(calendar.month) + 1; // 获取天数 int day = c.get(calendar.day_of_month); system.out.println(year + "年" + month + "月" + day + "日");
* a: calendar类_3 * a: 成员方法 * set(int field,int value) 设置指定的时间 * b: 代码演示 /* * calendar类的set方法 设置日历 set(int field,int value) field 设置的是哪个日历字段 value * 设置后的具体数值 * * set(int year,int month,int day) 传递3个整数的年,月,日 */ public static void function_1() { calendar c = calendar.getinstance(); // 设置,月份,设置到10月分 // c.set(calendar.month, 9); // 设置年,月,日 c.set(2099, 4, 1); // 获取年份 int year = c.get(calendar.year); // 获取月份 int month = c.get(calendar.month) + 1; // 获取天数 int day = c.get(calendar.day_of_month); system.out.println(year + "年" + month + "月" + day + "日"); }
* a: calendar类_4 * a: 成员方法 * add(int field, int value) 进行整数的偏移 * int get(int field) 获取指定字段的值 * b: 案例演示 /* * calendar类方法add 日历的偏移量, * 可以指定一个日历中的字段, * 进行整数的偏移 add(int field, int value) */ public static void function_2() { calendar c = calendar.getinstance(); // 让日历中的天数,向后偏移280天 c.add(calendar.day_of_month, -280); // 获取年份 int year = c.get(calendar.year); // 获取月份 int month = c.get(calendar.month) + 1; // 获取天数 int day = c.get(calendar.day_of_month); system.out.println(year + "年" + month + "月" + day + "日"); }
* a: 日期练习_活了多少天 * a: 案例代码 /* * 计算活了多少天 * 生日 今天的日期 * 两个日期变成毫秒值,减法 */ public static void function() throws exception { system.out.println("请输入出生日期 格式 yyyy-mm-dd"); //获取出生日期,键盘输入 string birthdaystring = new scanner(system.in).next(); //将字符串日期,转成date对象 //创建simpledateformat对象,写日期模式 simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); //调用方法parse,字符串转成日期对象 date birthdaydate = sdf.parse(birthdaystring); //获取今天的日期对象 date todaydate = new date(); //将两个日期转成毫秒值,date类的方法gettime long birthdaysecond = birthdaydate.gettime(); long todaysecond = todaydate.gettime(); long secone = todaysecond-birthdaysecond; if(secone < 0){ system.out.println("还没出生呢"); } else{ system.out.println(secone/1000/60/60/24); } }
* a: 日期练习_闰年计算 * a: 案例代码 /* * 闰年计算 * 2000 3000 * 高级的算法: 日历设置到指定年份的3月1日,add向前偏移1天,获取天数,29闰年 */ public static void function_1(){ calendar c = calendar.getinstance(); //将日历,设置到指定年的3月1日 c.set(2088, 2, 1); //日历add方法,向前偏移1天 c.add(calendar.day_of_month, -1); //get方法获取天数 int day = c.get(calendar.day_of_month); system.out.println(day); }
如对本文有疑问, 点击进行留言回复!!
荐 嵌入式linux开发环境搭建(Docker版,基于iTop 4412开发板)
SpringBoot项目部署到服务器之 Liunx环境安装jdk详细步骤
网友评论