学习笔记输出来源:拉勾教育Java就业急训营
修改时间:2020年12月28日20时 作者:pp_x 邮箱:pp_x12138@163.com
可变长字符串类
基本概念
由于String类描述的字符串内容是常量不可改变,当需要在Java代码中描述大量类似的字符串时,只能单独申请和存储,此时会造成内存空间的浪费 java.lang.StringBuilder类和java.lang.StringBuffer类来描述可以改变的字符串 StringBuffer从jdk1.0开始,线程安全,效率比较低 String Builder从jdk1.5开始,线程不安全,效率比较高
StringBuilder类常用的构造方法
方法声明 功能介绍 StringBuilder() 使用无参方式构造对象,容量为16 Strig Builder(int capacity) 根据参数指定的内容构造对象,容量为参数的指定大小 StringBuilder(String str) 根据参数中指定的字符串来构造对象 容量为16+字符串长度
public class StringBuilderTest {
public static void main ( String[ ] args) {
StringBuilder sb1 = new StringBuilder ( ) ;
System. out. println ( "sb1 = " + sb1) ;
System. out. println ( "容量是:" + sb1. capacity ( ) ) ;
System. out. println ( "长度是:" + sb1. length ( ) ) ;
System. out. println ( "-----------------------------------------------------------" ) ;
StringBuilder sb2 = new StringBuilder ( 20 ) ;
System. out. println ( "sb2 = " + sb2) ;
System. out. println ( "容量是:" + sb2. capacity ( ) ) ;
System. out. println ( "长度是:" + sb2. length ( ) ) ;
System. out. println ( "-----------------------------------------------------------" ) ;
StringBuilder sb3 = new StringBuilder ( "hello" ) ;
System. out. println ( "sb3 = " + sb3) ;
System. out. println ( "容量是:" + sb3. capacity ( ) ) ;
System. out. println ( "长度是:" + sb3. length ( ) ) ;
}
}
String Builder类常用的成员方法
方法声明 功能介绍 int capacity() 用于返回调用对象的容量 int length() 返回字符串的长度 StringBuilder insert(int offset, String str) 根据传入参数offset的位置来插入字符串并返回本身 StringBuilder append(String str) 追加字符串并返回本身 StringBuilder deleteCharAt(int index) 删除当前字符串下标为参数位置的单个字符 StringBuilder delete(int start,int end) 删除下标从start到end的字符串,包含start不包含end StringBuilder replace(int start,int end,String str) 替换下标从start到end的字符串 StringBuilder reverse() 反转字符串
注意 :作为参数传递的时候,方法内部的String不会改变值,String Buffer和String Builder会改变值
System. out. println ( "-----------------------------------------------------------" ) ;
String str1 = new String ( "hello" ) ;
String str2 = str1. toUpperCase ( ) ;
System. out. println ( "str2 = " + str2) ;
System. out. println ( "str1 = " + str1) ;
StringBuilder sb4 = sb3. insert ( 0 , "abcd" ) ;
System. out. println ( "sb4 = " + sb4) ;
System. out. println ( "sb3 = " + sb3) ;
sb3. insert ( 4 , "1234" ) ;
System. out. println ( "sb3 = " + sb3) ;
sb3. insert ( sb3. length ( ) , "ABCD" ) ;
System. out. println ( "sb3 = " + sb3) ;
System. out. println ( "-----------------------------------------------------------" ) ;
sb3. append ( "world" ) ;
System. out. println ( "sb3 = " + sb3) ;
System. out. println ( "容量是:" + sb3. capacity ( ) ) ;
System. out. println ( "长度是:" + sb3. length ( ) ) ;
System. out. println ( "-----------------------------------------------------------" ) ;
sb3. deleteCharAt ( 8 ) ;
System. out. println ( "sb3 = " + sb3) ;
for ( int i = 8 ; i < 12 ; i++ ) {
sb3. deleteCharAt ( 8 ) ;
}
System. out. println ( "删除后的字符串是:" + sb3) ;
System. out. println ( "-----------------------------------------------------------" ) ;
sb3. delete ( 0 , 4 ) ;
System. out. println ( "sb3 = " + sb3) ;
sb3. delete ( 4 , 8 ) ;
System. out. println ( "sb3 = " + sb3) ;
sb3. delete ( 4 , sb3. length ( ) ) ;
System. out. println ( "sb3 = " + sb3) ;
System. out. println ( "-----------------------------------------------------------" ) ;
sb3. setCharAt ( 0 , 'a' ) ;
System. out. println ( "修改单个字符后的内容是:" + sb3) ;
sb3. replace ( 1 , 4 , "bcd" ) ;
System. out. println ( "修改字符串后的结果是:" + sb3) ;
int pos = sb3. indexOf ( "b" ) ;
System. out. println ( "从前向后查找的结果是:" + pos) ;
pos = sb3. lastIndexOf ( "b" ) ;
System. out. println ( "从后向前查找的结果是:" + pos) ;
sb3. reverse ( ) ;
System. out. println ( "反转后的结果是:" + sb3) ;
注意 :
1、String Builder
很多的方法的返回值均为StringBuilder
类型,这些方法的返回语句均为return this
2、所以在对String Builder
所封装的字符序列进行改变后又返回了该对象的引用 。为了可以连续调用
sb3. reverse ( ) . append ( "1" ) . append ( "2" ) . insert ( 0 , "3" ) . delete ( 0 , 1 ) . reverse ( ) ;
String str3 = sb3. toString ( ) ;
StringBuilder sb5 = new StringBuilder ( str3) ;
知识点延申 - JAVA引用和引用所指向的值(以String Buffer为例)
日期相关类
Java8之前的日期相关类
System类的概念
java.lang.System类中提供了一些有用的类字段和方法
常用方法
方法声明 功能 static long currentTimeMillis() 返回当前时间与1970年1月1日0时0分0秒之间的毫秒数
Date类的概念
java.util.Date类主要用于描述特定的瞬间,也就是年月日分秒,可以精确到毫秒
常用的方法
方法声明 功能 Date() 使用无参的方式构造对象,也就是当前系统时间 Date(long date) 根据参数指定的毫秒数构造对象,参数为距离1970年1月1日0时0分0秒之间的毫秒数 long getTime() 调用对象1970年1月1日0时0分0秒之间的毫秒数 void setTime(long time) 设置调用对象为距离基准时间time毫秒的时间点
import java. util. Date;
public class DateTest {
public static void main ( String[ ] args) {
Date d1 = new Date ( ) ;
System. out. println ( "d1 = " + d1) ;
System. out. println ( "------------------------------------" ) ;
Date d2 = new Date ( 1000 ) ;
System. out. println ( "d2 = " + d2) ;
System. out. println ( "------------------------------------" ) ;
long msec = d2. getTime ( ) ;
System. out. println ( "获取到的毫秒数是:" + msec) ;
d2. setTime ( 2000 ) ;
System. out. println ( "修改后的时间是:" + d2) ;
}
}
SimpleDateFormat类的概念
java.text.SimpleDateFormat类主要用于实现日期和文本的转换
常用的方法
方法 功能 Simple DATe Format() 无参构造函数 Simple DATe Format(String pattern) 根据参数指定的模式来构造对象,模式主要有: y-年 M-月 d-日 H-时 m-分 s-秒 final String format(Date date) 用于将日期类型转换为文本类型 (能继承不能重写) Date parse(String source) 用于将文本类型转换为日期类型
import java. text. SimpleDateFormat;
import java. util. Date;
public class SimpleDateFormatTest {
public static void main ( String[ ] args) throws Exception {
Date d1 = new Date ( ) ;
System. out. println ( "d1 = " + d1) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
String format = sdf. format ( d1) ;
System. out. println ( "转换后的日期为:" + format) ;
Date parse = sdf. parse ( format) ;
System. out. println ( "转回日期格式的结果为:" + parse) ;
}
}
Calendar类的概念
java.util.Calender类主要用于描述特定的瞬间,取代Date类中过时方法实现全球化 该类是个抽象类,不能实例化对象,其具体子类针对不同国家的日历系统,其中应用最广泛的GREgorian Calendar(格里高利历),对应世界上绝大多数国家/地区的标准日历系统
常用的方法
方法声明 功能介绍 static Calendar getInstance() 用于获取Calendar类型的引用(返回的时子类类型) void set(int year, int month, int date, int hourOfDay, int minute, int second) 用于设置年月日时分秒信息 Date getTime() 用于将Calendar类型转换为Date类型 void set(int field, int value) 设置指定字段的数值 void add(int field, int amount) 向指定字段增加数值
多态的使用场合
通过方法的参数传递形成多态 public void draw(Shape s){ s.show(); } 在方法体中直接使用多态语法格式 Account acc = new FixedAccount(); 通过方法的返回值类型形成多态 Calender getInstance(){ return new GregorianCalendar(zone, aLocale); }
package com. lagou. task13;
import java. text. SimpleDateFormat;
import java. util. Calendar;
import java. util. Date;
public class CalendarTest {
public static void main ( String[ ] args) {
Date d1 = new Date ( 2008 - 1900 , 8 - 1 , 8 , 20 , 8 , 8 ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
String format = sdf. format ( d1) ;
System. out. println ( "获取到的时间是:" + format) ;
System. out. println ( "-----------------------------------------------------" ) ;
Calendar instance = Calendar. getInstance ( ) ;
instance. set ( 2008 , 8 - 1 , 8 , 20 , 8 , 8 ) ;
Date d2 = instance. getTime ( ) ;
String format1 = sdf. format ( d2) ;
System. out. println ( "获取到的时间是:" + format1) ;
System. out. println ( "-----------------------------------------------------" ) ;
instance. set ( Calendar. YEAR, 2018 ) ;
Date d3 = instance. getTime ( ) ;
System. out. println ( "设置年份后的结果是:" + sdf. format ( d3) ) ;
instance. add ( Calendar. MONTH, 2 ) ;
Date d4 = instance. getTime ( ) ;
System. out. println ( "增加月份后的结果是:" + sdf. format ( d4) ) ;
}
}
Java8中的日期相关类
Java8日期类的由来
JDK 1.0
中包含了一个java.util.Date
类,但是它的大多数方法已经在JDK 1.1
引入Calendar
类之后被弃用了。而Calendar
并不比Date
好多少。它们面临的问题是:Date
类中的年份是从1900开始的,而月份都从0开始。格式化只对Date
类有用,对Calendar
类则不能使用 非线程安全等。
Java8日期类的概述
Java 8
通过发布新的Date-Time API
来进一步加强对 日期与时间的处理。java.time
包:该包日期/时间API的基础包。 java.time.chrono
包:该包提供对不同日历系统的访问。 java.time.format
包:该包能够格式化和解析日期时间对象 java.time.temporal
包:该包包含底层框架和扩展特性 。java.time.zone
包:该包支持不同时区以及相关规则的类 。
LocalDate类的概述
java.time.LocalDate
类主要用于描述年-月-日 格式的日期信息,该类不表示时间和时区信息
常用方法
方法 功能 static LocalDate now() 在默认时区中从系统时钟获取当前日期
LocalTime类的概述
java.time.LocalTime
类主要用于描述时间信息,可以描述时分秒以及纳秒。
常用方法
方法 功能 static LocalTime now() 在默认时区中从系统时钟获取当前时间 static LocalTime now(ZoneId zone) 指定时区的当前时间
LocalDateTime类的概述
java.time.LocalDateTime
类主要用于描述ISO-8601日历系统中没有时区的日期时间,如2007-12- 03T10:15:30。
常用方法
方法 功能 static LocalDateTime now() 从默认时区的系统时间中获取当前日期时间 static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) 根据参数指定的年月日时分秒信息来设置日期时间 int getYear() 获取年份 int getMonthValue() 获取月份 int getDayOfMonth() 获取日期字段 int getHour() 获取小时 int getMinute() 获取分钟 int getSecond() 获取秒 LocalDateTime withYear(int year) 设置年份 LocalDateTime withMonth(int month) 设置月份 LocalDateTime withDayOfMonth(int dayOfMonth) 设置日 LocalDateTime withHour(int hour) 设置小时 LocalDateTime withMinute(int minute) 设置分钟 LocalDateTime withSecond(int second) 设置秒 LocalDateTime plusYears(long years) 加上指定的年 LocalDateTime plusMonths(long months) 加上指定的月 LocalDateTime plusDays(long days) 加上指定的日 LocalDateTime plusHours(long hours) 加上指定的小时 LocalDateTime plusMinutes(long minutes) 加上指定的分钟 LocalDateTime plusSeconds(long seconds) 加上指定的秒 LocalDateTime minusYears(long years) 减去指定的年 LocalDateTime minusMonths(long months) 减去指定的月 LocalDateTime minusDays(long days) 减去指定的日 LocalDateTime minusHours(long hours) 减去指定的小时 LocalDateTime minusMinutes(long minutes) 减去指定的分钟 LocalDateTime minusSeconds(long seconds) 减去指定的秒
import java. time. LocalDate;
import java. time. LocalDateTime;
import java. time. LocalTime;
public class LocalDateTimeTest {
public static void main ( String[ ] args) {
LocalDate now = LocalDate. now ( ) ;
System. out. println ( "获取到的当前日期是:" + now) ;
LocalTime now1 = LocalTime. now ( ) ;
System. out. println ( "获取到的当前时间是:" + now1) ;
LocalDateTime now2 = LocalDateTime. now ( ) ;
System. out. println ( "获取到的当前日期时间是:" + now2) ;
System. out. println ( "-------------------------------------------------------" ) ;
LocalDateTime of = LocalDateTime. of ( 2008 , 8 , 8 , 20 , 8 , 8 ) ;
System. out. println ( "指定的日期时间是:" + of) ;
System. out. println ( "获取到的年是:" + of. getYear ( ) ) ;
System. out. println ( "获取到的月是:" + of. getMonthValue ( ) ) ;
System. out. println ( "获取到的日是:" + of. getDayOfMonth ( ) ) ;
System. out. println ( "获取到的时是:" + of. getHour ( ) ) ;
System. out. println ( "获取到的分是:" + of. getMinute ( ) ) ;
System. out. println ( "获取到的秒是:" + of. getSecond ( ) ) ;
System. out. println ( "-------------------------------------------------------" ) ;
LocalDateTime localDateTime = of. withYear ( 2012 ) ;
System. out. println ( "localDateTime = " + localDateTime) ;
System. out. println ( "of = " + of) ;
LocalDateTime localDateTime1 = localDateTime. withMonth ( 12 ) ;
System. out. println ( "localDateTime1 = " + localDateTime1) ;
System. out. println ( "-------------------------------------------------------" ) ;
LocalDateTime localDateTime2 = localDateTime1. plusDays ( 2 ) ;
System. out. println ( "localDateTime2 = " + localDateTime2) ;
System. out. println ( "localDateTime1 = " + localDateTime1) ;
LocalDateTime localDateTime3 = localDateTime2. plusHours ( 3 ) ;
System. out. println ( "localDateTime3 = " + localDateTime3) ;
System. out. println ( "-------------------------------------------------------" ) ;
LocalDateTime localDateTime4 = localDateTime3. minusMinutes ( 1 ) ;
System. out. println ( "localDateTime4 = " + localDateTime4) ;
System. out. println ( "localDateTime3 = " + localDateTime3) ;
LocalDateTime localDateTime5 = localDateTime4. minusSeconds ( 3 ) ;
System. out. println ( "localDateTime5 = " + localDateTime5) ;
}
}
Instant类的概述
java.time.Instant
类主要用于描述瞬间的时间点信息。
常用的方法
方法声明 功能 static Instant now() 从系统时钟上获取当前时间 OffsetDateTime atOffset(ZoneOffset offset) 将此瞬间与偏移量组合以创建偏移日期时间 static Instant ofEpochMilli(long epochMilli) 根据参数指定的毫秒数来构造对象,参数为距离1970年1月1 日0时00秒的毫秒数 long toEpochMilli() 获取距离1970年1月1日0时0分0秒的毫秒数
import java. time. Instant;
import java. time. OffsetDateTime;
import java. time. ZoneOffset;
public class InstantTest {
public static void main ( String[ ] args) {
Instant now = Instant. now ( ) ;
System. out. println ( "获取到的当前时间为:" + now) ;
OffsetDateTime offsetDateTime = now. atOffset ( ZoneOffset. ofHours ( 8 ) ) ;
System. out. println ( "偏移后的日期时间为:" + offsetDateTime) ;
System. out. println ( "--------------------------------------------------------" ) ;
long g1 = now. toEpochMilli ( ) ;
System. out. println ( "获取到的毫秒差为:" + g1) ;
Instant instant = Instant. ofEpochMilli ( g1) ;
System. out. println ( "根据参数指定的毫秒数构造出来的对象为:" + instant) ;
}
}
DateTimeFormatter类的概述
java.time.format.DateTimeFormatter
类主要用于格式化和解析日期。
常用方法
方法声明 功能 static DateTimeFormatter ofPattern(String pattern) 根据参数指定的模式来获取对象 String format(TemporalAccessor temporal) 将参数指定日期时间转换为字符串 TemporalAccessor parse(CharSequence text) 将参数指定字符串转换为日期时间
package com. lagou. task13;
import java. time. LocalDateTime;
import java. time. format. DateTimeFormatter;
import java. time. temporal. TemporalAccessor;
public class DateTimeFormatterTest {
public static void main ( String[ ] args) {
LocalDateTime now = LocalDateTime. now ( ) ;
System. out. println ( "now = " + now) ;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter. ofPattern ( "yyyy-MM-dd HH:mm:ss" ) ;
String str = dateTimeFormatter. format ( now) ;
System. out. println ( "调整格式后的结果是:" + str) ;
TemporalAccessor parse = dateTimeFormatter. parse ( str) ;
System. out. println ( "转回去的结果是:" + parse) ;
}
}