课堂笔记 常用类API

常用类API

掌握System系统相关类的使用

System类是一个final类,同时该类不能被实例化。

System类定义时使用final修饰,同时构造方法被设计为private
所以System类的属性和方法都被设计为static
属性 in out err

System.exit(0);

关闭虚拟机 非零状态码表示异常关闭

System.gc();

调用垃圾回收器

System.currentTimeMillis();

获取1970-01-01到现在的时间毫秒数
其主要用于:计算程序运行所需的时间
long start=System.currentTimeMillis();
long end=System.currentTimeMillis();
算法
System.out.println(end-start);

*System.arraycopy();

src-源数组
srcPos-源数组中的起始位置
dest-目标数组
destPos-目标数据中的起始位置
length-要复制的数组元素的数量

掌握数学类的使用

Math.abs();

获取一个数的绝对值

Math.max();

获取两个数的最大值

Math.min();

获取两个数的最小值

*Math.random();

生成0-1之间的double随机数

理解包装类及自动装箱/拆箱

包装类

基本数据类型(4类8种)与 引用数据类型的区别
基本数据类型的变量空间中存放的是值本身
引用数据类型的变量空间中存放的是真正数据的引用,该引用指向真正的数据

包装类本质上是基本数据类型与引用数据类型之间进行转换的桥梁

八大基本数据类型经过包装之后,成为了引用数据类型
byte—Byte类
short—Short类
int—Integer类
long—Long类
float—Float类
double—Double类
char—Character类
boolean–Boolean

字符串转基本数据类型的方法
包装类名.parseXXXX(String str)
Integer.parseInt(“111111”)
Double.parseDouble(“1111.2”)

手动装箱

public void test() {
        int i = 10;
    // 手动装箱
        Integer a = new Integer(i);
    // 手动拆箱
        int i1 = a.intValue();
        System.out.println(i1);
}

自动装箱

public void test() {
    // 自动装箱,底层使用的是Integer.valueOf(10)方法,返回一个Integer对象
       Integer a = 10;
    // 自动拆箱,底层使用的是Integer.intValue()方法
       int b = a;
}

数据类型Value()

获取包装类中封装的数据

类名.parse基本数据类型*(字符串)

将字符串转成对应的基本数据类型

  • Integer,Byte,Short,Long 装箱缓存范围是 -128 ~ 127
        Integer i1=100;
        Integer i2=100;
        Integer i3=200;
        Integer i4=200;
        System.out.println(i1 == i2);
        System.out.println(i3 == i4);
打印结果:true;
        false;
        
        Integer i1=100;
        Integer i2=100;
        Integer i3=200;
        Integer i4=200;
        System.out.println(i1 == i2);
        System.out.println(i3 .equals i4);
打印结果:true;
        true;

掌握字符串处理相关类的使用

String字符串类

.char()
.length()
.indexOf() 得到子串第一次出现的下标
.lastIndexOf() 得到子串最后一次出现的下标
.trim() 去掉字符串前后的空格
.replace() 将指定的子串替换为新的子串
.split() 将字符串按指定的子串拆分成数组
.getByte() 将字符串转化为字节数组

String的特性

  1. String是唯一一个设计了字面常量的引用数据类型
  2. String拥有一个“字符串常量池”的设计
  3. 判断字符串非空,应该判断两个条件:
    str!=null
    !" ".equals(str)
  4. String对象的值一旦确定,不能改变

两种得到字符串对象的方式:

//从常量池获取
String str="hello world";
//从常量池拷贝到空间中,再从堆空间中获取
String str=new String("hello world");

StringBuffer和StringBuilder

由于String内容不可变,那么我们在做字符串操作时,会在内存中产生很多无用的字符串对象,导致性能受到影响,所以就出现了StringBuffer和StringBuilder类,内容就是可变的。
两者区别:StringBuffer线程安全,StringBuilder不安全
目前关于线程安全结论:线程安全性能低,线程不安全性能高
性能:StringBulider>StringBuffer>String

菜鸟教程:
https://www.runoob.com/java/java-stringbuffer.html

        StringBuilder sb=new StringBuilder(10);
        System.out.println(sb.append("woshiyx.."));
        System.out.println(sb.append("oo"));
        System.out.println(sb.insert(7,"okkk"));
        System.out.println(sb.delete(11,13));
        
        //控制台结果
        woshiyx..
        woshiyx..oo
        woshiyxokkk..oo
        woshiyxokkkoo

理解正则表达式

正则表达式是使用特定的符号来描述字符串的一种方法。是数据验证的主要手段。
正则表达式本身也是字符串,可以验证指定的字符串,是否匹配该正则表达式描述的格式。
正则表达式使用String的matches( )进行匹配。

正则表达式主要作用是做数据的格式验证
需要掌握的规则:
1.三种括号
2.6-7个特殊符号

正则表达式的语法规则

  • [ ]:匹配[ ]中描述的任意字符
表达式描述
[013]单个数字 0, 1, 或 3.
[0-9][0-9]单个数字 0, 1, 或 3.
A[0-4]b[05]由四个字符组成的字符串. 第一个字符是 A. 第二个字符是一个0到4之间的数字 ,第三个字符是b. 最后一个字符是 0 或 5.
  • ()和| 描述多个字符的选择范围
  • {} 描述前一个规则可以出现的次数
    X{n} 该规则匹配n次
    X{n,} 该规则至少匹配n次,无上限
    X{n,m} 该规则匹配n~m次

正则表达式的特殊符号

表达式描述
+等价于{1,}。最少出现一次,没有上限限制
*等价于{0,}。可以不出现,也可以出现多次
.匹配任意的单个字符,除开\r和\n
?等价于{0,1},可以不出现,要出现只能出现一次
\d(\D)等价于[0-9]。匹配数字。\D为非数字
\w(\W)等价于[a-zA-Z0-9_]。匹配合法标识符。\W为非合法标识符
\s(\S)匹配空格和回车。\S为非空格和回车
\u4e00-\u9fa5匹配汉字(要放入中括号)
  • 正则表达式使用matches进行匹配
练习:
package com.practice;

public class Practice2 {
    /*
    假设有一个注册用户的系统
    需要提供的信息为
    用户名/密码/电话号码
    用户名:要求只能使用数字/字母/和. 长度无限制但至少为1
    密码:要求只能使用数字/字母/_/$,长度为6-12
    电话号码:要求是13/15/17/19开头的11位数字
     */
public static void main(String[] args) {
    String usn="[0-9a-zA-Z.]+";//"([\\d]|[a-zA-Z]|\\.)+";
    System.out.println("ahdka.231".matches(usn));
    String psd="[\\w_$]{6,12}";
    System.out.println("eweuwa_$da".matches(psd));
    String pnm="[1][3579]\\d{9}";//"[1](3|5|7|9)\\d{9}";//"(13|15|17|19)\\d{9}";
    System.out.println("17993131434".matches(pnm));
}

}

掌握日期时间相关类的使用

了解计算机对日期的存储本质
在计算机中时间本质上是一个Long型数字,表示1970-01-01:00:00:00到现在的时间毫秒数

第一代日期类 Date类 DateFormat类

 public static void main(String[] args) {
        Date date=new Date();
        System.out.println();
        //年
        System.out.println((date.getYear()+1900)+"年");
        //月
        System.out.println((date.getMonth()+1)+"月");
        //日
        System.out.println(date.getDate()+"日");
        //日期格式化
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
    }

第二代日期类 Calendar类

Date类不能转换时区且难以做日期计算,所以有了Calendar(日历类)

  • 代码示例
public class case1 {
    public static void main(String[] args) {
 
    Calendar time = Calendar.getInstance();
 
        //输出年
    System.out.print("今天是" + time.get(Calendar.YEAR) + "年"); 
        //输出月
    System.out.print( (time.get(Calendar.MONTH)+1) + "月");     
        //输出日           
    System.out.print(time.get(Calendar.DAY_OF_MONTH) + "日");   
 
        //输出小时
    System.out.println("小时:" ++time.get(Calendar.HOUR));   
        //输出分钟
    System.out.println("分钟:" + time.get(Calendar.MINUTE)); 
        //秒
    System.out.println("秒:" + time.get(Calendar.SECOND));
    
        //输出星期
        System.out.println("今天是星期" + (time.get(Calendar.DAY_OF_WEEK)-1));
    }
}

*第三代日期类 LocalDate LocalTime LocalDateTime

LocalDate :只包含(日期/年月日),可以获取日期字段
LocalTime :只包含(时间/时分秒), 可以获取时间字段LocalDateTime :包含(日间/年月日时分秒),可以获取日期和时间字段

  • 使用 now() 获取当前日期对象
//年月日时分秒
LocalDateTime p1 = LocalDateTime.now(); 
 
//年月日
LocalDate p2 = LocalDate.now();
 
//时分秒
LocalTime p3 = LocalTime.now();
  • 使用 DateTimeFormatter 对象来进行格式化
LocalDateTime p = LocalDateTime.now(); 
 
// 创建 DateTimeFormatter 对象
DateTimeFormatter time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
String format = time.format(p);
 
System.out.println("格式化的日期=" + format);
  • 分别获取年、月、日、时、分、秒
LocalDateTime p = LocalDateTime.now(); 
 
System.out.println("年=" + p.getYear());
System.out.println("月=" + p.getMonth());
System.out.println("月=" + p.getMonthValue());
System.out.println("日=" + p.getDayOfMonth());
System.out.println("时=" + p.getHour());
System.out.println("分=" + p.getMinute());
System.out.println("秒=" + p.getSecond());
  • 用plus方法对时间进行加
LocalDateTime p = LocalDateTime.now(); 
 
//在当前日期上加366天
LocalDateTime loctime = p.plusDays(366);
 
System.out.println("366天后=" + dateTimeFormatter.format(loctime));
  • 用minus方法对时间进行减
LocalDateTime p = LocalDateTime.now(); 
 
//在30000分钟前是什么时候
LocalDateTime loctime = p.minusMinutes(30000);
System.out.println("30000分钟前的时间=" + dateTimeFormatter.format(loctime));

now() 获取当前时间
of() 通过参数得到指定时间对象
parse() 通过字符串得到指定时间对象

PS:通过字符串转换时,该字符串需要满足时间格式
2023-04-13
15:55:52.194481
2023-04-13T15:55:52.194546

*ChronoUnit时间计算

package com.lty;
 
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
 
public class ChronoUnitDemo {
    public static void main(String[] args) {
        LocalDateTime today = LocalDateTime.now();
        LocalDateTime birthDate = LocalDateTime.of(2000, 5, 21, 0, 0, 0, 0);
 
        System.out.println("相差的年数: " + ChronoUnit.YEARS.between(birthDate,today));
        System.out.println("相差的月数: " + ChronoUnit.MONTHS.between(birthDate,today));
        System.out.println("相差的周数: " + ChronoUnit.WEEKS.between(birthDate,today));
        System.out.println("相差的天数: " + ChronoUnit.DAYS.between(birthDate,today));
        System.out.println("相差的时数: " + ChronoUnit.HOURS.between(birthDate, today));
        System.out.println("相差的分数: " + ChronoUnit.MINUTES.between(birthDate, today));
        System.out.println("相差的秒数: " + ChronoUnit.SECONDS.between(birthDate,today));
        System.out.println("相差的毫秒数: " + ChronoUnit.MILLIS.between(birthDate,today));
        System.out.println("相差的微秒数: " + ChronoUnit.MICROS.between(birthDate,today));
        System.out.println("相差的纳秒数: " + ChronoUnit.NANOS.between(birthDate, today));
        System.out.println("相差的半天数: " + ChronoUnit.HALF_DAYS.between(birthDate, today));
        System.out.println("相差的十年数: " + ChronoUnit.DECADES.between(birthDate,today));
        System.out.println("相差的世纪有年数: " + ChronoUnit.CENTURIES.between(birthDate,today));
        System.out.println("相差的千年数: " + ChronoUnit.MILLENNIA.between(birthDate,today));
        System.out.println("相差的纪元数: " + ChronoUnit.ERAS.between(birthDate,today));
 
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值