JavaSE.06.常用类
1.String
1.String:字符串,使用一对""引起来表示
①.String声明为final的,不可被继承
②.String实现了Serializable接口:表示字符串是支持序列化的
实现了Comparable接口:表示String可以比较大小
③.String内部定义了final char[] value用于储存字符串数据
④.String:代表一个不可变的字符序列。简称:不可变性
体现:1.当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value值赋值
2.当对现有的字符串进行连接操作时,也需要重新制定内存区域赋值,不能使用原有的value值进行赋值
3.当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域创建一个新的value并赋值
⑤.通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中
⑥.常量池中是不会储存两个相同的字符串
@Test
public void test1 ( ) {
String s1 = "abc" ;
String s2 = "abc" ;
System. out. println ( s1== s2) ;
s1 = "hello" ;
System. out. println ( s1) ;
System. out. println ( s2) ;
String s3 = "abc" ;
s3 += "aaa" ;
System. out. println ( s3) ;
String s4 = "abc" ;
String s5 = s4. replace ( 'a' , 'm' ) ;
System. out. println ( s4) ;
System. out. println ( s5) ;
System. out. println ( s4 == s5) ;
}
2.String对象的创建
①.String str = "hello";
②.String s1 = new String();//本质上this.value = new char[0];
③.String s2 = new String(String original);//this.value = original.value;
④.String s3 = new String(char[] a);//this.value = Arrays.copyOf(value,value.length);
⑤.String s4 = new String(char[] a,int startIndex,int count);
例题:String s = new String("abc");方式创建对象,内存中创建了一个对象?
两个:一个是堆空间中new结构,另一个是char[]对应的常量池中的数据:"abc"
@Test
public void test2 ( ) {
String s1 = "java" ;
String s2 = "java" ;
String s3 = new String ( "java" ) ;
String s4 = new String ( "java" ) ;
System. out. println ( s1 == s2) ;
System. out. println ( s1 == s3) ;
System. out. println ( s3 == s4) ;
Person p1 = new Person ( "Tom" ) ;
Person p2 = new Person ( "Tom" ) ;
System. out. println ( p1. equals ( p2) ) ;
System. out. println ( p1. name == p2. name) ;
p1. name = "Jerry" ;
System. out. println ( p2. name) ;
}
3.总结
结论:
1.常量与常量的拼接结果在常量池,且常量池中不会存在相同内容的常量
2.只要其中有一个是变量,结果就在堆中
@Test
public void test3 ( ) {
String s1 = "java" ;
String s2 = "had" ;
String s3 = "javahad" ;
String s4 = "java" + "had" ;
String s5 = s1 + "had" ;
String s6 = "java" + s2;
String s7 = s1 + s2;
System. out. println ( s3 == s4) ;
System. out. println ( s3 == s5) ;
System. out. println ( s3 == s6) ;
System. out. println ( s5 == s6) ;
System. out. println ( s5 == s7) ;
System. out. println ( s6 == s7) ;
String s8 = s5. intern ( ) ;
System. out. println ( s3 == s8) ;
final String s9 = "java" ;
String s10 = s9 + "had" ;
System. out. println ( s10 == s3) ;
}
public class StringClassTest1 {
String str = new String ( "good" ) ;
char [ ] ch = { 't' , 'e' , 's' , 't' } ;
public void change ( String str, char ch[ ] ) {
str = "test ok" ;
ch[ 0 ] = 'b' ;
}
public static void main ( String[ ] args) {
StringClassTest1 ex = new StringClassTest1 ( ) ;
ex. change ( ex. str, ex. ch) ;
System. out. println ( ex. str) ;
System. out. println ( ex. ch) ;
}
}
4.String类常用方法
Int length():返回字符串的长度
char charAt(int index):返回某索引处的字符
boolean isEmpty():判断是否是空字符串
String toLowerCase():将所有字符转换为小写
String toUpperCase():将所有字符转换为大写
String trim():返回字符串的副本,忽略前部空白和尾部空白
boolean equals(Object obj):比较字符串内容是否相同
boolean equalsIgnoreCase(String anotherString):比较相同,忽略大小写比较
String concat(String str):将制定字符串连接到此字符串的结尾,等于“+”
int compareTo(String anotherString):比较两个字符串的大小
String substring(int beginIndex):返回一个新的字符串,他是此字符串的从beginIndex开始截取到最后的一个子字符串
String substring(int beginIndex,int endIndex):返回一个新的字符串,他是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串
public class StringMethodTest {
@Test
public void test1 ( ) {
String s1 = "helloworld" ;
System. out. println ( s1. length ( ) ) ;
System. out. println ( s1. charAt ( 0 ) ) ;
System. out. println ( s1. isEmpty ( ) ) ;
System. out. println ( s1. toUpperCase ( ) ) ;
System. out. println ( s1. toLowerCase ( ) ) ;
String s2 = " hello world " ;
System. out. println ( s2. trim ( ) ) ;
String s3 = "helloworld" ;
System. out. println ( s1. equals ( s2) ) ;
System. out. println ( s1. equals ( s2. trim ( ) ) ) ;
System. out. println ( s1. equals ( s3) ) ;
System. out. println ( s1. concat ( s2) ) ;
System. out. println ( s1. compareTo ( s2) ) ;
System. out. println ( s1. substring ( 2 ) ) ;
}
boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束
boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始
boolean startsWith(String prefix,int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始
boolean contains(CharSequence s):当且仅当此字符串包含指定的char值序列时,返回true
int indexOf(String str):返回指定字符串在此字符串中第一次出现的索引
int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出现的索引,从指定的索引开始
int lastIndexOf(String str):返回指定子字符串在此字符串中最右边的出现处的索引
int lastIndexOf(String str,int fromIndex):返回指定字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
注:indexOf和lastIndexOf方法如果未找到为-1
@Test
public void test3 ( ) {
String str1 = "helloworld" ;
boolean b1 = str1. endsWith ( "rld" ) ;
System. out. println ( b1) ;
boolean b2 = str1. startsWith ( "h" ) ;
System. out. println ( b2) ;
boolean b3 = str1. startsWith ( "w" , 5 ) ;
System. out. println ( b3) ;
String str2 = "wo" ;
System. out. println ( str1. contains ( str2) ) ;
System. out. println ( str1. indexOf ( "lo" ) ) ;
System. out. println ( str1. indexOf ( "lol" ) ) ;
System. out. println ( str1. lastIndexOf ( "lo" ) ) ;
}
替换
String replace(char oldChar,char newChar):返回一个新的字符串,他是通过newChar替换此字符串中出现的所有oldChar得到的
String replace(CharSequence target,CharSequence replacement):使用指定的字面值替换序列替换此字符串所匹配字面值目标序列的子字符串里
String replaceAll(String regex,String replacement):使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串
String replaceFirst(String regex,String replacement):使用给定的replacement替换此字符串匹配给定的正则表达式的第一个子字符串里
匹配
boolean mathches(String regex):告知此字符串是否匹配给定的正则表达式
切片
String[] split(String regex):根据给定正则表达式的匹配拆分此字符串
String[] split(String regex,int limlt):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中
@Test
public void test4 ( ) {
String str1 = "abcde" ;
String str11 = str1. replace ( "c" , "m" ) ;
System. out. println ( str1) ;
System. out. println ( str11) ;
String str12 = str1. replace ( "cd" , "mm" ) ;
System. out. println ( str12) ;
String str2 = "12hello34world5java7891mysq1456" ;
String string = str2. replaceAll ( "\\d+" , "," ) . replaceAll ( "^,|,$" , "" ) ;
System. out. println ( string) ;
String str = "12345" ;
boolean mathches = str. matches ( "\\d+" ) ;
System. out. println ( mathches) ;
String tel = "0571-4534289" ;
boolean result = tel. matches ( "0571-\\d{7,8}" ) ;
System. out. println ( result) ;
String str3 = "21,24,52,42" ;
String[ ] arr = str3. split ( "," ) ;
for ( String string2 : arr) {
System. out. print ( string2 + " " ) ;
}
}
5.String和其他数据类型的转换(复习)
String与基本数据类型,包装类之间的转换
String--->基本数据类型,包装类:调用包装类的静态方法:parseXxx(str)
基本数据类型,包装类--->String:调用String重载的valueOf(xxx)
@Test
public void test1 ( ) {
String str1 = "123" ;
int num = Integer. parseInt ( str1) ;
System. out. println ( num) ;
int num1 = 123 ;
String str2 = String. valueOf ( num1) ;
System. out. println ( str2) ;
}
String 与 char[] 之间的转换
String--->char[]:调用String的toCharArray()
char[]--->String:调用String的构造器
@Test
public void test2 ( ) {
String str = "abc123" ;
char [ ] charArray = str. toCharArray ( ) ;
for ( char c : charArray) {
System. out. println ( c) ;
}
char [ ] arr = new char [ ] { 's' , '2' , 'g' } ;
String str2 = new String ( arr) ;
System. out. println ( str2) ;
}
String和byte[]之间的转换
编码:String--->byte[]:调用String的getByte()
解码:byte[]--->String:调用String的构造器
注:解码时的字符集应与编码时的字符集一致
@Test
public void test3 ( ) {
String str = "sdsdasd我的" ;
byte [ ] by = str. getBytes ( ) ;
String str1 = new String ( by) ;
System. out. println ( str1) ;
byte [ ] by1 = null;
try {
by1 = str1. getBytes ( "gbk" ) ;
} catch ( UnsupportedEncodingException e) {
e. printStackTrace ( ) ;
}
for ( byte b : by) {
System. out. println ( b) ;
}
for ( byte b : by1) {
System. out. println ( b) ;
}
System. out. println ( new String ( by1) ) ;
String str2 = null;
try {
str2 = new String ( by1, "gbk" ) ;
} catch ( UnsupportedEncodingException e) {
e. printStackTrace ( ) ;
}
System. out. println ( str2) ;
}
2.String、StringBuffer、StringBuilder
1.三者比较
String、StringBuffer、StringBuilder三者的异同?
String:不可变的字符序列
StringBuffer:可变的字符序列,线程安全,效率偏低
StringBuilder:可变的字符序列,jdk5.0新增,线程不安全,效率高
相同:底层使用char[]进行存储
源码分析:
不可变:
String str = new String();//new char[0];
String str1 = new String("abc");//new char[]{'a','b','c'};
可变:
StringBuffer sb1 = new StringBuffer();//new char[16]:底层创建了一个长度为16的数组
System.out.print(sb1.length());//0
sb1.append('a');//value[0] = 'a';
sb1.append('b');//value[1] = 'b';
StringBuffer sb2 = new StringBuffer("abc");//长度为3 + 16
问题1:System.out.print(sb2.length());//3
问题2:扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层的数组
默认情况下,扩容为原来容量的2倍 + 2;同时将原有数组中的元素复制到新的数组中
指导意义:开发中建议大家使用:StringBuffer(int capacity)或StringBuilder(int capacity)
2.StringBuffer常用方法
StringBuffer类的常用方法
StringBuffer append(xxx):提供了很多的append()方法,用于进行字符串拼接
StringBuffer delete(int start,int end):删除指定位置的内容
StringBuffer replace(int start,int end,String str):把[start,end)位置替换为str
StringBuffer insert(int offset,xxx):在指定位置插入xxx
StringBuffer reverse():把当前字符序列逆转
int indexOf(String str)
String substring(int start,int end)
int length()
char charAt(int n)
setCharAt(int n,char ch)
对比String,StringBuffer,StringBuilder三者的效率?
从高到低排序:StringBuilder > StringBuffer >>> String
总结:
增:append()
删:delete()
改:setCharAt()/replace()
查:charAt()
插:insert()
长度:length()
遍历:for + charAt()
@Test
public void test1 ( ) {
StringBuffer sb1 = new StringBuffer ( "abc" ) ;
sb1. setCharAt ( 0 , 'm' ) ;
System. out. println ( sb1) ;
}
@Test
public void test2 ( ) {
StringBuffer sb1 = new StringBuffer ( "abc" ) ;
sb1. append ( 1 ) ;
sb1. append ( '1' ) ;
System. out. println ( sb1) ;
sb1. delete ( 1 , 2 ) ;
System. out. println ( sb1) ;
sb1. replace ( 0 , 1 , "m" ) ;
System. out. println ( sb1) ;
sb1. insert ( 0 , "n" ) ;
System. out. println ( sb1) ;
sb1. reverse ( ) ;
System. out. println ( sb1) ;
System. out. println ( sb1. indexOf ( "c" ) ) ;
String sub = sb1. substring ( 0 , 2 ) ;
System. out. println ( sub) ;
System. out. println ( sb1. length ( ) ) ;
System. out. println ( sb1. charAt ( 2 ) ) ;
sb1. setCharAt ( 2 , 'b' ) ;
System. out. println ( sb1) ;
}
@Test
public void testStringBuffer ( ) {
String str = null;
StringBuffer sb = new StringBuffer ( ) ;
sb. append ( str) ;
System. out. println ( sb. length ( ) ) ;
System. out. println ( sb) ;
StringBuffer sb1 = new StringBuffer ( str) ;
System. out. println ( sb1) ;
}
3.String总结练习
public class StringClassExercise {
public String reverse ( String str, int startIndex, int endIndex) {
if ( str != null) {
StringBuilder sb = new StringBuilder ( str. length ( ) ) ;
sb. append ( str. substring ( 0 , startIndex) ) ;
for ( int i = endIndex; i >= startIndex; i-- ) {
sb. append ( str. charAt ( i) ) ;
}
sb. append ( str. substring ( endIndex + 1 ) ) ;
return sb. toString ( ) ;
}
return null;
}
@Test
public void test1 ( ) {
String str = "abcdefg" ;
String newStr = new StringClassExercise ( ) . reverse ( str, 2 , 5 ) ;
System. out. println ( newStr) ;
}
public int getCount ( String mainStr, String subStr) {
int mainLength = mainStr. length ( ) ;
int subLength = subStr. length ( ) ;
int index = 0 ;
int count = 0 ;
if ( subLength < mainLength) {
while ( ( index = mainStr. indexOf ( subStr, index) ) != - 1 ) {
count++ ;
index += subLength;
}
return count;
} else {
return 0 ;
}
}
@Test
public void test2 ( ) {
String mainStr = "abkkkdasabodkwodksab" ;
String subStr = "ab" ;
System. out. println ( new StringClassExercise ( ) . getCount ( mainStr, subStr) ) ;
}
public String getMaxSameString ( String str1, String str2) {
if ( str1 != null && str2 != null) {
String maxStr = str1. length ( ) >= str2. length ( ) ? str1 : str2;
String minStr = str1. length ( ) < str2. length ( ) ? str1 : str2;
int length = minStr. length ( ) ;
ArrayList< String> list = new ArrayList < > ( ) ;
for ( int i = 0 ; i < length; i++ ) {
for ( int x = 0 , y = length - i; y <= length ; x++ , y++ ) {
String subStr = minStr. substring ( x, y) ;
if ( maxStr. contains ( subStr) ) {
list. add ( subStr) ;
}
}
if ( list. size ( ) != 0 ) {
break ;
}
}
return list. toString ( ) ;
}
return null;
}
@Test
public void test3 ( ) {
String str1 = "wdandhelloisndoasabcdef" ;
String str2 = "sdhelloadabcdef" ;
System. out. println ( new StringClassExercise ( ) . getMaxSameString ( str1, str2) ) ;
}
}
4.Comparable、Comparator
实现Comparable接口:涉及对象比较大小
Comparable和Comparator比较
Comparable一旦定义,则每一次可使用,并比较
Comparator只能使用一次
@Test
public void test1 ( ) {
String[ ] arr = new String [ ] { "AA" , "bb" , "cc" , "UU" , "DD" } ;
Arrays. sort ( arr) ;
System. out. println ( Arrays. toString ( arr) ) ;
}
@Test
public void test2 ( ) {
Goods[ ] arr = new Goods [ 4 ] ;
arr[ 0 ] = new Goods ( "xiaomi" , 50 ) ;
arr[ 1 ] = new Goods ( "huawei" , 21 ) ;
arr[ 2 ] = new Goods ( "yijia" , 34 ) ;
arr[ 3 ] = new Goods ( "luoji" , 56 ) ;
Arrays. sort ( arr) ;
System. out. println ( Arrays. toString ( arr) ) ;
}
@Test
public void test3 ( ) {
Goods[ ] arr = new Goods [ 4 ] ;
arr[ 0 ] = new Goods ( "xiaomi" , 50 ) ;
arr[ 1 ] = new Goods ( "huawei" , 21 ) ;
arr[ 2 ] = new Goods ( "yijia" , 34 ) ;
arr[ 3 ] = new Goods ( "luoji" , 56 ) ;
Arrays. sort ( arr, new Comparator < Object> ( ) {
@Override
public int compare ( Object o1, Object o2) {
if ( o1 instanceof Goods && o2 instanceof Goods ) {
Goods goods1 = ( Goods) o1;
Goods goods2 = ( Goods) o2;
if ( goods1. getPrice ( ) > goods2. getPrice ( ) ) {
return 1 ;
} else if ( goods1. getPrice ( ) < goods2. getPrice ( ) ) {
return - 1 ;
} else {
return goods1. getName ( ) . compareTo ( goods2. getName ( ) ) ;
}
}
throw new RuntimeException ( "传入的数据类型不一致" ) ;
}
} ) ;
System. out. println ( Arrays. toString ( arr) ) ;
}
public class Goods implements Comparable < Object> {
private String name;
private int price;
public Goods ( String name, int price) {
super ( ) ;
this . name = name;
this . price = price;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getPrice ( ) {
return price;
}
public void setPrice ( int price) {
this . price = price;
}
@Override
public String toString ( ) {
return "Goods [name=" + name + ", price=" + price + "]" ;
}
@Override
public int compareTo ( Object o) {
if ( o instanceof Goods ) {
Goods goods = ( Goods) o;
if ( this . price > goods. price) {
return 1 ;
} else if ( this . price < goods. price) {
return - 1 ;
} else {
return this . name. compareTo ( goods. name) ;
}
}
throw new RuntimeException ( "传入的数据类型不一致" ) ;
}
}
5.System Math BigInteger BigDecimal
System Math BigInteger BigDecimal
System: native long currentTimeMillis()
exit(int status): 退出程序
gc():垃圾回收器
getProperty(String key):获取系统中属性名为key的属性对应的值
常用的key: java.version/java.home/os.name/os.version/user.name/user.home/user.dir
BigInteger:可以表示不可变的 任意精度的整数
BigDecimal:商业计算中,要求精度比较高,所以用BigDecimal类
1.Math
2.BigInteger
3.BigDecimal
public class SystemMathBigIntegerTest {
public static void main ( String[ ] args) {
String version1 = System. getProperty ( "java.version" ) ;
System. out. println ( version1) ;
String home = System. getProperty ( "java.home" ) ;
System. out. println ( home) ;
String name = System. getProperty ( "os.name" ) ;
System. out. println ( name) ;
String version2 = System. getProperty ( "os.version" ) ;
System. out. println ( version2) ;
}
}
6.关于时间API
1.Calendar
@Test
public void test1 ( ) {
Calendar calendar = Calendar. getInstance ( ) ;
int days = calendar. get ( Calendar. DAY_OF_MONTH) ;
System. out. println ( days) ;
calendar. set ( Calendar. DAY_OF_MONTH, 22 ) ;
System. out. println ( calendar. get ( Calendar. DAY_OF_MONTH) ) ;
calendar. add ( Calendar. DAY_OF_MONTH, 3 ) ;
System. out. println ( calendar. get ( Calendar. DAY_OF_MONTH) ) ;
Date date = calendar. getTime ( ) ;
System. out. println ( date) ;
calendar. setTime ( new Date ( ) ) ;
System. out. println ( calendar. get ( Calendar. DAY_OF_MONTH) ) ;
}
2.LocalDate LocalTime LocalDateTime
@Test
public void test2 ( ) {
LocalDate localDate = LocalDate. now ( ) ;
System. out. println ( localDate) ;
LocalTime localTime = LocalTime. now ( ) ;
System. out. println ( localTime) ;
LocalDateTime localDateTime = LocalDateTime. now ( ) ;
System. out. println ( localDateTime) ;
LocalDateTime localDateTime1 = LocalDateTime. of ( 2020 , 11 , 5 , 14 , 27 , 20 ) ;
System. out. println ( localDateTime1) ;
System. out. println ( localDateTime1. getDayOfMonth ( ) ) ;
System. out. println ( localDateTime1. getDayOfYear ( ) ) ;
LocalDate localDate1 = localDate. withDayOfMonth ( 22 ) ;
System. out. println ( localDate1) ;
System. out. println ( localDate) ;
LocalDateTime localDateTime2 = localDateTime1. withHour ( 4 ) ;
System. out. println ( localDateTime2) ;
LocalDateTime localDateTime3 = localDateTime1. plusMonths ( 3 ) ;
System. out. println ( localDateTime3) ;
LocalDateTime localDateTime4 = localDateTime1. minusDays ( 6 ) ;
System. out. println ( localDateTime4) ;
}
3.System类中的currentTimeMillis()
@Test
public void test1 ( ) {
long time = System. currentTimeMillis ( ) ;
System. out. println ( time) ;
}
4.Date类
java.util.Date类
---java.sql.Date类
1.两个构造器的使用
Date date = new Date();
Date date1 = new Date(1604389602965l);
2.两个方法的使用
toString():显示当前的年、月、日、时、分、秒
getTime():获取当前Date对象对应的毫秒数。(时间戳)
3.java.sql.Date对应着数据库中的日期类型的变量
如何实例化
如何将java.util.Date对象转换为java.sql.Date对象
@Test
public void test2 ( ) {
Date date = new Date ( ) ;
System. out. println ( date) ;
System. out. println ( date. getTime ( ) ) ;
Date date1 = new Date ( 1604389602965 l) ;
System. out. println ( date1) ;
java. sql. Date date2 = new java. sql. Date ( 1604389602965 l) ;
System. out. println ( date2) ;
Date date3 = new java. sql. Date ( 1604389602965 l) ;
java. sql. Date date4 = ( java. sql. Date) date3;
Date date5 = new Date ( ) ;
java. sql. Date date6 = new java. sql. Date ( date5. getTime ( ) ) ;
System. out. println ( date6) ;
}
5.SimpleDateFormat
SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化和解析
1.两个操作
1.1格式化:日期--->字符串
1.2解析:格式化的逆过程,字符串--->日期
2.SimpleDateFormat的实例化
@Test
public void test1 ( ) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat ( ) ;
Date date = new Date ( ) ;
System. out. println ( date) ;
String format = sdf. format ( date) ;
System. out. println ( format) ;
String str = "2019-08-09 上午 11:43" ;
Date date1 = sdf. parse ( str) ;
System. out. println ( date1) ;
System. out. println ( "--------------------------" ) ;
SimpleDateFormat sdf1 = new SimpleDateFormat ( "yyyyy.MMMMM.dd GGG hh:mm aaa" ) ;
String str2 = sdf1. format ( date) ;
System. out. println ( str2) ;
Date date2 = sdf1. parse ( str2) ;
System. out. println ( date2) ;
}
@Test
public void test2 ( ) throws ParseException {
String birth = "2020-09-08" ;
SimpleDateFormat sdf1 = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date date = sdf1. parse ( birth) ;
java. sql. Date date1 = new java. sql. Date ( date. getTime ( ) ) ;
String startDate = "1990-01-01" ;
String endDate = "2000-01-01" ;
SimpleDateFormat ssdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date sdate = sdf1. parse ( startDate) ;
Date edate = sdf1. parse ( endDate) ;
long time = edate. getTime ( ) - sdate. getTime ( ) ;
long day = ( time/ 1000 / 24 / 60 / 60 ) % 5 ;
System. out. println ( day) ;
}
6.Instant
瞬时 Instant
时间线上的一个瞬时点,这可能被用来记录应用程序中的事件时间戳
@Test
public void test1 ( ) {
Instant instant = Instant. now ( ) ;
System. out. println ( instant) ;
OffsetDateTime odt = instant. atOffset ( ZoneOffset. ofHours ( 8 ) ) ;
System. out. println ( odt) ;
long milli = instant. toEpochMilli ( ) ;
System. out. println ( milli) ;
Instant ins = Instant. ofEpochMilli ( 1604559286990 l) ;
System. out. println ( ins) ;
}
7.DateTimeFormatter
DateTimeFormatter:格式化或解析日期、时间
类似于SimpleDateFormat
@Test
public void test2 ( ) {
DateTimeFormatter formatter1 = DateTimeFormatter. ISO_LOCAL_DATE_TIME;
LocalDateTime ldt = LocalDateTime. now ( ) ;
String str1 = formatter1. format ( ldt) ;
System. out. println ( ldt) ;
System. out. println ( str1) ;
TemporalAccessor parse = formatter1. parse ( str1) ;
System. out. println ( parse) ;
DateTimeFormatter formatter2 = DateTimeFormatter. ofLocalizedDateTime ( FormatStyle. SHORT) ;
String str2 = formatter2. format ( ldt) ;
System. out. println ( str2) ;
DateTimeFormatter formatter3 = DateTimeFormatter. ofLocalizedDate ( FormatStyle. FULL) ;
String str3 = formatter3. format ( LocalDate. now ( ) ) ;
System. out. println ( str3) ;
DateTimeFormatter formatter4 = DateTimeFormatter. ofPattern ( "yyyy-MM-dd hh:mm:ss" ) ;
String str4 = formatter4. format ( LocalDateTime. now ( ) ) ;
System. out. println ( str4) ;
TemporalAccessor accessor = formatter4. parse ( "2020-11-05 03:16:13" ) ;
System. out. println ( accessor) ;
}