1、可变字符串类
1.1、基本概念
- 由于String类描述的字符串内容是个常量不可改变,当需要在Java代码中描述大量类似的字符串时,只能单独申请和存储,此时会造成内存空间的浪费。
- 为了解决上述问题,可以使用java.lang.StringBuilder类和java.lang.StringBuffer类来描述字符序列可以改变的字符串,如:"ab"。
- StringBuffer类是从jdk1.0开始存在,属于线程安全的类,因此效率比较低。
- StringBuilder类是从jdk1.5开始存在,属于非线程安全的类,效率比较高。
1.2、StringBuilder类常用的构造方法
方法声明 | 功能介绍 |
StringBuilder() | 使用无参方式构造对象,容量为16 |
StringBuilder(int capacity) | 根据参数指定的容量来构造对象,容量为参数指定大小 |
StringBuilder(String str) | 根据参数指定的字符串来构造对象,容量为:16+字符串长度 |
1.3、StringBuilder类常用的成员方法
方法声明 | 功能介绍 |
int capacity() | 用于返回调用对象的容量 |
int length() | 用于返回字符串的长度,也就是字符的个数 |
StringBuilder insert(int offset, String str) | 插入字符串并返回调用对象的引用,就是自 己。 |
StringBuilder append(String str) | 追加字符串 |
StringBuilder deleteCharAt(int index) | 将当前字符串中下标为index位置的单个字符 删除 |
StringBuilder delete(int start,int end) | 删除字符串 |
StringBuilder replace(int start,int end, String str) | 替换字符串 |
StringBuilder reverse() | 字符串反转 |
setCharAt(int index, char ch) | 指定索引处的字符设置为 ch |
replace(int start, int end, String str) | 使用指定的 String的字符替换此序列的子字符串中的字符。 |
- 作为参数传递的话,方法内部String不会改变其值,StringBuffer和StringBuilder会改变其值。
String str1=new String("hello");
String str2=str1.toUpperCase();
System.out.println(str1+","+str2); //HELLO,hello
StringBuilder sb1=new StringBuilder("hello");
StringBuilder sb2=sb1.insert(0,"abcd");
System.out.println(sb1+","+sb2); // abcdhello,abcdhello
//考试考点
//考点一:既然StringBuilder类得对象本身可以修改,那么为什么成员方法还有返回值呢
//解析:为了连续调用
sb1.reverse().append("1").append("2").insert(0,"1");
//考点二:如何实现StringBuilder类型合String类型之间的转换
String str3=sb1.toString();
StringBuilder str4=new StringBuilder(str3);
//考点三:String,StringBuilder,StringBuffer之间效率谁高?如何排序
//String < StringBuffer < StringBuilder
1.4、返回值的设计
- StringBuilder的很多方法的返回值均为StringBuilder类型。这些方法的返回语句均为:return this。
- 由此可见,这些方法在对StringBuilder所封装的字符序列进行改变后又返回了该对象的引用。基于这样设计的目的在于可以连续调用。
2、Java8中的日期相关类
2.1、Java8日期类的由来
- JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了。而Calendar并不比Date好多少。它们面临的问题是:
- Date类中的年份是从1900开始的,而月份都从0开始。
- 格式化只对Date类有用,对Calendar类则不能使用。
- 非线程安全等。
//有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,
//可以作为比long还要大的数据类型
long mcss=System.currentTimeMillis();
System.out.println("距离1970年1月1日0分0秒已经过去"+mcss+"毫秒");
/**
* @author XiceSberg
* @date 2020/8/5 22:40
* @description
*/
public class DateTest {
public static void main(String[] args) throws Exception{
//获取当前系统时间
Date d1=new Date();
System.out.println("d1="+d1); //d1=Thu Aug 06 03:26:24 CST 2020
//构造SimpleDateFormat类型的对象并指定格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
//实现日期向文本类型转换
//alt+Enter可以实现返回值的生成
String format = sdf.format(d1);
System.out.println(format); //2020-26-06 03:26:24
//实现文本类型向实际日期转换
Date parse = sdf.parse(format); //parse会出现异常需要再main哪里加上异常抛出处理
System.out.println(parse); //Mon Jan 06 03:26:24 CST 2020
}
}
2.2、Java8日期类的概述
- Java 8通过发布新的Date-Time API来进一步加强对 日期与时间的处理。
- java.time包:该包日期/时间API的基础包。
- java.time.chrono包:该包提供对不同日历系统的访问。
- java.time.format包:该包能够格式化和解析日期时间对象。
- java.time.temporal包:该包包含底层框架和扩展特性。
- java.time.zone包:该包支持不同时区以及相关规则的类。
2.3、LocalDate类的概述
- java.time.LocalDate类主要用于描述年-月-日格式的日期信息,该类不表示时间和时区信息。
方法声明 | 功能介绍 |
static LocalDate now() | 在默认时区中从系统时钟获取当前日期 |
2.4、LocalTime类的概述
- java.time.LocalTime 类主要用于描述时间信息,可以描述时分秒以及纳秒。
方法声明 | 功能介绍 |
static LocalTime now() | 从默认时区的系统时间中获取当前时间 |
static LocalTime now(ZoneId zone) | 获取指定时区的当前时间 |
2.5、LocalDateTime类的概述
- java.time.LocalDateTime类主要用于描述ISO-8601日历系统中没有时区的日期时间,如2007-12- 03T10:15:30。
/**
* @author XiceSberg
* @date 2020/8/9 15:11
* @description
*/
public class LocalDateTest {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println("获取到的当前日期是:"+now); //获取到的当前日期是:2020-08-09
LocalTime now1 = LocalTime.now();
System.out.println("获取到的当前时间是:"+now1); //获取到的当前时间是:15:13:12.690550
LocalDateTime now2 = LocalDateTime.now();
System.out.println("获取到的当前日期时间是:"+now2); //获取到的当前日期时间是:2020-08-09T15:13:12.691270400
}
}
LocalDateTime of = LocalDateTime.of(2008, 8, 8, 8, 8, 8);
System.out.println("当前设置的时间是:"+of); //当前设置的时间是:2008-08-08T08:08:08
System.out.println(of.getYear()); //2008
System.out.println(of.getMonthValue());//8
System.out.println(of.getDayOfMonth());//8
System.out.println(of.getHour());//8
System.out.println(of.getMinute());//8
System.out.println(of.getSecond());//8
LocalDateTime localDateTime = of.withYear(2012);
System.out.println(localDateTime); //2012-08-08T08:08:08
System.out.println(of); //2008-08-08T08:08:08
LocalDateTime localDateTime1 = localDateTime.plusDays(2);
System.out.println(localDateTime1); //2012-08-10T08:08:08
System.out.println(localDateTime); //2012-08-08T08:08:08
LocalDateTime localDateTime2 = localDateTime1.minusMinutes(10);
System.out.println(localDateTime2); //2012-08-10T07:58:08
System.out.println(localDateTime1); //2012-08-10T08:08:08
2.6、Instant类的概述
- java.time.Instant类主要用于描述瞬间的时间点信息。
2.7、DateTimeFormatter类的概述
- java.time.format.DateTimeFormatter类主要用于格式化和解析日期。
//按照指定格式准备一个DateTimeFormatter类型得对象
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-mm-dd hh:mm:ss");
//获取当前系统时间
LocalDateTime now3 = LocalDateTime.now();
//实现日期格式得向字符串类型转换
String format = dateTimeFormatter.format(now3);
System.out.println(format); //2020-06-10 06:06:45
//实现字符串类型得向日期格式得转换
TemporalAccessor parse = dateTimeFormatter.parse(format);
//{HourOfAmPm=6, MinuteOfHour=6, Year=2020, MilliOfSecond=0, DayOfMonth=10, SecondOfMinute=45, MicroOfSecond=0, NanoOfSecond=0},ISO
System.out.println(parse);