String,Object类,时间相关API

常用API

一、String

  • String代表字符串,可以用来创建对象封装字符串数据,并对其进行处理。

String类创建对象封装字符串数据的方式

  • 方式一: 直接使用双引号“…” 。
  • 方式二:new String类,调用构造器初始化字符串对象
构造器说明
public String()创建一个空白字符串对象,不含有任何内容
public String(String original)根据传入的字符串内容,来创建字符串对象
public String(char[] chars)根据字符数组的内容,来创建字符串对象
public String(byte[] bytes)根据字节数组的内容,来创建字符串对象
// 1、直接双引号得到字符串对象,封装字符串数据
String name = "晚上一个饼";
System.out.println(name);

// 2、new String创建字符串对象,并调用构造器初始化字符串
String rs1 = new String();
System.out.println(rs1); // ""

byte[] bytes = {97, 98, 99};
String rs4 = new String(bytes);
System.out.println(rs4);

String类的常用方法

方法名说明
public int length()获取字符串的长度返回(就是字符个数)
public char charAt(int index)获取某个索引位置处的字符返回
public char[] toCharArray():将当前字符串转换成字符数组返回
public boolean equals(Object anObject)判断当前字符串与另一个字符串的内容一样,一样返回true
public boolean equalsIgnoreCase(String anotherString)判断当前字符串与另一个字符串的内容是否一样(忽略大小写)
public String substring(int beginIndex, int endIndex)根据开始和结束索引进行截取,得到新的字符串(包前不包后)
public String substring(int beginIndex)从传入的索引处截取,截取到末尾,得到新的字符串返回
public String replace(CharSequence target, CharSequence replacement)使用新值,将字符串中的旧值替换,得到新的字符串
public boolean contains(CharSequence s)判断字符串中是否包含了某个字符串
public boolean startsWith(String prefix)判断字符串是否以某个字符串内容开头,开头返回true,反之
public String[] split(String regex)把字符串按照某个字符串内容分割,并返回字符串数组回来
String s = "晚上一个饼";

// 1、获取字符串的长度
System.out.println(s.length());

// 2、提取字符串中某个索引位置处的字符
char c = s.charAt(1);
System.out.println(c);

// 字符串的遍历
for (int i = 0; i < s.length(); i++) {
	// i = 0 1 2 3 4 5
	char ch = s.charAt(i);
	System.out.println(ch);
}

// 3、把字符串转换成字符数组,再进行遍历
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
	System.out.println(chars[i]);
}

// 4、判断字符串内容,内容一样就返回true
String s1 = new String("张三");
String s2 = new String("张三");
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true

// 5、忽略大小写比较字符串内容
String c1 = "34AeFG";
String c2 = "34aEfg";
System.out.println(c1.equals(c2)); // false
System.out.println(c1.equalsIgnoreCase(c2)); // true

// 6、截取字符串内容 (包前不包后的)
String s3 = "晚上一个饼是真的饱不了";
String rs = s3.substring(0, 6);
System.out.println(rs);

// 7、从当前索引位置一直截取到字符串的末尾
String rs2 = s3.substring(5);
System.out.println(rs2);

// 8、把字符串中的某个内容替换成新内容,并返回新的字符串对象
String info = "这个真的绝了";
String rs3 = info.replace("绝了", "**");
System.out.println(rs3);

// 9、判断字符串中是否包含某个关键字
String info2 = "晚上一个饼是真的饱不了";
System.out.println(info2.contains("晚上"));

// 10、判断字符串是否以某个字符串开头。
String rs4 = "张三";
System.out.println(rs4.startsWith("张"));

// 11、把字符串按照某个指定内容分割成多个字符串,放到一个字符串数组中返回
String rs5 = "张三,李四,王五,赵六";
String[] names = rs5.split(",");
for (int i = 0; i < names.length; i++) {
	System.out.println(names[i]);
}

注意

  • String类的对象是不可变的对象
  • 字符串字面量和new出来字符串的区别
    只要是以“...”方式写出的字符串对象,会存储到字符串常量池,且相同内容的字符串只存储一份。
    但通过new方式创建字符串对象,每new一次都会产生一个新的对象放在堆内存中。

二、StringBuilder

  • StringBuilder代表可变字符串对象,相当于是一个容器,它里面装的字符串是可以改变的,就是用来操作字符串的。
  • 好处:StringBuilder比String更适合做字符串的修改操作,效率会更高,代码也会更简洁。

构造器

构造器说明
public StringBuilder()创建一个空白的可变的字符串对象,不包含任何内容
public StringBuilder(String str)创建一个指定字符串内容的可变字符串对象

常用方法

方法名称说明
public StringBuilder append(任意类型)添加数据并返回StringBuilder对象本身
public StringBuilder reverse()将对象的内容反转
public int length()返回对象内容长度
public String toString()通过toString()就可以实现把StringBuilder转换为String
  • 对于字符串相关的操作,如频繁的拼接、修改等,建议用StringBuidler,效率更高

  • 注意:如果操作字符串较少,或者不需要操作,以及定义字符串变量,还是建议用String

// stringBuilder的用法和作用。
StringBuilders=new stringBuilder("zxlcb");
//1、排按内容
s.append(12);
s.append("张三");

System.out.println(s);

// 支持链式编程
s.append(666).append("发生的").append("dasdd");
System.out.printn(s):

// 2.反转操作
s.reverse();
System.out.println(s);

// 3.返回字符串的长度
System.out.println(s.length());

//4.把stringBuilder对象又转换成String类型
string rs =s.tostring();
System.out.println(rs):

三、StringBuffer

  • StringBuffer的用法与StringBuilder是一模一样的
  • StringBuilder是线程不安全的StringBuffer是线程安全的

四、StringJoiner

  • JDK8开始才有的,跟StringBuilder一样,也是用来操作字符串的,也可以看成是一个容器,创建之后里面的内容是可变的。

  • 好处:不仅能提高字符串的操作效率,并且在有些场景下使用它操作字符串,代码会更简洁

构造器

构造器说明
public StringJoiner (间隔符号)创建一个StringJoiner对象,指定拼接时的间隔符号
public StringJoiner (间隔符号,开始符号,结束符号)创建一个StringJoiner对象,指定拼接时的间隔符号、开始符号、结束符号

常用方法

方法名称说明
public StringJoiner add (添加的内容)添加数据,并返回对象本身
public int length()返回长度 ( 字符出现的个数)
public String toString()返回一个字符串(该字符串就是拼接之后的结果)

五、Object类

public String toString()
返回对象的字符串表示形式。默认的格式是:“包名.类名@哈希值16进制”
【子类重写后,返回对象的属性值】

public boolean equals(Object o)
判断此对象与参数对象是否"相等"。默认比较对象的地址值,和"=="没有区别
【子类重写后,比较对象的属性值】

public Object clone()
克隆当前对象,返回一个新对象
想要调用clone()方法,必须让被克隆的类实现Cloneable接口。
浅克隆
拷贝出的新对象,与原对象中的数据一模一样(引用类型拷贝的只是地址)
深克隆
对象中基本类型的数据直接拷贝。
对象中的字符串数据拷贝的还是地址。
对象中还包含的其他对象,不会拷贝地址,会创建新对象

六、Objects类

//先做非空判断,再比较两个对象
public static boolean equals(0bject a,0bject b)
//判断对象是否为null,为null返回true,反之
public static boolean isNull(Object obj)
//判断对象是否不为null,不为null 则返回true,反之
public static boolean nonNull(object obj)

public class Test{
    public static void main(String[] args){
        String s1 = null;
        String s2 = "zhangsan";
        
        //这里会出现NullPointerException异常,调用者不能为null
        System.out.println(s1.equals(s2));
        //此时不会有NullPointerException异常,底层会自动先判断空
        System.out.println(Objects.equals(s1,s2));
        
        //判断对象是否为null,等价于==
        System.out.println(Objects.isNull(s1)); //true
        System.out.println(s1==null); //true
        
        //判断对象是否不为null,等价于!=
        System.out.println(Objects.nonNull(s2)); //true
        System.out.println(s2!=null); //true
    }
}

七、包装类

自动装箱(自动将基本类型转换为引用类型)
自动拆箱(自动将引用类型转换为基本类型)

基本数据类型对应的包装类(引用数据类型)
byteByte
shortShort
intInteger
longLong
charCharacter
floatFloat
doubleDouble
booleanBoolean
ArrayList<Integer> list = new ArrayList<>();
//添加的元素是基本类型,实际上会自动装箱为Integer类型
list.add(100);
//获取元素时,会将Integer类型自动拆箱为int类型
int e = list.get(0);

包装类数据类型转换

把字符串转换为数值型数据:包装类.parseXxx(字符串)
public static int parseInt(String s)
把字符串转换为基本数据类型

将数值型数据转换为字符串:包装类.valueOf(数据);
public static String valueOf(int a)
把基本类型数据转换为

八、Math、System、Runtime**

Math

  • 代表数学,是一个工具类,里面提供的都是对数据进行操作的一些静态方法。

常见方法

方法名说明
public static int abs(int a)获取参数绝对值
public static double ceil(double a)向上取整
public static double floor(double a)向下取整
public static int round(float a)四舍五入
public static int max(int a,int b)获取两个int值中的较大值
public static double pow(double a,double b)返回a的b次幂的值
public static double random()返回值为double的随机值,范围[0.0,1.0)

System

  • System代表程序所在的系统,也是一个工具类

常见方法

方法名说明
public static void exit(int status)终止当前运行的Java虚拟机。
public static long currentTimeMillis()返回当前系统的时间毫秒值形式

Runtime

  • 代表程序所在的运行环境。

  • Runtime是一个单例类。

常用方法

方法名说明
public static Runtime getRuntime()返回与当前Java应用程序关联的运行时对象
public void exit(int status)终止当前运行的虚拟机
public int availableProcessors()返回Java虚拟机可用的处理器数。
public long totalMemory()返回Java虚拟机中的内存总量
public long freeMemory()返回Java虚拟机中的可用内存
public Process exec(String command)启动某个程序,并返回代表该程序的对象

九、BigDecimal

  • 用于解决浮点型运算时,出现结果失真的问题。

常见构造器

构造器说明
public BigDecimal(double val) 注意:不推荐使用这个将 double转换为 BigDecimal
public BigDecimal(String val)把String转成BigDecimal

常用方法

方法名说明
public static BigDecimal valueOf(double val)转换一个 double成 BigDecimal
public BigDecimal add(BigDecimal b)加法
public BigDecimal subtract(BigDecimal b)减法
public BigDecimal multiply(BigDecimal b)乘法
public BigDecimal divide(BigDecimal b)除法
public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式)除法、可以控制精确到小数几位
public double doubleValue()将BigDecimal转换为double

如何把浮点型转换成BigDecimal的对象

  • BigDecimal b1 = BigDecimal.valueOf(0.1)

十、时间相关

有LocalDate、LocalTime、以及LocalDateTime类

方法名示例
public static Xxxx now(): 获取系统当前时间对应的该对象LocaDate ld = LocalDate.now();
LocalTime lt = LocalTime.now();
LocalDateTime ldt = LocalDateTime.now();
public static Xxxx of(…):获取指定时间的对象LocalDate localDate1 = LocalDate.of(2099 , 11,11);
LocalTime localTime1 = LocalTime.of(9, 8, 59);
LocalDateTime localDateTime1 = LocalDateTime.of(2025, 11, 16, 14, 30, 01);

LocalDate:代表本地日期(年、月、日、星期)

方法名说明
public int geYear()获取年
public int getMonthValue()获取月份(1-12)
public int getDayOfMonth()获取日
public int getDayOfYear()获取当前是一年中的第几天
Public DayOfWeek getDayOfWeek()获取星期几:ld.getDayOfWeek().getValue()
方法名说明
withYear、withMonth、withDayOfMonth、withDayOfYear直接修改某个信息,返回新日期对象
plusYears、plusMonths、plusDays、plusWeeks把某个信息加多少,返回新日期对象
minusYears、minusMonths、minusDays,minusWeeks把某个信息减多少,返回新日期对象
equals isBefore isAfter判断两个日期对象,是否相等,在前还是在后

LocalTime:代表本地时间(时、分、秒、纳秒)

方法名说明
public int getHour()获取小时
public int getMinute()获取分
public int getSecond()获取秒
public int getNano()获取纳秒
方法名说明
withHour、withMinute、withSecond、withNano修改时间,返回新时间对象
plusHours、plusMinutes、plusSeconds、plusNanos把某个信息加多少,返回新时间对象
minusHours、minusMinutes、minusSeconds、minusNanos把某个信息减多少,返回新时间对象
equals isBefore isAfter判断2个时间对象,是否相等,在前还是在后

LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)

方法名说明
getYear、getMonthValue、getDayOfMonth、getDayOfYear getDayOfWeek、getHour、getMinute、getSecond、getNano获取年月日、时分秒、纳秒等
withYear、withMonth、withDayOfMonth、withDayOfYear withHour、withMinute、withSecond、withNano修改某个信息,返回新日期时间对象
plusYears、plusMonths、plusDays、plusWeeks plusHours、plusMinutes、plusSeconds、plusNanos把某个信息加多少,返回新日期时间对象
minusYears、minusMonths、minusDays、minusWeeks minusHours、minusMinutes、minusSeconds、minusNanos把某个信息减多少,返回新日期时间对象
equals isBefore isAfter判断2个时间对象,是否相等,在前还是在后

LocalDateTime的转换成LocalDate、LocalTime

方法名说明
public LocalDate toLocalDate()转换成一个LocalDate对象
public LocalTime toLocalTime()转换成一个LocalTime对象

ZoneId 时区的常见方法

方法名说明
public static Set getAvailableZoneIds()获取Java中支持的所有时区
public static ZoneId systemDefault()获取系统默认时区
public static ZoneId of(String zoneId)获取一个指定时区

ZonedDateTime带时区时间的常见方法

方法名说明
public static ZonedDateTime now()获取当前时区的ZonedDateTime对象
public static ZonedDateTime now(ZoneId zone)获取指定时区的ZonedDateTime对象
getYear、getMonthValue、getDayOfMonth、getDayOfYeargetDayOfWeek、getHour、getMinute、getSecond、getNano获取年月日、时分秒、纳秒等
public ZonedDateTime withXxx(时间)修改时间系列的方法
public ZonedDateTime minusXxx(时间)减少时间系列的方法
public ZonedDateTime plusXxx(时间)增加时间系列的方法
//ZonedDateTime:带时区的时间。
// public static ZonedDateTime now(ZoneId zone): 获取某个时区的ZonedDateTime对象。
ZonedDateTime now = ZonedDateTime.now(zoneId1);
System.out.println(now);

// 世界标准时间了
ZonedDateTime now1 = ZonedDateTime.now(Clock.systemUTC());
System.out.println(now1);

// public static ZonedDateTime now():获取系统默认时区的ZonedDateTime对象
ZonedDateTime now2 = ZonedDateTime.now();
System.out.println(now2);

Instant 时间线上的某个时刻/时间戳

  • 代替Date

  • 通过获取Instant的对象可以拿到此刻的时间,该时间由两部分组成:从1970-01-01 00:00:00 开始走到此刻的总秒数 + 不够1秒的纳秒数

方法名说明
public static Instant now()获取当前时间的Instant对象(标准时间)
public long getEpochSecond()获取从1970-01-01T00:00:00开始记录的秒数。
public int getNano()从时间线开始,获取从第二个开始的纳秒数
plusMillis plusSeconds plusNanos判断系列的方法
minusMillis minusSeconds minusNanos减少时间系列的方法
equals、isBefore、isAfter增加时间系列的方法
  • 作用:可以用来记录代码的执行时间,或用于记录用户操作某个事件的时间点。

  • 传统的Date类,只能精确到毫秒,并且是可变对象;

  • Instant类,可以精确到纳秒,并且是不可变对象,推荐用Instant代替Date。

DateTimeFormatter

  • 线程安全,代替了SimpleDateFormat类
方法名说明
public static DateTimeFormatter ofPattern(时间格式)获取格式化器对象
public String format(时间对象)格式化时间
// 1、创建一个日期时间格式化器对象出来。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

// 2、对时间进行格式化
LocalDateTime now = LocalDateTime.now();
System.out.println(now);

LocalDateTime提供的格式化、解析时间的方法

方法名说明
public String format(DateTimeFormatter formatter)格式化时间
public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)解析时间

Period(一段时期)

可以用于计算两个 LocalDate对象 相差的年数、月数、天

方法名说明
public static Period between(LocalDate start, LocalDate end)传入2个日期对象,得到Period对象
public int getYears()计算隔几年,并返回
public int getMonths()计算隔几个月,年返回
public int getDays()计算隔多少天,并返回

Duration(持续时间)

l可以用于计算两个时间对象相差的天数、小时数、分数、秒数、纳秒数;支持LocalTime、LocalDateTime、Instant等时间。

方法名说明
public static Duration between(开始时间对象1,截止时间对象2)传入2个时间对象,得到Duration对象
public long toDays()计算隔多少天,并返回
public long toHours()计算隔多少小时,并返回
public long toMinutes()计算隔多少分,并返回
public long toSeconds()计算隔多少秒,并返回
public long toMillis()计算隔多少毫秒,并返回
public long toNanos()计算隔多少纳秒,并返回
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值