Java中的常用类(三)

一、正则表达式

正则表达式 regex,全称Regular Expression。正则表达式是一种规则(模式)匹配语法
可以使用一些正则表达式中的特殊符号来定义一种规则,然后用此规则匹配某个字符,如果字符串与规则匹配则返回true,不匹配返回false。
其常用方法如下:
\\d 匹配数字 \\D匹配非数字 \\w 匹配数字字母下划线  \\W 匹配非数字 字母 下划线
\\s匹配空格 \\非空格 |逻辑或 .匹配任意字符 使用时需要转义 []表示范围 {}表示数量
*零或多 +一或多 ?最多1

大多常用情景可以在网上搜索生成拿来使用,本文提供相关网站为

https://www.sojson.com/regex/generate    生成regex
        String s="a";

        System.out.println(s.matches("b"));//定义s里是否有b

        /*//数字*/
        String s1="123456";
        //定义s里只能有数字,但是数字只能有一次,[]表示范围
        System.out.println(s1.matches("[0-9]"));
        //*表示允许零次或多次出现数字
        System.out.println(s1.matches("[0-9]*"));
        //+表示至少一次出现数字
        System.out.println(s1.matches("[0-9]+"));
        //?表示至多一次出现数字
        System.out.println(s1.matches("[0-9]?"));
        //{num}必须有几次,本例中表示0-9必须有三位
        System.out.println(s1.matches("[0-9]{3}"));
        //第一位是1,后面三位数字
        System.out.println(s1.matches("1[0-9]{3}"));
        //{num1,num2}表示区间
        System.out.println(s1.matches("[0-9]{3,6}"));
        //只允许出现357
        System.out.println(s1.matches("[357]{3}"));

        //  \\d表示数字、\\D表示非数字
        System.out.println(s1.matches("\\d{3,6}"));
        System.out.println(s1.matches("\\D{3,6}"));


        //验证手机号
        String phoneNum="13691309195";
        System.out.println(phoneNum.matches("1[3578]\\d{9}"));


        /*//字母*/
        String eng ="abcdABCD";
        System.out.println(eng.matches("[a-z]*"));
        System.out.println(eng.matches("[A-Z]*"));
        System.out.println(eng.matches("[a-zA-Z]*"));
        //编码中A比a小
        System.out.println(eng.matches("[A-z]*"));

        //数字加字母加下划线
        System.out.println(eng.matches("\\w*"));//允许
        System.out.println(eng.matches("\\W*"));//不允许

        /*//空格*/
        String k =" ";
        System.out.println(k.matches("\\s"));

        //例子 :邮箱 xxxxxxx@xxx.com 或者xxxxxx@xxxx.com.cn
        String s3 = "1790352490@qq.com";
        System.out.println(
                s3.matches("\\w{6,10}" +
                        // .有特殊含义 需要转义
                        "@[0-9A-z]{2,6}\\.(com|com\\.cn)")
        );

二、Date类和Calendar 类

1、Date类

   Date类是 Java 中表示日期和时间的类,可以用来获取和设置日期和时间的各个部分,比如年、月、日、小时、分钟、秒等。Date 类也可以用于日期和时间的计算、比较和格式化。

方法:

1、构造方法:

Date date = new Date();

2、获取时间

System.out.println(date.getTime());

此时获取的时间是自1900年1月1日到构造方法运行时的毫秒数,通常需要定义1个long类型的变量来接收。

这时候有人就要说了,我要这个毫秒数有什么用呢?其实是有大用处的,当我们想要测试运行效率时,可以这样做

例如我们要测试String类和StringBuffer类各自拼接十万次的运行效率,可以这样

//要求:测试String字符串和StringBuffer字符串循环拼接10万次,程序运行耗时.    
    public static void main(String[] args) {
        String string =new String("abc");
        Date date1=new Date();
        for (int i=0;i<100000;i++){
            string +="a";
        }
        Date date2=new Date();
        System.out.println("String 拼接十万次用时:");
        System.out.println(date2.getTime()-date1.getTime());


        StringBuffer stringBuffer=new StringBuffer("abc");
        Date date3 =new Date();
        date3.getTime();
        for (int i=0;i<100000;i++){
            stringBuffer.append("a");
        }
        Date date4 =new Date();
        System.out.println("StringBuffer 拼接十万次用时:");
        System.out.println(date4.getTime()-date3.getTime());
    }

这时我们可以明显发现,StringBuffer拼接效率高于String类,这也源自与他底层没有final限制

2、toString方法

打印程序运行开始的那一时刻

3、getYear();getMonth();getDay();

这三个方法已启用需要自己手动“配平”

 System.out.println(date.getYear()+1900);//2024
        System.out.println(date.getMonth()+1);//3
        System.out.println(date.getDay());//6 显示周几

非常难用

所以在使用这些功能时,推荐使用Calendar类的方法

3、时间戳

//传入运行时的毫秒数可以转回具体时间  1709376357241称为时间戳
        Date date2 = new Date(1709376357241L);
        System.out.println(date2);//Sat Mar 02 18:45:57 CST 2024

当构造函数时传入时间戳时,可以返回获取时间戳时的时间

时间戳就是我们在getTime时获取的一大串毫秒数

2、Calendar类 

刚刚讲到,我们在用Date类获取时间时相当的麻烦,而且其相关方法都已经弃用了,这里Java给我们提供了更加方便的包,日历Calendar类

1、构造方法:

Calendar calendar=Calendar.getInstance();

2、相关方法

System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH)+1);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//今天是月的第几天
System.out.println(calendar.get(Calendar.DAY_OF_WEEK)-1);//是周的第几天,老外周日是第一天
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//年的第几天
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));//年的第几周
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH));//月的第几周

 //设置时间
calendar.set(year,month,date);

相当的方便

3、Math类与Random类

见名知意,这两个类中为我们提供了数学与随机数的相关方法

 System.out.println(Math.PI);//Π的值
 System.out.println(Math.abs(-1));//求绝对值
 System.out.println(Math.sqrt(9));//开方
 System.out.println(Math.pow(2,3));//n次幂
 System.out.println(Math.max(1,2));//返回最大值
 System.out.println(Math.floor(9.9));//向下取整
 System.out.println(Math.ceil(9.1));//向上取整
 System.out.println(Math.round(9.5));//四舍五入
 System.out.println(Math.random());//返回[0,1)之间的随机数

 

public class RandomDemo {
    static Random random=new Random();

    public static void main(String[] args) {
        System.out.println(random.nextBoolean());//布尔值随机数
        System.out.println(random.nextInt());//int型随机数
        System.out.println(random.nextLong());//long型随机数
        System.out.println(random.nextInt(5));//给定范围,[0,范围)随机整数

    }
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北京最后的深情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值