Objects
Objects是一个java提供的操作对象的工具类
常用方法
- public static boolean equals(Object a, Object b) 先非空判断再比较两个对象
- public static boolean isNull(Object obj) 判断对象是不是null,是返回true
- public static boolean nonNull(Object obj) 判断对象是不是不是null,是返回true
为什么使用Objects中的equals判断两个对象
- 更安全 (先非空判断再比较两个对象)
包装类
为了实现java初期提出的万物皆对象,所以把基本的数据类型进行了封装,把它封装的类就是包装类。
应用场景
在泛型和集合中不支持基本数据类型,这个时候就需要使用包装类
基本数据类型 | 对应的包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
自动拆箱
包装类型会自动装换成基本数据类型
Integer a = 10;
int b = a;
自动装箱
基本数据类型会自动装换成包装类型
int a = 10;
Integer b = a;
常用方法
StringBuilder StringBuffer
出现原因:String是不可变的字符串,为了更好的操作字符串,java引入了StringBuilder StringBuffer。
介绍:StringBuilder可以理解成一个存储字符串的容器
构造器 | 说明 |
---|---|
public StringBuilder () | 创建一个空的StringBuilder 对象 |
public StringBuffer(String str) | 创建一个有初始化值的StringBuilder 对象 |
方法名称 | 说明 |
---|---|
public StringBuilder append(任意类型) | 添加数据并返回StringBuilder 对象 |
public StringBuilder reverse() | 字符串翻转 test->tset |
public int length() | 返回字符串长度 |
public String toString() | 实现StringBuilder 转成String对象 |
为什么操作字符串建议使用StringBuilder而不使用String?
对于频繁操作字符串,比如拼接效率更高
为什么对于频繁操作字符串,比如拼接StringBuilder效率更高呢?
String是不可变的字符串对象,每次拼接字符串都会创建一个新的字符串对象,重新指向新的字符串对象。
StringBuilder是可变的字符串对象,由于它相当于一个字符串容器,每次操作字符串都是在该容器中进行操作,所以大量操作字符串会很快。
注意:操作字符串较少或者不操作字符串还是建议使用String
比较简单的例子
// 使用String对象拼接
public class Test01 {
public static void main(String[] args) {
String str = "";
for (int i = 0; i < 10000000; i++) {
str+="abc";
}
System.out.println(str);
}
}
// StringBuilder 拼接字符串
public class Test01 {
public static void main(String[] args) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < 10000000; i++) {
str.append("abc");
}
System.out.println(str);
}
}
StringBuffer和StringBuilder 使用方法一致,只是在使用场景上有区别,StringBuilder 建议在单线程上使用,StringBuffer建议在多线程上使用
System(操作系统变量的工具类)
方法名 | 说明 |
---|---|
public static void exit(int status) | 终止当前的java虚拟机, status建议使用0,代表手动终止 |
public static native long currentTimeMillis() | 获取当前系统时间的毫秒数,一般用于进行代码分析 |
System.exit(0)
public class Test01 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
StringBuilder str = new StringBuilder();
for (int i = 0; i < 10000000; i++) {
str.append("abc");
}
System.out.println(str);
long end = System.currentTimeMillis();
System.out.println("运行耗时"+(end-start)+"ms");
}
}
Runtime
- Runtime是一个单例类
- 代表程序所在环境
常用方法
方法名 | 说明 |
---|---|
public static Runtime getRuntime() | 返回与当前java应用程序关联的运行对象 |
public static void exit(int status) | 终止当前的java虚拟机 |
public int availableProcessors() | 返回java虚拟机的处理器数 |
public long totalMemory() | Java虚拟机中的内存总量 |
public long freeMemory() | Java虚拟机中的可用内存 |
public Process exec(String command) | 启动某个程序,并返回代表该对象的进程对象 |
public class RuntimeTest {
public static void main(String[] args) {
Runtime jre = Runtime.getRuntime();
System.out.println("获取虚拟机能够使用的处理器数" + jre.availableProcessors());
System.out.println("Java虚拟机中的内存总量" + (jre.totalMemory() / 1024.0 / 1024.0) + "MB");
System.out.println("Java虚拟机中的可用内存总量" + (jre.freeMemory() / 1024.0 / 1024.0) + "MB");
// 执行命令,启动程序,返回一个进程对象代表这个软件
try {
Process calc = jre.exec("calc");
Optional<String> info = calc.info().user();
System.out.println(info);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
BigDecimal
- 用于解决浮点数参与运算出现结果失真问题
例如
public class Test01 {
public static void main(String[] args) {
double a = 0.1;
double b = 0.2;
System.out.println(a+b); // 0.30000000000000004
}
}
BigDecimal常用构造器、常用方法
构造器 | 说明 |
---|---|
public BigDecimal(double val) | 将double转换成BigDecimal(不推荐使用) |
public BigDecimal(String val) | 将字符串对象转成BigDecimal(推荐使用) |
方法名 | 说明 |
---|---|
public BigDecimal add(BigDecimal augend) | 加法 |
public BigDecimal subtract(BigDecimal subtrahend) | 减法 |
public BigDecimal multiply(BigDecimal multiplicand) | 乘法 |
public BigDecimal divide(BigDecimal divisor) | 除法 |
public static BigDecimal valueOf(double val) | 将double转换成BigDecimal |
public double doubleValue() | 将BigDecimal转换成double |
public class Test {
public static void main(String[] args) {
double a = 0.1;
double b = 0.3;
// BigDecimal a1 = new BigDecimal(Double.toString(a));
// BigDecimal b1 = new BigDecimal(Double.toString(b));
BigDecimal a1 = BigDecimal.valueOf(a);
BigDecimal b1 = BigDecimal.valueOf(b);
System.out.println(a1.add(b1));
System.out.println(a1.subtract(b1));
System.out.println(a1.divide(b1,2, 4));
}
}
java日期、时间
jdk8.0新增的时间对象
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(); // 月
int day = ld.getDayOfMonth(); // 日
int dayOfYear = ld.getDayOfYear(); // 一年中的第几天
int dayOfWeek = ld.getDayOfWeek().getValue(); // 星期几
System.out.println(year);
System.out.println(month);
System.out.println(day);
System.out.println(dayOfYear);
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);
System.out.println(ld4);
// 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
}
}
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(21);
LocalTime lt4 = lt.withMinute(10);
LocalTime lt5 = lt.withSecond(10);
LocalTime lt6 = lt.withNano(10);
System.out.println(lt);
// 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
}
}
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);
// 判断今天是否是某个人的生日。
LocalDate now = LocalDate.now();
// 拿到他出生的日子。
LocalDate bithday = LocalDate.of(2002, 11, 11);
MonthDay m1 = MonthDay.of(now.getMonth(), now.getDayOfMonth());
MonthDay m2 = MonthDay.of(bithday.getMonth(), bithday.getDayOfMonth());
System.out.println(m1.equals(m2)); // 生日
}
}
public class Test4_ZoneId_ZonedDateTime {
public static void main(String[] args) {
// 1、ZoneId(用于获取时区的)的常见方法:
// public static ZoneId systemDefault(): 获取系统默认的时区
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId.getId());
// public static Set<String> getAvailableZoneIds(): 获取Java支持的全部时区Id
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
System.out.println(zoneIds); // America/New_York
// public static ZoneId of(String zoneId) : 把某个时区id封装成ZoneId对象。
ZoneId aZoneId = ZoneId.of("America/New_York");
// 2、ZonedDateTime:带时区的时间。
// public static ZonedDateTime now(ZoneId zone): 获取某个时区的ZonedDateTime对象。
ZonedDateTime zdt = ZonedDateTime.now(aZoneId);
System.out.println(zdt);
// public static ZonedDateTime now():获取系统默认时区的ZonedDateTime对象
ZonedDateTime zdt2 = ZonedDateTime.now();// 没有给时区
System.out.println(zdt2);
// 很多服务器要获取世界时间(时间)。 UTC。
ZonedDateTime utcNow = ZonedDateTime.now(Clock.systemUTC());
System.out.println(utcNow);
ZonedDateTime currentUtcTime = ZonedDateTime.now(java.time.ZoneOffset.UTC);
System.out.println(currentUtcTime);
}
}
public class Test5_Instant {
public static void main(String[] args) {
// 1、创建Instant的对象,获取此刻时间信息。
Instant now = Instant.now(); // 世界标准时间
System.out.println(now);
// 2、获取总秒数
System.out.println(now.getEpochSecond());
// 3、不够1秒的纳秒数
System.out.println(now.getNano());
// Instant对象的作用:做代码的性能分析,或者记录用户的操作时间点
}
}
public class Test6_DateTimeFormatter {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
// 1、创建一个日期时间格式化器对象出来。参数:格式化的时间形式。
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd HH:mm:ss EEE a");
// 2、对时间进行格式化
String result = dtf.format(ldt);
System.out.println(result);
// 3、格式化时间,其实还有一种方案。
String result2 = ldt.format(dtf);
System.out.println(result2);
// 4、解析时间:解析时间一般使用LocalDateTime提供的解析方法来解析。
String dateStr = "2023-11-11 11:11:11";
// 创建日期时间格式化对象 : 参数格式,必须与被解析的时间格式一样
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 调用LocalDateTime的parse方法,按照dtf2规定的格式解析字符串时间成为日期时间对象。
LocalDateTime ldt2 = LocalDateTime.parse(dateStr, dtf2);
System.out.println(ldt2);
System.out.println(ldt2.getDayOfYear());
}
}
public class Test7_Period {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2023, 8, 10);
LocalDate end = LocalDate.now(); // 2023-11-11
// 1、创建Period对象,封装两个日期对象。
Period period = Period.between(start, end);
// 2、通过period对象获取两个日期对象相差的信息。
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
}
}
public class Test8_Duration {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2023, 11, 11, 11, 10, 10);
LocalDateTime end = LocalDateTime.of(2024, 1, 1, 11, 11, 11);
// 1、得到Duration对象
Duration duration = Duration.between(start, end);
// 2、获取两个时间对象间隔的信息
System.out.println(duration.toDays());// 间隔多少天
System.out.println(duration.toHours());// 间隔多少小时
System.out.println(duration.toMinutes());// 间隔多少分
System.out.println(duration.toSeconds());// 间隔多少秒
System.out.println(duration.toMillis());// 间隔多少毫秒
System.out.println(duration.toNanos());// 间隔多少纳秒
System.out.println("-------------------------高考倒计时----------------------------------");
// 需求:高考时间是: 2024-06-07 09:00:00
LocalDateTime ldt = LocalDateTime.now(); // 今天
System.out.println(ldt);
String str = "2024-06-07 09:00:00";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt2 = LocalDateTime.parse(str , dtf); // 高考时间
// 计算现在离高考差多少天,多少小时,多少分,多少秒。
Duration duration1 = Duration.between(ldt, ldt2);
System.out.println(duration1.toDays() + " " + duration1.toHoursPart() + " " + duration1.toMinutesPart() + " " + duration1.toSecondsPart());
}
}