字符串相关的类 String的特性
String类:代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。
String是一个final
类,代表不可变的字符序列。
字符串是常量,用双引号引起来表示。它们的值在创建之后不能更改。
String对象的字符内容是存储在一个字符数组value[]
中的。
1 2 3 4 5 6 public final class String implements java .io .Serializable , Comparable <String >, CharSequence { private final char value[]; private int hash;
1 2 3 String s1 = "abc" ; String s2 = "abc" ; s1 = "hello" ;
String对象的创建 1 2 3 4 5 6 7 8 String str = "hello" ; String s1 = new String(); String s2 = new String(String original); String s3 = new String(char [] a); String s4 = new String(char [] a,int startIndex,int count);
String str1 = "abc";
与String str2 = new String("abc");
的区别?
1 2 3 4 5 6 7 8 String s1 = "javaEE" ; String s2 = "javaEE" ; String s3 = new String("javaEE" ); String s4 = new String("javaEE" ); System.out.println(s1 == s2); System.out.println(s1 == s3); System.out.println(s1 == s4); System.out.println(s3 == s4);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 class Person { private String name; private int age; public Person (String name, int age) { this .name = name; this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } }public class StringTest { public static void main (String[] args) { Person p1 = new Person("Tom" ,12 ); Person p2 = new Person("Tom" ,12 ); System.out.println(p1.getName() == p2.getName()); System.out.println(p1.getAge() == p2.getAge()); } }
字符串的特性
结论 :
常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。
只要其中有一个是变量,结果就在堆中。
如果拼接的结果调用intern()
方法,返回值就在常量池中。
String使用陷阱
String s1 = "a";
说明:在字符串常量池中创建了一个字面量为”a”的字符串。
s1 = s1 + "b";
说明:实际上原来的“a”字符串对象已经丢弃了,现在在堆空间中产生了一个字符串s1+”b”(也就是”ab”)。如果多次执行这些改变串内容的操作,会导致大量副本字符串对象存留在内存中,降低效率。如果这样的操作放到循环中,会极大影响程序的性能。
String s2 = "ab";
说明:直接在字符串常量池中创建一个字面量为”ab”的字符串。
String s3 = "a" + "b";
说明:s3指向字符串常量池中已经创建的”ab”的字符串。String s4 = s1.intern();
说明:堆空间的s1对象在调用intern()
之后,会将常量池中已经存在的”ab”字符串赋值给s4。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class StringTest { 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) { StringTest ex = new StringTest(); ex.change(ex.str, ex.ch); System.out.print(ex.str + " and " ); System.out.println(ex.ch); } }
String类及常用方法
int length()
:返回字符串的长度: return value.length
char charAt(int index)
: 返回某索引处的字符return value[index]
boolean isEmpty()
:判断是否是空字符串:return value.length == 0
String toLowerCase()
:使用默认语言环境,将 String 中的所有字符转换为小写
String toUpperCase()
:使用默认语言环境,将 String 中的所有字符转换为大写
String trim()
:返回字符串的副本,忽略前导空白和尾部空白
boolean equals(Object obj)
:比较字符串的内容是否相同
boolean equalsIgnoreCase(String anotherString)
:与equals方法类似,忽略大小写
String concat(String str)
:将指定字符串连接到此字符串的结尾。 等价于用“+”
int compareTo(String anotherString)
:比较两个字符串的大小
String substring(int beginIndex)
:返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。
String substring(int beginIndex, int endIndex)
:返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。
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
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 matches(String regex)
:告知此字符串是否匹配给定的正则表达式。
String[] split(String regex)
:根据给定正则表达式的匹配拆分此字符串。
String[] split(String regex, int limit)
:根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 String str = "12hello34world5java7891mysql456" ; String string = str.replaceAll("\\d+" , "," ).replaceAll("^,|,$" , "" ); System.out.println(string); String str = "12345" ;boolean matches = str.matches("\\d+" ); System.out.println(matches); String tel = "0571-4534289" ;boolean result = tel.matches("0571-\\d{7,8}" ); System.out.println(result); String str = "hello|world|java" ; String[] strs = str.split("\\|" );for (int i = 0 ; i < strs.length; i++) { System.out.println(strs[i]); } System.out.println(); String str2 = "hello.world.java" ; String[] strs2 = str2.split("\\." );for (int i = 0 ; i < strs2.length; i++) { System.out.println(strs2[i]); }
String与基本数据类型转换 字符串→基本数据类型、包装类
Integer包装类的public static int parseInt(String s)
:可以将由“数字”字符组成的字符串转换为整型。
类似地,使用java.lang包中的Byte、Short、Long、Float、Double类调相应的类方法可以将由“数字”字符组成的字符串,转化为相应的基本数据类型。
基本数据类型、包装类→字符串
调用String类的public String valueOf(int n)
可将int型转换为字符串
相应的valueOf(byte b)、valueOf(long l)、valueOf(float f)、valueOf(double d)、valueOf(boolean b)
可由参数的相应类型到字符串的转换
String与字符数组转换 字符数组→字符串
String 类的构造器:String(char[]) 和 String(char[],int offset,int length) 分别用字符数组中的全部字符和部分字符创建字符串对象。
字符串→字符数组
public char[] toCharArray():将字符串中的全部字符存放在一个字符数组中的方法。
public void getChars(int srcBegin, int srcEnd, char[] dst,int dstBegin):提供了将指定索引范围内的字符串存放到数组中的方法。
String与字节数组转换 字节数组→字符串
String(byte[]):通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
String(byte[],int offset,int length) :用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象。
字符串→字节数组
public byte[] getBytes() :使用平台的默认字符集将此 String 编码为byte 序列,并将结果存储到一个新的 byte 数组中。
public byte[] getBytes(String charsetName) :使用指定的字符集将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
常见算法题目 1.将一个字符串进行反转。将字符串中指定部分进行反转。比如“abcdefg”反转为”abfedcg”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public class StringDemo { public static String reverse (String str,int startIndex,int endIndex) { if (str != null ){ char [] arr = str.toCharArray(); for (int x = startIndex,y = endIndex; x < y; x++, y--){ char temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } return new String(arr); } return null ; } public String reverse1 (String str,int startIndex,int endIndex) { if (str != null ){ String reverseStr = str.substring(0 ,startIndex); for (int i = endIndex; i >= startIndex ; i--) { reverseStr += str.charAt(i); } reverseStr += str.substring(endIndex+1 ); return reverseStr; } return null ; } public String reverse2 (String str,int startIndex,int endIndex) { if (str != null ){ StringBuilder builder = new StringBuilder(str.length()); builder.append(str.substring(0 ,startIndex)); for (int i = endIndex; i >= startIndex ; i--) { builder.append(str.charAt(i)); } builder.append(str.substring(endIndex+1 )); return builder.toString(); } return null ; } public static void main (String[] args) { System.out.println(StringDemo.reverse("abcdefg" ,2 ,5 )); } }
2.获取一个字符串在另一个字符串中出现的次数。比如:获取“ ab”在 “abkkcadkabkebfkabkskab” 中出现的次数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class StringDemo1 { public static int getCount (String mainStr,String subStr) { int mainLength = mainStr.length(); int subLength = subStr.length(); int count = 0 ; int index = 0 ; if (mainLength >= subLength){ while ((index = mainStr.indexOf(subStr,index)) != -1 ){ count++; index += subLength; } return count; }else { return 0 ; } } public static void main (String[] args) { String mainStr = "abkkcadkabkebfkaabkskab" ; String subStr = "ab" ; int count = getCount(mainStr, subStr); System.out.println(count); } }
3.获取两个字符串中最大相同子串。比如:str1 = "abcwerthelloyuiodef“;str2 = "cvhellobnm"
提示:将短的那个串进行长度依次递减的子串与较长的串比较。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 import org.junit.Test;import java.util.Arrays;public class StringDemo2 { 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(); 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)){ return subStr; } } } } return null ; } public String[] getMaxSameString1(String str1, String str2) { if (str1 != null && str2 != null ) { StringBuffer sBuffer = new StringBuffer(); String maxString = (str1.length() > str2.length()) ? str1 : str2; String minString = (str1.length() > str2.length()) ? str2 : str1; int len = minString.length(); for (int i = 0 ; i < len; i++) { for (int x = 0 , y = len - i; y <= len; x++, y++) { String subString = minString.substring(x, y); if (maxString.contains(subString)) { sBuffer.append(subString + "," ); } } if (sBuffer.length() != 0 ) { break ; } } String[] split = sBuffer.toString().replaceAll(",$" , "" ).split("\\," ); return split; } return null ; } @Test public void testGetMaxSameString () { String str1 = "abcwerthello1yuiodefabcdef" ; String str2 = "cvhello1bnmabcdef" ; String[] maxSameStrings = getMaxSameString1(str1, str2); System.out.println(Arrays.toString(maxSameStrings)); } }
StringBuffer类
java.lang.StringBuffer代表可变的字符序列,JDK1.0中声明,可以对字符串内容进行增删,此时不会产生新的对象。
很多方法与String相同。
作为参数传递时,方法内部可以改变值。
StringBuffer类不同于String,其对象必须使用构造器生成。有三个构造器:
StringBuffer()
:初始容量为16的字符串缓冲区
StringBuffer(int size)
:构造指定容量的字符串缓冲区
StringBuffer(String str)
:将内容初始化为指定字符串内容
1 2 3 String s = new String("我喜欢学习" ); StringBuffer buffer = new StringBuffer("我喜欢学习" ); buffer.append("数学" );
StringBuffer类的常用方法 StringBuffer append(xxx)
:提供了很多的append()
方法,用于进行字符串拼接StringBuffer delete(int start,int end)
:删除指定位置的内容StringBuffer replace(int start, int end, String str)
:把[start,end)位置替换为strStringBuffer insert(int offset, xxx)
:在指定位置插入xxxStringBuffer reverse()
:把当前字符序列逆转
当append和insert时,如果原来value数组长度不够,可扩容。
如上这些方法支持方法链操作。
方法链的原理:
StringBuilder类 StringBuilder 和 StringBuffer 非常类似,均代表可变的字符序列,而且 提供相关功能的方法也一样
面试题:对比String、StringBuffer、StringBuilder
String(JDK1.0):不可变字符序列
StringBuffer(JDK1.0):可变字符序列、效率低、线程安全
StringBuilder(JDK 5.0):可变字符序列、效率高、线程不安全 注意:作为参数传递的话,方法内部String不会改变其值,StringBuffer
和StringBuilder
会改变其值。
JDK 8之前的日期时间API java.lang.System类 System类提供的public static long currentTimeMillis()
用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
计算世界时间的主要标准有:
UTC(Coordinated Universal Time)
GMT(Greenwich Mean Time)
CST(Central Standard Time)
java.util.Date类 表示特定的瞬间,精确到毫秒
构造器:
Date():使用无参构造器创建的对象可以获取本地当前时间。
Date(long date)
常用方法
getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象 表示的毫秒数。
toString():把此 Date 对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一周中的某一天 (Sun, Mon, Tue,Wed, Thu, Fri, Sat),zzz是时间标准。
其它很多方法都过时了。
1 2 3 4 5 6 7 8 import java.util.Date; Date date = new Date(); System.out.println(date); System.out.println(System.currentTimeMillis()); System.out.println(date.getTime()); Date date1 = new Date(date.getTime()); System.out.println(date1.getTime()); System.out.println(date1.toString());
java.text.SimpleDateFormat类 Date类的API不易于国际化,大部分被废弃了,java.text.SimpleDateFormat
类是一个不与语言环境有关的方式来格式化和解析日期的具体类。
格式化 :
SimpleDateFormat()
:默认的模式和语言环境创建对象
public SimpleDateFormat(String pattern)
:该构造方法可以用参数pattern指定的格式创建一个对象,该对象调用:
public String format(Date date)
:方法格式化时间对象date
解析:
public Date parse(String source):从给定字符串的开始解析文本,以生成一个日期。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 Date date = new Date(); SimpleDateFormat formater = new SimpleDateFormat(); System.out.println(formater.format(date)); SimpleDateFormat formater2 = new SimpleDateFormat("yyyy年MM月dd日 EEE HH:mm:ss" ); System.out.println(formater2.format(date));try { Date date2 = formater2.parse("2008年08月08日 星期一 08:08:08" ); System.out.println(date2.toString()); } catch (ParseException e) { e.printStackTrace(); }
java.util.Calendar(日历)类 Calendar是一个抽象基类,主用用于完成日期字段之间相互操作的功能。
获取Calendar实例的方法
使用Calendar.getInstance()
方法
调用它的子类GregorianCalendar
的构造器。
一个Calendar的实例是系统时间的抽象表示,通过get(int field)
方法来取得想要的时间信息。比如YEAR、MONTH、DAY_OF_WEEK、HOUR_OF_DAY 、MINUTE、SECOND
public void set(int field,int value)
public void add(int field,int amount)
public final Date getTime()
public final void setTime(Date date)
注意:
获取月份时:一月是0,二月是1,以此类推,12月是11
获取星期时:周日是1,周二是2 , 。。。。周六是7
1 2 3 4 5 6 7 8 9 10 11 12 Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); date = new Date(234234235235L ); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, 8 ); System.out.println("当前时间日设置为8后,时间是:" + calendar.getTime()); calendar.add(Calendar.HOUR, 2 ); System.out.println("当前时间加2小时后,时间是:" + calendar.getTime()); calendar.add(Calendar.MONTH, -2 ); System.out.println("当前日期减2个月后,时间是:" + calendar.getTime());
JDK 8中新日期时间API 新日期时间API出现的背景 如果我们可以跟别人说:“我们在1502643933071见面,别晚了!”那么就再简单不过了。但是我们希望时间与昼夜和四季有关,于是事情就变复杂了。JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了。而Calendar并不比Date好多少。它们面临的问题是:
可变性:像日期和时间这样的类应该是不可变的。
偏移性:Date中的年份是从1900开始的,而月份都从0开始。
格式化:格式化只对Date有用,Calendar则不行。
此外,它们也不是线程安全的;不能处理闰秒等。 总结:对日期和时间的操作一直是Java程序员最痛苦的地方之一。
新时间日期API
第三次引入的API是成功的,并且Java 8中引入的java.time API 已经纠正了过去的缺陷,将来很长一段时间内它都会为我们服务。
Java 8 吸收了 Joda-Time 的精华,以一个新的开始为 Java 创建优秀的 API。新的 java.time 中包含了所有关于本地日期(LocalDate)、本地时间(LocalTime)、本地日期时间(LocalDateTime)、时区(ZonedDateTime)和持续时间(Duration)的类。历史悠久的 Date 类新增了 toInstant() 方法,用于把 Date 转换成新的表示形式。这些新增的本地化时间日期 API 大大简化了日期时间和本地化的管理。
新时间日期API
java.time
– 包含值对象的基础包
java.time.chrono
– 提供对不同的日历系统的访问
java.time.format
– 格式化和解析时间和日期
java.time.temporal
– 包括底层框架和扩展特性
java.time.zone
– 包含时区支持的类
LocalDate、LocalTime、LocalDateTime LocalDate、LocalTime、LocalDateTime 类是其中较重要的几个类,它们的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的本地日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。
LocalDate
代表IOS格式(yyyy-MM-dd)的日期,可以存储 生日、纪念日等日期。
LocalTime
表示一个时间,而不是日期。
LocalDateTime
是用来表示日期和时间的,这是一个最常用的类之一。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 LocalDate localDate = LocalDate.now(); LocalTime localTime = LocalTime.now(); LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDate); System.out.println(localTime); System.out.println(localDateTime); LocalDateTime localDateTime1 = LocalDateTime.of(2020 , 10 , 6 , 13 , 23 , 43 ); System.out.println(localDateTime1); System.out.println(localDateTime.getDayOfMonth()); System.out.println(localDateTime.getDayOfWeek()); System.out.println(localDateTime.getMonth()); System.out.println(localDateTime.getMonthValue()); System.out.println(localDateTime.getMinute()); LocalDate localDate1 = localDate.withDayOfMonth(22 ); System.out.println(localDate); System.out.println(localDate1); LocalDateTime localDateTime2 = localDateTime.withHour(4 ); System.out.println(localDateTime); System.out.println(localDateTime2); LocalDateTime localDateTime3 = localDateTime.plusMonths(3 ); System.out.println(localDateTime); System.out.println(localDateTime3); LocalDateTime localDateTime4 = localDateTime.minusDays(6 ); System.out.println(localDateTime); System.out.println(localDateTime4);
瞬时:Instant
Instant:时间线上的一个瞬时点。 这可能被用来记录应用程序中的事件时间戳。
在处理时间和日期的时候,我们通常会想到年,月,日,时,分,秒。然而,这只是时间的一个模型,是面向人类的。第二种通用模型是面向机器的,或者说是连续的。在此模型中,时间线中的一个点表示为一个很大的数,这有利于计算机处理。在UNIX中,这个数从1970年开始,以秒为的单位;同样的,在Java中, 也是从1970年开始,但以毫秒为单位。
java.time包通过值类型Instant提供机器视图,不提供处理人类意义上的时间单位。Instant表示时间线上的一点,而不需要任何上下文信息,例如,时区。概念上讲,它只是简单的表示自1970年1月1日0时0分0秒(UTC)开始的秒数。因为java.time包是基于纳秒计算的,所以Instant的精度可以达到纳秒级。
(1 ns = 10-9 s) 1秒 = 1000毫秒 =10^6微秒=10^9纳秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Test public void test2 () { Instant instant = Instant.now(); System.out.println(instant); OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8 )); System.out.println(offsetDateTime); long milli = instant.toEpochMilli(); System.out.println(milli); Instant instant1 = Instant.ofEpochMilli(1550475314878L ); System.out.println(instant1); }
格式化与解析日期或时间 java.time.format.DateTimeFormatter
类:该类提供了三种格式化方法:
预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG)
自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 @Test public void test3 () { DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; LocalDateTime localDateTime = LocalDateTime.now(); String str1 = formatter.format(localDateTime); System.out.println(localDateTime); System.out.println(str1); TemporalAccessor parse = formatter.parse("2019-02-18T15:42:18.797" ); System.out.println(parse); DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG); String str2 = formatter1.format(localDateTime); System.out.println(str2); DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); String str3 = formatter2.format(LocalDate.now()); System.out.println(str3); DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss" ); String str4 = formatter3.format(LocalDateTime.now()); System.out.println(str4); TemporalAccessor accessor = formatter3.parse("2019-02-18 03:52:09" ); System.out.println(accessor); }
其它API
ZoneId
:该类中包含了所有的时区信息,一个时区的ID,如 Europe/Paris
ZonedDateTime
:一个在ISO-8601日历系统时区的日期时间,如 2007-12- 03T10:15:30+01:00 Europe/Paris。
其中每个时区都对应着ID,地区ID都为“{区域}/{城市}”的格式,例如:Asia/Shanghai等
Clock
:使用时区提供对当前即时、日期和时间的访问的时钟。
持续时间:Duration,用于计算两个“时间”间隔
日期间隔:Period,用于计算两个“日期”间隔
TemporalAdjuster
: 时间校正器。有时我们可能需要获取例如:将日期调整到“下一个工作日”等操作。
TemporalAdjusters
: 该类通过静态方法(firstDayOfXxx()/lastDayOfXxx()/nextXxx())提供了大量的常用TemporalAdjuster 的实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 Set<String> zoneIds = ZoneId.getAvailableZoneIds();for (String s : zoneIds) { System.out.println(s); } LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Tokyo" )); System.out.println(localDateTime); ZonedDateTime zonedDateTime = ZonedDateTime.now(); System.out.println(zonedDateTime); ZonedDateTime zonedDateTime1 = ZonedDateTime.now(ZoneId.of("Asia/Tokyo" )); System.out.println(zonedDateTime1); LocalTime localTime = LocalTime.now(); LocalTime localTime1 = LocalTime.of(15 , 23 , 32 ); Duration duration = Duration.between(localTime1, localTime); System.out.println(duration); System.out.println(duration.getSeconds()); System.out.println(duration.getNano()); LocalDateTime localDateTime = LocalDateTime.of(2016 , 6 , 12 , 15 , 23 , 32 ); LocalDateTime localDateTime1 = LocalDateTime.of(2017 , 6 , 12 , 15 , 23 , 32 ); Duration duration1 = Duration.between(localDateTime1, localDateTime); System.out.println(duration1.toDays()); LocalDate localDate = LocalDate.now(); LocalDate localDate1 = LocalDate.of(2028 , 3 , 18 ); Period period = Period.between(localDate, localDate1); System.out.println(period); System.out.println(period.getYears()); System.out.println(period.getMonths()); System.out.println(period.getDays()); Period period1 = period.withYears(2 ); System.out.println(period1); TemporalAdjuster temporalAdjuster = TemporalAdjusters.next(DayOfWeek.SUNDAY); LocalDateTime localDateTime = LocalDateTime.now().with(temporalAdjuster); System.out.println(localDateTime); LocalDate localDate = LocalDate.now().with(new TemporalAdjuster() {@Override public Temporal adjustInto (Temporal temporal) { LocalDate date = (LocalDate) temporal;if (date.getDayOfWeek().equals(DayOfWeek.FRIDAY)) {return date.plusDays(3 ); } else if (date.getDayOfWeek().equals(DayOfWeek.SATURDAY)) {return date.plusDays(2 ); } else {return date.plusDays(1 ); } } }); System.out.println("下一个工作日是:" + localDate);
Java比较器 方式一:自然排序:java.lang.Comparable
Comparable接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序。
实现 Comparable 的类必须实现 compareTo(Object obj)
方法,两个对象即通compareTo(Object obj)
方法的返回值来比较大小。如果当前对象this大于形参对象obj,则返回正整数,如果当前对象this小于形参对象obj,则返回负整数,如果当前对象this等于形参对象obj,则返回零。
实现Comparable
接口的对象列表(和数组)可以通过Collections.sort
或Arrays.sort
进行自动排序。实现此接口的对象可以用作有序映射中的键或有序集合中的元素,无需指定比较器。
对于类 C 的每一个 e1 和 e2 来说,当且仅e1.compareTo(e2) == 0
与e1.equals(e2)
具有相同的 boolean 值时,类 C 的自然排序才叫做与 equals一致。建议(虽然不是必需的)最好使自然排序与 equals
一致。
Comparable 的典型实现:(默认都是从小到大排列的)
String
:按照字符串中字符的Unicode值进行比较
Character
:按照字符的Unicode值来进行比较
数值类型对应的包装类以及BigInteger
、BigDecimal
:按照它们对应的数值大小进行比较
Boolean
:true 对应的包装类实例大于 false 对应的包装类实例
Date
、Time
等:后面的日期时间比前面的日期时间大
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 import java.util.Arrays;class Goods implements Comparable { private String name; private double price; public String getName () { return name; } public void setName (String name) { this .name = name; } public double getPrice () { return price; } public void setPrice (double price) { this .price = price; } public Goods (String name, double price) { this .name = name; this .price = price; } @Override public int compareTo (Object o) { if (o instanceof Goods){ Goods other = (Goods) o; if (this .price > other.price){ return 1 ; }else if (this .price < other.price){ return -1 ; } return 0 ; } throw new RuntimeException("输入的数据类型不一致!" ); } @Override public String toString () { return "Goods{" + "name='" + name + '\'' + ", price=" + price + '}' ; } }public class ComparableTest { public static void main (String[] args) { Goods[] all = new Goods[4 ]; all[0 ] = new Goods("《红楼梦》" , 100 ); all[1 ] = new Goods("《西游记》" , 80 ); all[2 ] = new Goods("《三国演义》" , 140 ); all[3 ] = new Goods("《水浒传》" , 120 ); Arrays.sort(all); System.out.println(Arrays.toString(all)); } }
方式二:定制排序:java.util.Comparator
当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码,或者实现了java.lang.Comparable
接口的排序规则不适合当前的操作,那 么可以考虑使用 Comparator
的对象来排序,强行对多个对象进行整体排序的比较。
重写compare(Object o1,Object o2)
方法,比较o1和o2的大小:如果方法返回正整数,则表示o1大于o2;如果返回0,表示相等;返回负整数,表示o1小于o2。
可以将 Comparator
传递给 sort 方法(如 Collections.sort
或 Arrays.sort
),从而允许在排序顺序上实现精确控制。
还可以使用 Comparator 来控制某些数据结构(如有序 set或有序映射)的顺序,或者为那些没有自然顺序的对象 collection 提供排序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 import java.util.Arrays;import java.util.Comparator;class Goods { private String name; private double price; public String getName () { return name; } public void setName (String name) { this .name = name; } public double getPrice () { return price; } public void setPrice (double price) { this .price = price; } public Goods (String name, double price) { this .name = name; this .price = price; } @Override public String toString () { return "Goods{" + "name='" + name + '\'' + ", price=" + price + '}' ; } }public class ComparableTest { public static void main (String[] args) { Goods[] all = new Goods[4 ]; all[0 ] = new Goods("《红楼梦》" , 100 ); all[1 ] = new Goods("《西游记》" , 80 ); all[2 ] = new Goods("《三国演义》" , 140 ); all[3 ] = new Goods("《水浒传》" , 120 ); Arrays.sort(all, new Comparator<Goods>() { @Override public int compare (Goods o1, Goods o2) { return Double.compare(o1.getPrice(),o2.getPrice()); } }); System.out.println(Arrays.toString(all)); } }
System类
System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang
包。
由于该类的构造器是private
的,所以无法创建该类的对象,也就是无法实例化该类。其内部的成员变量和成员方法都是static
的,所以也可以很方便 的进行调用。
成员变量
System
类内部包含in
、out
和err
三个成员变量,分别代表标准输入流(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)。
成员方法
native long currentTimeMillis()
:该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。
void exit(int status)
:该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等。
void gc()
:该方法的作用是请求系统进行垃圾回收。至于系统是否立刻回收,则取决于系统中垃圾回收算法的实现以及系统执行时的情况。
String getProperty(String key)
:该方法的作用是获得系统中属性名为key的属性对应的值。系统中常见的属性名以及属性的作用如下表所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 String javaVersion = System.getProperty("java.version" ); System.out.println("java的version:" + javaVersion); String javaHome = System.getProperty("java.home" ); System.out.println("java的home:" + javaHome); String osName = System.getProperty("os.name" ); System.out.println("os的name:" + osName); String osVersion = System.getProperty("os.version" ); System.out.println("os的version:" + osVersion); String userName = System.getProperty("user.name" ); System.out.println("user的name:" + userName); String userHome = System.getProperty("user.home" ); System.out.println("user的home:" + userHome); String userDir = System.getProperty("user.dir" ); System.out.println("user的dir:" + userDir);
Math类 java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。
abs
绝对值
acos,asin,atan,cos,sin,tan
三角函数
sqrt
平方根
pow(double a,doble b)
a的b次幂
log
自然对数
exp e
为底指数
max(double a,double b)
min(double a,double b)
random()
返回0.0到1.0的随机数
long round(double a)
double型数据a转换为long型(四舍五入)
toDegrees(double angrad)
弧度—>角度
toRadians(double angdeg)
角度—>弧度
BigInteger与BigDecimal
Integer类作为int的包装类,能存储的最大整型值为2^31-1,Long类也是有限的,最大为2^63-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类都无能为力,更不用说进行运算了。 - - - - - java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。
构造器
BigInteger(String val):根据字符串构建BigInteger对象
常用方法
public BigInteger abs()
:返回此 BigInteger 的绝对值的 BigInteger。
BigInteger add(BigInteger val)
:返回其值为 (this + val) 的 BigInteger
BigInteger subtract(BigInteger val)
:返回其值为 (this - val) 的 BigInteger
BigInteger multiply(BigInteger val)
:返回其值为 (this * val) 的 BigInteger
BigInteger divide(BigInteger val)
:返回其值为 (this / val) 的 BigInteger。整数相除只保留整数部分。
BigInteger remainder(BigInteger val)
:返回其值为 (this % val) 的 BigInteger。
BigInteger[] divideAndRemainder(BigInteger val)
:返回包含 (this / val) 后跟(this % val) 的两个 BigInteger 的数组。
BigInteger pow(int exponent)
:返回其值为 (thisexponent) 的 BigInteger。
一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类。
BigDecimal类支持不可变的、任意精度的有符号十进制定点数。
构造器
public BigDecimal(double val)
public BigDecimal(String val)
常用方法
public BigDecimal add(BigDecimal augend)
public BigDecimal subtract(BigDecimal subtrahend)
public BigDecimal multiply(BigDecimal multiplicand)
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
1 2 3 4 5 6 7 8 9 public void testBigInteger () { BigInteger bi = new BigInteger("12433241123" ); BigDecimal bd = new BigDecimal("12435.351" ); BigDecimal bd2 = new BigDecimal("11" ); System.out.println(bi); System.out.println(bd.divide(bd2, BigDecimal.ROUND_HALF_UP)); System.out.println(bd.divide(bd2, 15 , BigDecimal.ROUND_HALF_UP)); }