📃个人主页:个人主页
🔥系列专栏:JAVASE基础
目录
1.API概述
什么是API?
- API(Application Programming interface) 应用程序编程接口。
- 简单来说:就是Java帮我们已经写好的一些方法,我们直接拿过来用就可以了。

2.Object类
Object类的作用:
- 一个类要么默认继承了Object类,要么间接继承了Object类,Object类是Java中的祖宗类。
- Object作为所有类的父类,提供了很多常用的方法给每个子类对象拿来使用。
Object类的常用方法:
| 方法名 | 说明 |
| public String toString() | 默认是返回当前对象在堆内存中的地址信息: 类的全限名@内存地址 |
| public boolean equals(Object o) | 默认是比较当前对象与另一个对象的地址是否相同, 相同返回true,不同返回false |
Object的toString方法:
问题引出
- 开发中直接输出对象,默认输出对象的地址其实是毫无意义的。
- 开发中输出对象变量,更多的时候是希望看到对象的内容数据而不是对象的地址信息。
toString存在的意义
- 父类toString()方法存在的意义就是为了被子类重写,以便返回对象的内容信息,而不是地址信息!!
Object的equals方法:
问题思考
- 直接比较两个对象的地址是否相同完全可以用“==”替代equals。
- 同时,开发中很多业务情况下,更想判断2个对象的内容是否一样。
equals存在的意义
- 为了被子类重写,以便子类自己来定制比较规则(比如比较对象内容)。
3.Objects
Objects的常见方法:
| 方法名 | 说明 |
| public static boolean equals(Object a, Object b) | 比较两个对象的,底层会先进行非空判断, 从而可以避免空指针异常。再进行equals比较 |
| public static boolean isNull(Object obj) | 判断变量是否为null ,为null返回true ,反之 |
源码分析:
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
4.StringBuilder
StringBuilder概述
- StringBuilder是一个可变的字符串的操作类,我们可以把它看成是一个对象容器。
- 使用StringBuilder的核心作用:操作字符串的性能比String要更高(如拼接、修改等)。
String类拼接字符串原理图:【创建了多个对象】

StringBuilder提高效率原理图

结论:当需要进行字符串操作的时候,应该选择StringBuilder来完成,性能更好
StringBuilder 构造器
| 名称 | 说明 |
| public StringBuilder() | 创建一个空白的可变的字符串对象,不包含任何内容 |
| public StringBuilder(String str) | 创建一个指定字符串内容的可变字符串对象 |
StringBuilder常用方法
| 方法名称 | 说明 |
| public StringBuilder append(任意类型) | 添加数据并返回StringBuilder对象本身 |
| public StringBuilder reverse() | 将对象的内容反转 |
| public int length() | 返回对象内容长度 |
| public String toString() | 通过toString()就可以实现把StringBuilder转换为String |
为什么拼接、反转字符串建议使用StringBuilder?
- StringBuilder:内容是可变的、拼接字符串性能好、代码优雅。
- String :内容是不可变的、拼接字符串性能差。
- 定义字符串使用String
- 拼接、修改等操作字符串使用StringBuilder
5.日期与时间
Date 类
Date类代表当前所在系统的日期时间信息。
Date的构造器
| 名称 | 说明 |
| public Date() | 创建一个Date对象,代表的是系统当前此刻日期时间。 |
Date的常用方法
| 名称 | 说明 |
| public long getTime() | 返回从1970年1月1日 00:00:00走到此刻的总的毫秒数 |
时间毫秒值 -> 日期对象
| 构造器 | 说明 |
| public Date(long time) | 把时间毫秒值转换成Date日期对象。 |
| Date方法 | 说明 |
| public void setTime(long time) | 设置日期对象的时间为当前时间毫秒值对应的时间 |
案例
请计算出当前时间往后走1小时121秒之后的时间是多少。
public void test(){
Date date = new Date();
System.out.println(date);
//请计算出当前时间往后走1小时121秒之后的时间是多少。
Long dateTime =date.getTime()+(1*60*60+121)*1000;
date.setTime(dateTime);
System.out.println(date);
}

SimpleDateFormat
代表简单日期格式化,可以用来把日期时间格式化成为我们想要的形式

构造器
| 构造器 | 说明 |
| public SimpleDateFormat(String pattern) | 创建简单日期格式化对象,并封装格式化的形式信息 |
格式化方法
| 格式化方法 | 说明 |
| public final String format(Date date) | 将日期格式化成日期/时间字符串 |
| public final String format(Object time) | 将时间毫秒值式化成日期/时间字符串 |
格式化的时间形式的常用的模式对应关系如下:

SimpleDateFormat解析字符串时间成为日期对象
| 解析方法 | 说明 |
| public Date parse(String source) | 从给定字符串的开始解析文本以生成日期 |
public void test() throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
System.out.println(simpleDateFormat.format(date));
//请计算出当前时间往后走1小时121秒之后的时间是多少。
long time = date.getTime()+(1*60*60+121)*1000;
System.out.println(simpleDateFormat.format(time));
System.out.println("----------------------------------------------------------");
System.out.println("SimpleDateFormat解析字符串时间成为日期对象:");
Date parse = simpleDateFormat.parse("2022-01-04 12:30:30");
System.out.println(simpleDateFormat.format(parse));
//2分钟后:
Long date2=parse.getTime()+2*60*1000;
System.out.println(simpleDateFormat.format(date2));
}

Calendar
- Calendar代表了系统此刻日期对应的日历对象。
- Calendar是一个抽象类,不能直接创建对象。
Calendar如何去得到日历对象的?
public static Calendar getInstance() : 获取当前日历对象
Calendar日历类创建日历对象的方法:
| 方法名 | 说明 |
| public static Calendar getInstance() | 获取当前日历对象 |
Calendar常用方法
| 方法名 | 说明 |
| public int get(int field) | 取日期中的某个字段信息。 |
| public void set(int field,int value) | 修改日历的某个字段信息。 |
| public void add(int field,int amount) | 为某个字段增加/减少指定的值 |
| public final Date getTime() | 拿到此刻日期对象。 |
| public long getTimeInMillis() | 拿到此刻时间毫秒值 |
public void test() {
Calendar instance = Calendar.getInstance();
System.out.println(instance);
/**
* public int get(int field) 取日期中的某个字段信息。
*/
System.out.println("--------------------------------");
System.out.println("年:"+instance.get(Calendar.YEAR));
//这个类中 月份会少1 所以加1:
System.out.println("月:"+(instance.get(Calendar.MONTH)+1));
System.out.println("一年的第几天:"+instance.get(Calendar.DAY_OF_YEAR));
/**
* public void set(int field,int value) 修改日历的某个字段信息。
*/
System.out.println("--------------------------------");
System.out.println(instance);
//修改月份为11月:
instance.set(Calendar.MONTH,10);
System.out.println("年:"+instance.get(Calendar.YEAR));
//这个类中 月份会少1 所以加1:
System.out.println("月:"+(instance.get(Calendar.MONTH)+1));
System.out.println("一年的第几天:"+instance.get(Calendar.DAY_OF_YEAR));
System.out.println("--------------------------------");
/**
* public final Date getTime() 拿到此刻日期对象。
*/
Date date = instance.getTime();
System.out.println(date);
System.out.println("--------------------------------");
/**
* public long getTimeInMillis() 拿到此刻时间毫秒值
*/
Long time= instance.getTimeInMillis()+1000*60*60;
date.setTime(time);
System.out.println(date);
}

注意:calendar是可变日期对象,一旦修改后其对象本身表示的时间将产生变化。
6.JDK8新增日期类
概述
从Java 8开始,java.time包提供了新的日期和时间API,主要涉及的类型有:
- LocalDate:不包含具体时间的日期。
- LocalTime:不含日期的时间。
- LocalDateTime:包含了日期及时间。
- Instant:代表的是时间戳。
- DateTimeFormatter 用于做时间的格式化和解析的
- Duration:用于计算两个“时间”间隔
- Period:用于计算两个“日期”间隔
新增的API严格区分了时刻、本地日期、本地时间,并且,对日期和时间进行运算更加方便。
其次,新API的类型几乎全部是不变类型(和String的使用类似),可以放心使用不必担心被修改。
LocalDate、LocalTime、LocalDateTime
- 他们 分别表示日期,时间,日期时间对象,他们的类的实例是不可变的对象。
- 他们三者构建对象和API都是通用的
构建对象的方式如下:
| 方法名 | 说明 | |
| public static Xxxx now(); | 静态方法,根据当前时间创建对象 | LocaDate localDate = LocalDate.now(); LocalTime llocalTime = LocalTime.now(); LocalDateTime localDateTime = LocalDateTime.now(); |
| public static Xxxx of(…); | 静态方法,指定日期/时间创建对象 | LocalDate localDate1 = LocalDate.of(2099 , 11,11); LocalTime localTime1 = LocalTime.of(11, 11, 11); LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43); |
public void test() {
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
LocalDate date = localDate.of(2088, 2, 14);
System.out.println(date);
}

LocalDate、LocalTime、LocalDateTime获取信息的API:
| 方法名 | 说明 |
| public int geYear() | 获取年 |
| public int getMonthValue() | 获取月份(1-12) |
| Public int getDayOfMonth() | 获取月中第几天 |
| Public int getDayOfYear() | 获取年中第几天 |
| Public DayOfWeek getDayOfWeek() | 获取星期 |
public void test() {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("时间:"+dateTime);
System.out.println("获取年:"+dateTime.getYear());
System.out.println("获取月:"+dateTime.getMonthValue());
System.out.println("获取日:"+dateTime.getDayOfMonth());
System.out.println("一年中的第几天:"+dateTime.getDayOfYear());
System.out.println("获取星期:"+dateTime.getDayOfWeek().getValue());
}

转换相关的API:

LocalDateTime的转换API
| 方法名 | 说明 |
| public LocalDate toLocalDate() | 转换成一个LocalDate对象 |
| public LocalTime toLocalTime() | 转换成一个LocalTime对象 |
public void test() {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("时间:"+dateTime);
LocalDate date = dateTime.toLocalDate();
System.out.println("LocalDateTime 转为 LocalDate:"+date);
}

修改相关的API
- LocalDateTime 综合了 LocalDate 和 LocalTime 里面的方法,所以下面只用 LocalDate 和 LocalTime 来举例。
- 这些方法返回的是一个新的实例引用,因为LocalDateTime 、LocalDate 、LocalTime 都是不可变的。
| 方法名 | 说明 |
| plusDays, plusWeeks, plusMonths, plusYears | 向当前 LocalDate 对象添加几天、 几周、几个月、几年 |
| minusDays, minusWeeks, minusMonths, minusYears | 从当前 LocalDate 对象减去几天、 几周、几个月、几年 |
| withDayOfMonth, withDayOfYear, withMonth, withYear | 将月份天数、年份天数、月份、 年 份 修 改 为 指 定 的 值 并 返 回 新 的 LocalDate 对象 |
| isBefore, isAfter | 比较两个 LocalDate |
本文介绍了Java中的API概念,包括Object类的toString和equals方法,以及Objects工具类的使用。接着讲解了StringBuilder类在字符串操作中的优势,Date类、SimpleDateFormat和Calendar在日期时间处理中的功能。最后提到了JDK8新增的日期时间API,如LocalDate和LocalDateTime,强调了它们的不可变性和便利性。
847

被折叠的 条评论
为什么被折叠?



