今日内容介绍
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);
}
16_常用API_第16天(正则表达式、Date、DateFormat、Calendar)
原文:https://www.cnblogs.com/zoick/p/10590425.html