目录
一、Object
1、object类的作用:
2、Object类的常用方法
2.1、toString()
2.2、toString方法的重写
2.3、equals()及重写
二、Objects
1、Objects概述
2、Object常见方法
2.1、equals
2.2、isNull
三、StringBuilder
1、StringBuilder概述
2、StringBuilder常用操作
3、String类拼接字符串原理图
4、StringBuilder提高效率原理图
5、使用StringBuilder打印数组元素
四、Math
五、System
六、BigDecimal
七、时间与日期
1、Date类
时间处理案例
2、SimpleDateFormat类
实现格式化时间和时间毫秒值 .format()
实现字符串时间格式解析 .parse()
练习
3、Calendar类
Calendar常用方法
八、JDK8新增日期类
1、概述
2、LocalDate、LocalTime、LocalDateTime
编辑
LocalDate 获取日期
LocalTime
LocalDateTime
LocalDateTime转化成LocalDate或LocalTime
修改相关的API
判断今天是否是生日
3、Instant时间戳
4、DateTimeFormatter
一、Object
1、object类的作用:
一个类要么默认继承了Object类,要么间接继承了Object类,Object类是java的祖宗类。
Object类的方法是一切子类都可以直接使用的,所以我们要学习Object类的方法
2、Object类的常用方法
一个类要么默认继承了Object类,要么间接继承了Object类,Object类是java的祖宗类。
Object类的方法是一切子类都可以直接使用的,所以我们要学习Object类的方法
2、Object类的常用方法
2.1、toString()
返回当前对象的地址。
public class Test {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.toString());
//Student@4eec7777返回对象的地址
}
}
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
...
}
开发中直接输出对象,默认输出对象的地址其实是毫无意义的。
开发中输出对象变量,更多的时候是希望看到对象的内容数据而不是对象的地址信息,所以需要对toString方法重写
2.2、toString方法的重写
父类toString()方法存在的意义就是为了被子类重写,以便返回对象的内容信息而不是地址信息。
public class Test {
public static void main(String[] args) {
Student s = new Student();
//System.out.println(s.toString());
//Student@4eec7777返回对象的地址
//重写后,输出内容
System.out.println(s.toString());
}
}
class Student {
private String name;
private int age;
//重写toString()
//tos加回车快速生成重写方法
@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
...
}
2.3、equals()及重写
比较两个对象的地址是否相同
由于是比较对象的地址,所以可以直接使用 == 代替,所以父类equals方法存在的意义就是为了被子类重写,以便子类自己来定制比较规则。
public class Test {
public static void main(String[] args) {
Student s = new Student("梨花",23);
Student s1 = new Student("梨花",23);
System.out.println(s.equals(s1)); //false
}
}
class Student {
private String name;
private int age;
//重写equals()
@Override
public boolean equals(Object o) {
//判断是否是同一个对象
if (this == o) return true;
//如果o是null返回false,或者 判断类型是否相同,不是学生类型
if (o == null || this.getClass() != o.getClass()) return false;
//说明o是学生,转成学生类型
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
...
}
二、Objects
1、Objects概述
Objects类与Object还是继承关系,Objects类是从JDK1.7开始之后才有。
2、Object常见方法
2.1、equals
返回当前对象的地址。
开发中直接输出对象,默认输出对象的地址其实是毫无意义的。
开发中输出对象变量,更多的时候是希望看到对象的内容数据而不是对象的地址信息,所以需要对toString方法重写
父类toString()方法存在的意义就是为了被子类重写,以便返回对象的内容信息而不是地址信息。
public class Test {
public static void main(String[] args) {
Student s = new Student();
//System.out.println(s.toString());
//Student@4eec7777返回对象的地址
//重写后,输出内容
System.out.println(s.toString());
}
}
class Student {
private String name;
private int age;
//重写toString()
//tos加回车快速生成重写方法
@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
...
}
2.3、equals()及重写
比较两个对象的地址是否相同
由于是比较对象的地址,所以可以直接使用 == 代替,所以父类equals方法存在的意义就是为了被子类重写,以便子类自己来定制比较规则。
public class Test {
public static void main(String[] args) {
Student s = new Student("梨花",23);
Student s1 = new Student("梨花",23);
System.out.println(s.equals(s1)); //false
}
}
class Student {
private String name;
private int age;
//重写equals()
@Override
public boolean equals(Object o) {
//判断是否是同一个对象
if (this == o) return true;
//如果o是null返回false,或者 判断类型是否相同,不是学生类型
if (o == null || this.getClass() != o.getClass()) return false;
//说明o是学生,转成学生类型
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
...
}
二、Objects
1、Objects概述
Objects类与Object还是继承关系,Objects类是从JDK1.7开始之后才有。
2、Object常见方法
2.1、equals
比较两个对象的地址是否相同
由于是比较对象的地址,所以可以直接使用 == 代替,所以父类equals方法存在的意义就是为了被子类重写,以便子类自己来定制比较规则。
1、Objects概述
Objects类与Object还是继承关系,Objects类是从JDK1.7开始之后才有。
2、Object常见方法
2.1、equals
Objects类与Object还是继承关系,Objects类是从JDK1.7开始之后才有。
2.1、equals
因为在Objects.equals()中会先进行非空校验,如果两个值不为空才会进行比较。
public class ObjestsTest {
public static void main(String[] args) {
String a = null;
String a1 = "wqdqd";
//如果a为空值调用equals会报错
//System.out.println(a.equals(a1));
//使用Objects的equals方法不会报错更安全
System.out.println(Objects.equals(a, a1));
}
}
2.2、isNull
判断变量是否为null,返回值布尔型。
public class ObjestsTest {
public static void main(String[] args) {
String a = null;
//使用API更专业
System.out.println(Objects.isNull(a));
//与 == 相同
System.out.println(a == null);
}
}
三、StringBuilder
1、StringBuilder概述
判断变量是否为null,返回值布尔型。
1、StringBuilder概述
2、StringBuilder常用操作
public class StringTest {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append(1);
sb.append("we");
//StringBuilder重写了toString,直接输出字符串的内容
System.out.println(sb);
StringBuilder sb1 = new StringBuilder();
//支持链式编程
sb1.append(34).append("rt").append("测试");
System.out.println(sb1);
//字符串内容反转
System.out.println(sb1.reverse());
//字符串长度
System.out.println(sb1.length());
//转化成String类型
String r = sb1.toString();
System.out.println(r);
}
}
3、String类拼接字符串原理图
先将main方法提到栈内存,然后将字符串a放入字符串常量池中,让变量s1指向a,再将b加入常量池,执行 + 操作时会在堆内存中new一个StringBuilder对象,再调用toString新建一个String对象得到相加的结果ab,使变量s2指向ab。
可以看出每执行一次 + 操作,就会在堆内存中产生两个对象,不断地丢弃原对象。
4、StringBuilder提高效率原理图
先将class文件加载到方法区,将main方法提到栈内存中,为变量sb分配存储空间,在堆内存中新建一个StringBuilder对象,将对象的地址传给sb存储,字符串常量池中添加a,b,c,依次添加进StringBuilder对象中。
StringBuilder相当于一个容器,所有的字符串操作都会在这一个容器中进行。
String内容是不可变的、拼接字符串性能差。StringBuilder内容是可变的、拼接字符串性能好、代码优雅。
5、使用StringBuilder打印数组元素
public class Demo {
public static void main(String[] args) {
int[] a = {10,20,30,40};
String s = print(a);
System.out.println(s);
}
public static String print(int[] a){
if (a != null) {
StringBuilder s = new StringBuilder("[");
for (int i = 0; i < a.length; i++) {
s.append(a[i]).append(i == a.length - 1 ? "" : "," );
}
s.append("]");
// String s1 = s.toString();
// return s1;
return s.toString();
}
else {
return null;
}
}
}
四、Math
ceil 天花板,floor 地板砖
五、System
public class SystemDemo {
public static void main(String[] args) {
System.out.println("程序开始");
//System.exit(0); //JVM终止,不能使用,系统会直接结束
//2、计算机认为时间有起源:返回1970-1-1 00:00:00 走到此刻的总的毫秒值
long time = System.currentTimeMillis();
System.out.println(time);
//可以进行时间计算:性能分析
long time1 = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
System.out.println(i);
}
long time2 = System.currentTimeMillis();
System.out.println((time2 - time1) / 1000.0); //跑一万次循环0.217S
//3、数组拷贝 arraycopy()
//源码
//参数一:被拷贝的数组
//参数二:从哪个索引位置开始拷贝
//参数三:复制的目标数组
//参数四:粘贴位置
//参数五:拷贝元素的个数
int[] a = {10,20,30,40};
int[] a1 = new int[4];
System.arraycopy(a,2,a1,2,2);
//不需要遍历直接查看数组元素
System.out.println(Arrays.toString(a1));
}
}
六、BigDecimal
public class BigDecimaTest {
public static void main(String[] args) {
double a = 0.1;
double b = 0.2;
double c = a + b;
System.out.println(c); //存在失真 0.30000000000000004
//使用构造器创建对象
BigDecimal a1 = BigDecimal.valueOf(a);
BigDecimal b1 = BigDecimal.valueOf(b);
//求和
BigDecimal c1 = a1.add(b1);
System.out.println(c1); //0.3
//BigDecimal只是一种防止失真手段,返回值还得转成double
double r = c1.doubleValue();
System.out.println(r);
//相乘
BigDecimal c2 = a1.multiply(b1);
System.out.println(c2);
//注意BigDecima是适用于必须做精度运算的
BigDecimal a11 = BigDecimal.valueOf(10.0);
BigDecimal b11 = BigDecimal.valueOf(3.0);
//除不尽无法做精度运算会报错
//BigDecimal c11 = a11.divide(b11);
//使用保留小数解决精度问题 参数一:除数,参数二:保留小数位数,参数三:舍入模式
BigDecimal c11 = a11.divide(b11,2, RoundingMode.HALF_UP);
System.out.println(c11);
}
}
七、时间与日期
1、Date类
Date类的对象在java中代表的是当前所在系统的此刻日期。
public class DateDemo1 {
public static void main(String[] args) {
//使用date类处理时间
//1、创建一个Date对象:代表系统此刻日期时间对象
Date d = new Date();
//打印出日期,说明toString被重写
System.out.println(d);
//2、获取时间毫秒值
long time = d.getTime();
System.out.println(time);
//long time1 = System.currentTimeMillis();
//System.out.println(time1);
//可以将System生成的时间毫秒值放入Date中转换成日期形式
//Date d1 = new Date(time1);
}
}
时间处理案例
public class DateDemo2 {
public static void main(String[] args) {
//1、得到当前时间
Date d = new Date();
System.out.println(d);
//2、当前时间往后走1小时
long time = System.currentTimeMillis();
time += (60 * 60) * 1000;
//3、把时间毫秒值转化成对应的日期对象
//方法一
//Date d2 = new Date(time);
//System.out.println(d2);
//方法二、
Date d2 = new Date();
//让当前时间走到time
d2.setTime(time);
System.out.println(d2);
}
}
2、SimpleDateFormat类
时间日期可视化。
格式化形式
实现格式化时间和时间毫秒值 .format()
public class DateDemo3 {
public static void main(String[] args) {
//获取当前时间
Date d = new Date();
System.out.println(d);
//设置当前时间格式化形式
//EEE代表星期,a代表上午或下午,按时间来
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
//格式化当前日期
String rs = sdf.format(d);
System.out.println(rs);
System.out.println("------------------------");
//格式化时间毫秒值
//需求:请问121s后的时间是多少
long time = System.currentTimeMillis() + 121 * 1000;
String rs2 = sdf.format(time);
System.out.println(rs2);
}
}
实现字符串时间格式解析 .parse()
public class SimpleDateFormatDemo2 {
public static void main(String[] args) throws ParseException {
//有一个时间2021年08月06日 11:11:11 往后2天14小时49分06秒后的日期
//将时间赋给字符串存储
String s = "2021年08月06日 11:11:11";
//设置格式化时间形式
//注意格式要与字符串中的格式一致,否则运行时解析报错
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:sss");
//对字符串日期进行解析,alt+Enter 设置抛出异常
Date d = sdf.parse(s);
//计算往后的时间,为了防止数据长度溢出,加L变为long型
long time = d.getTime() + (2L*24*60*60 + 14*60*60 + 49*60 +6) * 1000;
//输出计算后的时间
System.out.println(sdf.format(time));
}
}
练习
public class SimpleDateFormatDemo2 {
public static void main(String[] args) throws ParseException {
//有一个时间2021年08月06日 11:11:11 往后2天14小时49分06秒后的日期
//将时间赋给字符串存储
String s = "2021年08月06日 11:11:11";
//设置格式化时间形式
//注意格式要与字符串中的格式一致,否则运行时解析报错
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:sss");
//对字符串日期进行解析,alt+Enter 设置抛出异常
Date d = sdf.parse(s);
//计算往后的时间,为了防止数据长度溢出,加L变为long型
long time = d.getTime() + (2L*24*60*60 + 14*60*60 + 49*60 +6) * 1000;
//输出计算后的时间
System.out.println(sdf.format(time));
}
}
练习
public class SimpleDateFormatDemo3 {
public static void main(String[] args) throws ParseException {
//小贾
judge("2020年11月11日 0:0:0","2020年11月11日 0:10:0","2020年11月11日 0:03:47");
//小皮
judge("2020年11月11日 0:0:0","2020年11月11日 0:10:0","2020年11月11日 0:10:11");
}
public static void judge(String start,String stop,String s) throws ParseException {
Date s1 = transform(start);
Date s2 = transform(stop);
Date s3 = transform(s);
//Date中的方法,判断时间在s1之后,s2之前
if (s3.after(s1) && s3.before(s2)){
System.out.println("恭喜您!抢购成功");
}else {
System.out.println("很抱歉!您抢购失败");
}
}
//解析方法
public static Date transform(String s) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date s1 = sdf.parse(s);
return s1;
}
}
3、Calendar类
- Calendar代表了系统此刻日期对应的日历对象。
- Calendar是一个抽象类,不能直接创建对象。
Calendar常用方法
public class CalendarDemo1 {
public static void main(String[] args) {
//拿到系统当前的日历
//获取日历对象
Calendar cal = Calendar.getInstance();
System.out.println(cal);
//获取日历信息
//在日历中获取系统当前时间的年
int year = cal.get(Calendar.YEAR);
System.out.println(year);
//在日历中获取系统当前时间的月
//由于月份是从0开始计所以要加一
int month = cal.get(Calendar.MONTH) + 1;
System.out.println(month);
int day_of_year = cal.get(Calendar.DAY_OF_YEAR);
System.out.println(day_of_year);
//修改日历的某个字段
//修改日历设置系统当前时间为一年中的第一天
cal.set(Calendar.DAY_OF_YEAR,1);
//为某个字段增加/减少指定的值
//请问64天后是什么时间
cal.add(Calendar.DAY_OF_YEAR,64);
//拿到此刻的日期对象
Date d = cal.getTime();
System.out.println(d);
//拿到时间毫秒值
long time = cal.getTimeInMillis();
System.out.println(time);
}
}
八、JDK8新增日期类
1、概述
2、LocalDate、LocalTime、LocalDateTime
LocalDate 获取日期
public class LocalDtateDemo {
public static void main(String[] args) {
//获取系统当前的日期对象
LocalDate nowDate = LocalDate.now();
//获取当前的年
int year = nowDate.getYear();
//获取当前的日
int day = nowDate.getDayOfMonth();
//获取当前的月
Month month = nowDate.getMonth();
//直接获取值
int month1 = nowDate.getMonthValue();
//指定年月日创建对象
LocalDate date = LocalDate.of(1990,2,3);
}
}
LocalTime
public class LocalDtateDemo {
public static void main(String[] args) {
//获取系统当前的日期对象
LocalTime nowDate = LocalTime.now();
//获取当前的年
int year = nowDate.getHour();
//获取当前的日
int day = nowDate.getMinute();
//获取当前的月
int month = nowDate.getSecond();
//指定年月日创建对象
LocalTime date = LocalTime.of(19,2,3);
//指定年月日时分秒
LocalDateTime ldt = LocalDateTime.of(1990,3,4,5,6,9);
}
}
LocalDateTime
LocalDate 获取日期
public class LocalDtateDemo {
public static void main(String[] args) {
//获取系统当前的日期对象
LocalDate nowDate = LocalDate.now();
//获取当前的年
int year = nowDate.getYear();
//获取当前的日
int day = nowDate.getDayOfMonth();
//获取当前的月
Month month = nowDate.getMonth();
//直接获取值
int month1 = nowDate.getMonthValue();
//指定年月日创建对象
LocalDate date = LocalDate.of(1990,2,3);
}
}
LocalTime
public class LocalDtateDemo {
public static void main(String[] args) {
//获取系统当前的日期对象
LocalTime nowDate = LocalTime.now();
//获取当前的年
int year = nowDate.getHour();
//获取当前的日
int day = nowDate.getMinute();
//获取当前的月
int month = nowDate.getSecond();
//指定年月日创建对象
LocalTime date = LocalTime.of(19,2,3);
//指定年月日时分秒
LocalDateTime ldt = LocalDateTime.of(1990,3,4,5,6,9);
}
}
LocalDateTime
public class LocalDtateDemo {
public static void main(String[] args) {
//获取系统当前的日期对象
LocalTime nowDate = LocalTime.now();
//获取当前的年
int year = nowDate.getHour();
//获取当前的日
int day = nowDate.getMinute();
//获取当前的月
int month = nowDate.getSecond();
//指定年月日创建对象
LocalTime date = LocalTime.of(19,2,3);
//指定年月日时分秒
LocalDateTime ldt = LocalDateTime.of(1990,3,4,5,6,9);
}
}
LocalDateTime
LocalDateTime转化成LocalDate或LocalTime
public class LocalDateTimeDemo {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
LocalDate ld = ldt.toLocalDate();
LocalTime lt = ldt.toLocalTime();
}
}
修改相关的API
判断今天是否是生日
public class Birthday {
public static void main(String[] args) {
//判断是否是生日
LocalDate birth = LocalDate.of(2001,10,16);
//获取当前日期
LocalDate nowDay = LocalDate.now();
//生成月日信息对象
MonthDay birMD = MonthDay.of(birth.getMonth(),birth.getDayOfMonth());
//将当前日期的月日提取新建一个对象
MonthDay nowMd = MonthDay.from(nowDay);
//判断今天是否是生日
System.out.println(birMD.equals(nowMd));
}
}
3、Instant时间戳
就是指系统当前的时刻 。
就是指系统当前的时刻 。
public class InstantDemo {
public static void main(String[] args) {
//获取时间戳对象
Instant ist = Instant.now();
System.out.println(ist);
//Instant获取的时间戳时区需要更改
//先获取当前标准时间
Instant instant = Instant.now();
//更改时区,设为系统默认时区
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
//得到修改后的标准时间戳
System.out.println(zdt);
}
}
4、DateTimeFormatter
正反调用format方法格式化



