常用API(二)
一、Object类\Objects
1.Object类概述
Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份。
2.toString方法
调用toString()方法可以返回对象的字符串表示形式。
没有重写toString()方法的话返回的是地址值。
在bean类重写toString()方法,我们可以返回对象的属性值
class Student extends Object {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s = new Student();
s.setName("林青霞");
s.setAge(30);
System.out.println(s);
System.out.println(s.toString());
}
}
打印结果:
Student{name='林青霞', age=30}
Student{name='林青霞', age=30}
2.equals方法
-
equals方法的作用
-
用于对象之间的比较,返回true和false的结果
-
举例:s1.equals(s2); s1和s2是两个对象
-
-
重写equals方法的场景
-
不希望比较对象的地址值,想要结合对象属性进行比较的时候。
-
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
//this -- s1
//o -- s2
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o; //student -- s2
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("林青霞");
s1.setAge(30);
Student s2 = new Student();
s2.setName("林青霞");
s2.setAge(30);
//需求:比较两个对象的内容是否相同
System.out.println(s1.equals(s2));
}
}
3.Objects
常用方法
方法名 | 说明 |
---|---|
public static String toString(对象) | 返回参数中对象的字符串表示形式。 |
public static String toString(对象, 默认字符串) | 返回对象的字符串表示形式。 |
public static Boolean isNull(对象) | 判断对象是否为空 |
public static Boolean nonNull(对象) | 判断对象是否不为空 |
代码演示:
学生类:
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试类:
public class MyObjectsDemo {
public static void main(String[] args) {
// public static String toString(对象): 返回参数中对象的字符串表示形式。
// Student s = new Student("小罗同学",50);
// String result = Objects.toString(s);
// System.out.println(result);
// System.out.println(s);
// public static String toString(对象, 默认字符串): 返回对象的字符串表示形式。如果对象为空,那么返回第二个参数.
//Student s = new Student("小花同学",23);
// Student s = null;
// String result = Objects.toString(s, "随便写一个");
// System.out.println(result);
// public static Boolean isNull(对象): 判断对象是否为空
//Student s = null;
// Student s = new Student();
// boolean result = Objects.isNull(s);
// System.out.println(result);
// public static Boolean nonNull(对象): 判断对象是否不为空
//Student s = new Student();
Student s = null;
boolean result = Objects.nonNull(s);
System.out.println(result);
}
}
二、基本类型包装类
基本数据类型 | 对应的包装类型(引用数据类型) |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
char | Character |
float | Float |
double | Double |
boolean | Boolean |
//1.创建Integer对象,封装基本类型数据10
Integer a = new Integer(10);
//2.使用Integer类的静态方法valueOf(数据)
Integer b = Integer.valueOf(10);
//3.还有一种自动装箱的写法(意思就是自动将基本类型转换为引用类型)
Integer c = 10;
//4.有装箱肯定还有拆箱(意思就是自动将引用类型转换为基本类型)
int d = c;
//5.装箱和拆箱在使用集合时就有体现
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)
把基本类型数据转换为
代码演示;
//1.字符串转换为数值型数据
String ageStr = "29";
int age1 = Integer.parseInt(ageStr);
String scoreStr = 3.14;
double score = Double.prarseDouble(scoreStr);
//2.整数转换为字符串,以下几种方式都可以(挑中你喜欢的记一下)
Integer a = 23;
String s1 = Integer.toString(a);
String s2 = a.toString();
String s3 = a+"";
String s4 = String.valueOf(a);
三、时间日期类
1.Date类
Date 代表了一个特定的时间,精确到毫秒
Date类构造方法
方法名 | 说明 |
---|---|
public Date() | 创建 Date对象,默认存储当前时间,单位毫秒 |
public Date(long date) | 创建 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数 |
代码演示:
import java.util.Date;
public class DateDemo1 {
public static void main(String[] args) {
// public Date() 创建一个Date对象,表示默认时间
//public Date(long date) 创建一个Date对象,表示指定时间
//那么这个时间就表示电脑中的当前时间。
Date date1 = new Date();
System.out.println(date1);
//从计算机的时间原点开始,过了指定毫秒的那个时间。
Date date2 = new Date(0L);
System.out.println(date2);//Thu Jan 01 08:00:00 CST 1970
//从时间原点开始,过了0毫秒。
//因为我们是在中国,我们是在东八区需要+8小时。
//1970年1月1日 上午的10点
Date date3 = new Date(2*3600L * 1000);
System.out.println(date3);
}
}
Date类成员方法
方法名 | 说明 |
---|---|
public long getTime() | 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值 |
public void setTime(long time) | 设置时间,给的是毫秒值 |
代码演示:
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo2 {
public static void main(String[] args) {
// public long getTime() 获取时间对象的毫秒值
// public void setTime(long time) 设置时间,传递毫秒值
//method1();
//method2();
}
private static void method2() {
Date date1 = new Date();
date1.setTime(0L);
System.out.println(date1);
}
private static void method1() {
//把当前时间封装成一个date对象
Date date1 = new Date();
//获取这个date对象的毫秒值 --- 获取当前时间的毫秒值
long time = date1.getTime();
System.out.println(time);
long time2 = System.currentTimeMillis();
System.out.println(time2);
}
}
2.SimpleDateFormat类
SimpleDateFormat可以对Date对象和日期字符串,进行格式化和解析
SimpleDateFormat类构造方法
方法名 | 说明 |
---|---|
public SimpleDateFormat() | 构造一个SimpleDateFormat,使用默认模式和日期格式 |
public SimpleDateFormat(String pattern) | 构造一个SimpleDateFormat使用给定的模式 |
2.1.SimpleDateFormat类的常用方法
-
格式化(从Date到String)
public final String format(Date date):将日期格式化成日期/时间字符串
-
解析(从日期/时间字符串String到Date)
public Date parse(String source):从给定字符串的开始解析文本以生成日期
示例代码(公历日期标准格式为;2007年2月6日;或:2007-02-06)
public class TestSimpleDateFormat {
public static void main(String[] args) throws ParseException {
// 目标:掌握SimpleDateFormat的使用。
// 1、准备一些时间
Date d = new Date();
System.out.println(d);
long time = d.getTime();
System.out.println(time);
// 2、格式化日期对象,和时间 毫秒值。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
String rs = sdf.format(d);
String rs2 = sdf.format(time);
System.out.println(rs);
System.out.println(rs2);
System.out.println("----------------------------------------------");
// 目标:掌握SimpleDateFormat解析字符串时间 成为日期对象。
String dateStr = "2022-12-12 12:12:11";
// 1、创建简单日期格式化对象 , 指定的时间格式必须与被解析的时间格式一模一样,否则程序会出bug.
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d2 = sdf2.parse(dateStr);
System.out.println(d2);
}
}
3.jdk8之后新增的时间api
JDK8新增的日期类分得更细致一些,比如表示年月日用LocalDate类、表示时间秒用LocalTime类、而表示年月日时分秒用LocalDateTime类等;除了这些类还提供了对时区、时间间隔进行操作的类等。它们几乎把对日期/时间的所有操作都通过了API方法,用起来特别方便。
3.1 LocalDate类
public class Test1_LocalDate {
public static void main(String[] args) {
// 0、获取本地日期对象(不可变对象)
LocalDate ld = LocalDate.now(); // 年 月 日
System.out.println(ld);
// 1、获取日期对象中的信息
int year = ld.getYear(); // 年
int month = ld.getMonthValue(); // 月(1-12)
int day = ld.getDayOfMonth(); // 日
int dayOfYear = ld.getDayOfYear(); // 一年中的第几天
int dayOfWeek = ld.getDayOfWeek().getValue(); // 星期几
System.out.println(year);
System.out.println(day);
System.out.println(dayOfWeek);
// 2、直接修改某个信息: withYear、withMonth、withDayOfMonth、withDayOfYear
LocalDate ld2 = ld.withYear(2099);
LocalDate ld3 = ld.withMonth(12);
System.out.println(ld2);
System.out.println(ld3);
System.out.println(ld);
// 3、把某个信息加多少: plusYears、plusMonths、plusDays、plusWeeks
LocalDate ld4 = ld.plusYears(2);
LocalDate ld5 = ld.plusMonths(2);
// 4、把某个信息减多少:minusYears、minusMonths、minusDays、minusWeeks
LocalDate ld6 = ld.minusYears(2);
LocalDate ld7 = ld.minusMonths(2);
// 5、获取指定日期的LocalDate对象: public static LocalDate of(int year, int month, int dayOfMonth)
LocalDate ld8 = LocalDate.of(2099, 12, 12);
LocalDate ld9 = LocalDate.of(2099, 12, 12);
// 6、判断2个日期对象,是否相等,在前还是在后: equals isBefore isAfter
System.out.println(ld8.equals(ld9));// true
System.out.println(ld8.isAfter(ld)); // true
System.out.println(ld8.isBefore(ld)); // false
}
}
3.2LocalTime类
public class Test2_LocalTime {
public static void main(String[] args) {
// 0、获取本地时间对象
LocalTime lt = LocalTime.now(); // 时 分 秒 纳秒 不可变的
System.out.println(lt);
// 1、获取时间中的信息
int hour = lt.getHour(); //时
int minute = lt.getMinute(); //分
int second = lt.getSecond(); //秒
int nano = lt.getNano(); //纳秒
// 2、修改时间:withHour、withMinute、withSecond、withNano
LocalTime lt3 = lt.withHour(10);
LocalTime lt4 = lt.withMinute(10);
LocalTime lt5 = lt.withSecond(10);
LocalTime lt6 = lt.withNano(10);
// 3、加多少:plusHours、plusMinutes、plusSeconds、plusNanos
LocalTime lt7 = lt.plusHours(10);
LocalTime lt8 = lt.plusMinutes(10);
LocalTime lt9 = lt.plusSeconds(10);
LocalTime lt10 = lt.plusNanos(10);
// 4、减多少:minusHours、minusMinutes、minusSeconds、minusNanos
LocalTime lt11 = lt.minusHours(10);
LocalTime lt12 = lt.minusMinutes(10);
LocalTime lt13 = lt.minusSeconds(10);
LocalTime lt14 = lt.minusNanos(10);
// 5、获取指定时间的LocalTime对象:
// public static LocalTime of(int hour, int minute, int second)
LocalTime lt15 = LocalTime.of(12, 12, 12);
LocalTime lt16 = LocalTime.of(12, 12, 12);
// 6、判断2个时间对象,是否相等,在前还是在后: equals isBefore isAfter
System.out.println(lt15.equals(lt16)); // true
System.out.println(lt15.isAfter(lt)); // false
System.out.println(lt15.isBefore(lt)); // true
}
}
3.3LocalDateTime类(重点)
public class Test3_LocalDateTime {
public static void main(String[] args) {
// 0、获取本地日期和时间对象。
LocalDateTime ldt = LocalDateTime.now(); // 年 月 日 时 分 秒 纳秒
System.out.println(ldt);
// 1、可以获取日期和时间的全部信息
int year = ldt.getYear(); // 年
int month = ldt.getMonthValue(); // 月
int day = ldt.getDayOfMonth(); // 日
int dayOfYear = ldt.getDayOfYear(); // 一年中的第几天
int dayOfWeek = ldt.getDayOfWeek().getValue(); // 获取是周几
int hour = ldt.getHour(); //时
int minute = ldt.getMinute(); //分
int second = ldt.getSecond(); //秒
int nano = ldt.getNano(); //纳秒
// 2、修改时间信息:
// withYear withMonth withDayOfMonth withDayOfYear withHour
// withMinute withSecond withNano
LocalDateTime ldt2 = ldt.withYear(2029);
LocalDateTime ldt3 = ldt.withMinute(59);
// 3、加多少:
// plusYears plusMonths plusDays plusWeeks plusHours plusMinutes plusSeconds plusNanos
LocalDateTime ldt4 = ldt.plusYears(2);
LocalDateTime ldt5 = ldt.plusMinutes(3);
// 4、减多少:
// minusDays minusYears minusMonths minusWeeks minusHours minusMinutes minusSeconds minusNanos
LocalDateTime ldt6 = ldt.minusYears(2);
LocalDateTime ldt7 = ldt.minusMinutes(3);
// 5、获取指定日期和时间的LocalDateTime对象:
// public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour,
// int minute, int second, int nanoOfSecond)
LocalDateTime ldt8 = LocalDateTime.of(2029, 12, 12, 12, 12, 12, 1222);
LocalDateTime ldt9 = LocalDateTime.of(2029, 12, 12, 12, 12, 12, 1222);
// 6、 判断2个日期、时间对象,是否相等,在前还是在后: equals、isBefore、isAfter
System.out.println(ldt9.equals(ldt8));
System.out.println(ldt9.isAfter(ldt));
System.out.println(ldt9.isBefore(ldt));
// 7、可以把LocalDateTime转换成LocalDate和LocalTime
// public LocalDate toLocalDate()
// public LocalTime toLocalTime()
// public static LocalDateTime of(LocalDate date, LocalTime time)
LocalDate ld = ldt.toLocalDate();
LocalTime lt = ldt.toLocalTime();
LocalDateTime ldt10 = LocalDateTime.of(ld, lt);
}
}