可变字符串类和日期相关类
可变字符串类------------------------------
public final class StringBuffer
extends Object
implements Serializable , Comparable < StringBuffer > , CharSequence
public final class StringBuilder
extends Object
implements Serializable , Comparable < StringBuilder > , CharSequence
由于String类描述的字符串内容是个常量不可改变
当需要在Java代码中描述大量类似的字符串时,只能单独申请和存储,此时会造成内存空间的浪费
为了解决上述问题,可以使用java.lang.StringBuilder类和java.lang.StringBuffer类来描述字符序列可以改变的字符串
如:“ab”,这个"ab"是这两个类的,是能调整的字符串,可以改变内容
StringBuffer类是从jdk1.0开始存在,属于线程安全的类(不执行完其他不能执行,安全),因此效率比较低(必须等待执行完)
StringBuilder类是从jdk1.5开始存在,属于非线程安全的类(都一起执行,不安全),效率比较高(不用等待)
StringBuffer在相关方法上与StringBuilder的方法基本上一模一样
但StringBuilder取代了StringBuffer了,所以这里就讲StringBuilder类
StringBuilder类常用的构造方法------------------------------
StringBuilder ( ) ,使用无参方式构造对象,容量为16 ( 申请一块内存空间,可以放16 个字符)
StringBuilder ( int capacity) ,根据参数指定的容量来构造对象,容量为参数指定大小
StringBuilder ( String str) ,根据参数指定的字符串来构造对象,容量为:16 + 字符串长度
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) ,删除字符串,删除start到end之间的字符串,包括start,不包括end
StringBuilder replace ( int start,int end,String str) ,替换字符串
StringBuilder reverse ( ) ,字符串反转
void setCharAt ( int index, char ch) ,指定索引处的字符设置为ch
注意:作为参数传递的话,方法内部String不会改变其值,StringBuffer和StringBuilder会改变其值,实际上他又何尝不是类似于操作String来实现的呢,因为String完全可以利用返回的数组来重新创建来进行改变,其他的方法返回又何尝不是呢
而他们之所以可以变,是因为类似于String的实现,只是他们没有操作final而已
返回值的设计------------------------------
StringBuilder的很多方法的返回值均为StringBuilder类型
这些方法的返回语句均为:return this
由此可见,这些方法在对StringBuilder所封装的字符序列进行改变后又返回了该对象的引用
基于这样设计的目的在于可以连续调用
package com. lagou. task13 ;
import java. util. Arrays ;
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 ( ) ) ;
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) ;
int [ ] a = new int [ ] { 1 , 2 , 3 } ;
int [ ] b = Arrays . copyOf ( a, 6 ) ;
int [ ] c = Arrays . copyOf ( a, 2 ) ;
int [ ] d = Arrays . copyOf ( a, 3 ) ;
System . out. println ( Arrays . toString ( b) ) ;
System . out. println ( Arrays . toString ( c) ) ;
System . out. println ( Arrays . toString ( d) ) ;
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) ;
System . out. println ( "-----------------------------------------------------------" ) ;
}
}
System类的概述------------------------------
public final class System
extends Object
Java.lang.System类中提供了一些有用的类字段和方法
System . arraycopy ( arr, 1 , brr, 0 , 3 ) ;
常用的方法------------------------------
static long currentTimeMillis ( ) ,返回当前时间与1970 年1 月1 日0 时0 分0 秒之间以毫秒为单位的时间差
package com. lagou. task13 ;
public class SystemTest {
public static void main ( String [ ] args) {
long msec = System . currentTimeMillis ( ) ;
System . out. println ( "当前系统时间距离1970年1月1日0时0分0秒已经过去" + msec + "毫秒了!" ) ;
}
}
Date类的概述------------------------------
public class Date
extends Object
implements Serializable , Cloneable , Comparable < 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毫秒的时间点
package com. lagou. task13 ;
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类的概述------------------------------
public class SimpleDateFormat
extends DateFormat
public abstract class DateFormat
extends Format
public abstract class Format
extends Object
implements Serializable , Cloneable
java.text.SimpleDateFormat类主要用于实现日期和文本之间的转换
常用的方法------------------------------
SimpleDateFormat ( ) ,使用无参方式构造对象
SimpleDateFormat ( Stringpattern ) ,根据参数指定的模式来构造对象,模式主要有: y- 年 M - 月 d- 日H - 时 m- 分 s- 秒
final String format ( Datedate ) ,用于将日期类型转换为文本类型
Date parse ( String source) ,用于将文本类型转换为日期类型
package com. lagou. task13 ;
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类的概述------------------------------
public abstract class Calendar
extends Object
implements Serializable , Cloneable , Comparable < Calendar >
java.util.Calender类主要用于描述特定的瞬间,取代Date类中的过时方法实现全球化
该类是个抽象类,因此不能实例化对象
其具体子类针对不同国家的日历系统,其中应用最广泛的是GregorianCalendar(格里高利历)
对应世界上绝大多数国家/地区使用的标准日历系统
常用的方法------------------------------
static Calendar getInstance ( ) ,用于获取Calendar 类型的引用
void set ( int year, int month, int date, int hourOfDay, intminute, int second) ,用于设置年月日时分秒信息
Date getTime ( ) ,用于将Calendar 类型转换为Date 类型
void set ( int field, int value) ,设置指定字段的数值
void add ( int field, int amount) ,向指定字段增加数值
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) ) ;
}
}
多态的使用场合------------------------------
public static void draw ( Shape s) {
s. show ( ) ;
}
draw ( new Rect ( 1 , 2 , 3 , 4 ) ) ;
Account acc = new FixedAccount ( ) ;
Calender getInstance ( ) {
return new GregorianCalendar ( zone, aLocale) ;
}
Java8日期类的由来------------------------------
JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了
而Calendar并不比Date好多少
它们面临的问题是:Date类中的年份是从1900开始的,而月份都从0开始,格式化只对Date类有用,对Calendar类则不能使用
非线程安全等
Java8日期类的概述------------------------------
LocalDate类的概述------------------------------
public final class LocalDate
extends Object
implements Temporal , TemporalAdjuster , ChronoLocalDate , Serializable
java.time.LocalDate类主要用于描述年-月-日格式的日期信息,该类不表示时间和时区信息
常用的方法------------------------------
static LocalDate now ( ) ,在默认时区中从系统时钟获取当前日期
LocalTime类的概述------------------------------
public final class LocalTime
extends Object
implements Temporal , TemporalAdjuster , Comparable < LocalTime > , Serializable
java.time.LocalTime 类主要用于描述时间信息,可以描述时分秒以及纳秒
常用的方法------------------------------
static LocalTime now ( ) ,从默认时区的系统时间中获取当前时间
static LocalTime now ( ZoneId zone) ,获取指定时区的当前时间
LocalDateTime类的概述------------------------------
public final class LocalDateTime
extends Object
implements Temporal , TemporalAdjuster , ChronoLocalDateTime < LocalDate > , Serializable
java.time.LocalDateTime类主要用于描述ISO-8601日历系统中没有时区的日期时间,如2007-12-03T10:15:30
常用的方法------------------------------
static LocalDateTime now ( ) ,从默认时区的系统时间中获取当前日期时间
static LocalDateTime of ( int year, int month, intdayOfMonth, int hour, int minute, int second)
根据参数指定的年月日时分秒信息来设置日期时间
int getYear ( ) ,获取年份字段的数值
int getMonthValue ( ) ,获取1 到12 之间的月份字段
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) ,减去参数指定的秒
package com. lagou. task13 ;
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类的概述------------------------------
public final class Instant
extends Object
implements Temporal , TemporalAdjuster , Comparable < Instant > , Serializable
java.time.Instant类主要用于描述瞬间的时间点信息
常用的方法------------------------------
static Instant now ( ) ,从系统时钟上获取当前时间
OffsetDateTime atOffset ( ZoneOffset offset) ,将此瞬间与偏移量组合以创建偏移日期时间
static Instant ofEpochMilli ( long epochMilli)
根据参数指定的毫秒数来构造对象,参数为距离1970 年1 月1 日0 时0 分0 秒的毫秒数
long toEpochMilli ( ) ,获取距离1970 年1 月1 日0 时0 分0 秒的毫秒数
package com. lagou. task13 ;
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类的概述------------------------------
public final class DateTimeFormatter
extends Object
java.time.format.DateTimeFormatter类主要用于格式化和解析日期
DateTimeFormatter 与SimpleDateFormat 的区别:前者安全后者不安全
常用的方法------------------------------
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) ;
}
}
最后注意:这些时间类,我们了解即可,当我们需要操作时间时,我们可以选择到这里进行查看
实际上东八区,只是对应类对时间的区处理,具体如何统一,或者是否减去,请结合当时情况