Java中的一些常用类

目录

目录

1:String类

1.1:equals方法:

1.2:substring方法:字符串截断

1.3:split方法:

1.4:indexOf方法:

1.5:replace方法:

1.6:trim方法

2:StringBuffer类

2.1:append方法:

2.2:insert方法

2.3:delete方法

2.4:replace方法

2.5:substring方法

2.6:reverse方法

3:object类

4:Math类

 5:Random类

5.1:nextBoolean方法:随机生成一个布尔类型的值。

5.2:nextDouble方法:用于生成一个0-1(不包括1)之间的随机小数。

5.3:nextInt(int end)方法:用于生成一个0至指定值的随机整数,不包括liu

6:Date类:获取日期

6.1:getTime方法

6.2:toLocaleString方法

7:SimpleDateFormat类:日期,时间格式化类

7.1:format方法

7.2:parse方法

8:Calendar类




1:String类

1.1:equals方法:

比较两个字符串是否相等,如果相等则返回值为true,反之,返回false。

    @Test
    public void test1(){
        String str1="Hello";
        String str2="hello";
        System.out.println(str1.equals(str2));
    }

1.2:substring方法:字符串截断

用法:str.substring(first,last)包含first,不包含last。

           str.substring(first)从first开始到结束

           索引从0开始。

    @Test
    public void test2(){
        String str="HelloJavaWord";//6-9
        System.out.println(str.substring(9,13));
        System.out.println(str.substring(9));
    }

1.3:split方法:

字符串拆分,可以将其保存在数组中。

    @Test
    public void test3(){
        String str="北京,上海,杭州,深圳";
       String[] arr= str.split(",");
       for(String s:arr){
           System.out.println(s);
       }
    }

1.4:indexOf方法:

字符串查找,返回值为指定字符串在此字符串中首次出现的位置,如果没有查找到,返回值为-1

    @Test
    public void test4(){
        String str="HelloJavaWorld";
        int a=str.indexOf("Java");
        System.out.println(a);
    }

1.5:replace方法:

字符串替换方法:replace(替换前,替换后)

    @Test
    public void test5(){
        String str="555555555845@qq.com";
        String str2=str.replace("qq","163");
        System.out.println(str2);
    }

1.6:trim方法

字符串去掉左右空格!

    @Test
    public void test6(){
        String str="    555555555845@qq.com    ";
        System.out.println(str.trim());
    }

2:StringBuffer类

2.1:append方法:

向原有字符末尾添加新的字符或者字符串。

    @Test
    public void test01(){
//        String  str=new String("hello");
        StringBuffer str=new StringBuffer("hello");
        str.append("World");
        System.out.println(str);
    }

2.2:insert方法

将字符(或字符串)插入到该序列的指定位置

    @Test
    public void test02(){
        StringBuffer str=new StringBuffer("HelloWorld");
        str.insert(5,"Java");
        System.out.println(str);
    }

2.3:delete方法

删除指定位置的字符字符串

    @Test
    public void test03(){
        StringBuffer str=new StringBuffer("HelloWorld");
        str.delete(5,10);
        System.out.println(str);
    }

2.4:replace方法

替换该序列指定位置的字符串

str.replace(开始,结束,替换字符)

    @Test
    public void test04(){
        StringBuffer str=new StringBuffer("HelloWorld");
        str.replace(5,10,"Java");
        System.out.println(str);
    }

2.5:substring方法

字符串的截取,返回一个替换好的String值,注意:不会改变原有字符串,只会生成新的字符串

    @Test
    public void test06(){
        StringBuffer str=new StringBuffer("HelloWorld");
       String s= str.substring(5,10);
        System.out.println(s);
    }

2.6:reverse方法

反转

    @Test
    public void test07(){
        StringBuffer str=new StringBuffer("HelloWorld");
        System.out.println(str.reverse());
    }

StringBuffer类与StringBuilder的区别:

StringBuffer是线程安全的,StringBuilder是线程不安全的,但是StringBuilder的效率更高! 

3:object类

getclass方法:获得指定对象运行时的类型,包名加类名

hashcode方法:获得指定对象的hashcode值

    @Test
    public void test001(){
        StringBuffer sb=new StringBuffer("Hello");
        System.out.println(sb.getClass());//获取指定对象运行时的类型,包名加类名
        System.out.println(sb.hashCode());//获得指定对象的hashcode
    }

4:Math类

4.1:PI圆周率

    @Test
    public void test0002(){
        System.out.println(Math.PI);
    }

4.2:floor小数取整,向下取整

    @Test
    public void test0004(){
        System.out.println(Math.floor(4.9));
    }

4.2:ceil小数取整,向上取整

    @Test
    public void test0006(){
        System.out.println(Math.ceil(4.1));
    }

4.3:round小数取整,四舍五入

    @Test
    public void test0008(){
        System.out.println(Math.round(4.9));
        System.out.println(Math.round(4.2));
    }

4.4:random从0-1中随机取小数,包含0,不包含1

    @Test
    public void test0009(){
        for (int i = 0; i <10 ; i++) {
            System.out.println(Math.random());
        }

    }

 5:Random类

5.1:nextBoolean方法:随机生成一个布尔类型的值。

    @Test
    public void test11(){
        Random r=new Random();
        for (int i = 0; i <10 ; i++) {
            System.out.println(r.nextBoolean());
        }
    }

5.2:nextDouble方法:用于生成一个0-1(不包括1)之间的随机小数。

    @Test
    public void test12(){
        Random r=new Random();
        for (int i = 0; i <10 ; i++) {
            System.out.println(r.nextDouble());
        }
    }

5.3:nextInt(int end)方法:用于生成一个0至指定值的随机整数,不包括liu

    @Test
    public void test13(){
        Random r=new Random();
        for (int i = 0; i <10 ; i++) {
            System.out.println(r.nextInt(10));
        }
    }

6:Date类:获取日期

6.1:getTime方法

获取当前时间的毫秒数,从1970年1月1日开始。

    @Test
    public void test20(){
        Date d=new Date();
        System.out.println(d.getTime());
    }

6.2:toLocaleString方法

获取当地时间,返回值是一个字符串

    @Test
    public void test21(){
        Date d=new Date();
        System.out.println(d.toLocaleString());//在当前jdk中不建议使用,被标记成废弃方法。
    }

7:SimpleDateFormat类:日期,时间格式化类

7.1:format方法

格式化日期(yyyy-MM-dd hh:mm:ss)

    @Test
    public void test14(){
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss");
        System.out.println(sdf.format(d));
    }

7.2:parse方法

将字符串转化为日期

    @Test
    public void test15() throws ParseException {
        String str="2021-08-19 04:09:01";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date d=sdf.parse(str);
        System.out.println(d);
    }

8:Calendar类

日期,时间处理的抽象类

通过get方法获得指定日期。

YEAR,MONTH,DATE,DAY-OF-WEEK,DAY-OF-MONTH,WEEK-OF-MONTH,WEEK-OF-YEAR

    @Test
    public void test26(){
        Calendar c=Calendar.getInstance();//实例化Calendar对象
        System.out.println(c.get(Calendar.YEAR));//查询现在时刻是在哪一年
        System.out.println(c.get(Calendar.MARCH)+1);//查询现在时刻在哪一月(从0开始查)
        System.out.println(c.get(Calendar.DATE));
        System.out.println(c.get(Calendar.DAY_OF_WEEK));//从周日开始
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值