[JavaSE]第八章 Java常用类

8.1 字符串相关的类

8.1.1 String

  • String:字符串,使用一对 “” 引起来表示

  • String 声明为 final 的,不可被继承

  • String 实现了 Serializable 接口:表示字符串是支持序列化的

  • String 实现了 Comparable 接口:表示 String 可以比较大小

  • String 内部定义了 final char[] value 用户存储字符串数据

  • String:代表不可变的字符序列。简称:不可变性。

    • 当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的 value 进行赋值

    • 当对现有的字符串进行连接操作时,也需要重新为指定内存区域赋值,不能使用原有的 value 进行赋值

    • 当调用 String 的 replace() 修改指定字符或字符串时,也需要重新指定内存区域赋值

  • 通过字面量的方式(区别于 new)给一个字符串赋值,此时的字符串值声明在字符串常量池中

  • 字符串常量池中是不会存储相同内容的字符串的。

	public void test1() {
        String s1 = "abc";//字面量的定义方式
        String s2 = "abc";
        s1 = "hello";

        System.out.println(s1 == s2);//false

        System.out.println(s1);//hello
        System.out.println(s2);//abc

        String s3 = "abc";
        System.out.println(s2 == s3);//true
        s3 += "def";
        System.out.println(s3);//abcdef

        String s4 = "abc";
        String s5 = s4.replace("a", "m");
        System.out.println(s4);//abc
        System.out.println(s5);//mbc
    }
  • String 的实例化方式:
    • 方式一:通过字面量定义的方式
    • 方式二:通过 new + 构造器的方式
  • String s = new String(“abc”);方式创建对象,在内存中创建了几个对象?
    • 两个:一个是堆空间中 new 结构,另一个是 char[] 对应的常量池中的数据:“abc”
public void test2() {
        //通过字面量定义的方式:此时的s1和s2的数据JavaEE声明在方法区中的字符串常量池中
        String s1 = "JavaEE";
        String s2 = "JavaEE";
        //通过 new + 构造器的方式:此时的 s3 和 s4 保存的是地址值,是数据在堆空间中开辟空间以后对应的地址值
        String s3 = new String("JavaEE");
        String s4 = new String("JavaEE");

        System.out.println(s1 == s2);//true
        System.out.println(s1 == s3);//false
        System.out.println(s3 == s4);//false
        System.out.println(s3.equals(s4));//true

        System.out.println("**************************");
        Person p1 = new Person("Tom", 12);
        Person p2 = new Person("Tom", 12);

        System.out.println(p1.equals(p2));//false
        System.out.println(p1.name.equals(p2.name));//true
        System.out.println(p1.name == p2.name);//true

        p1.name = "Jerry";
        System.out.println(p2.name);//Tom
    }
  • 常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。
  • 只要其中有一个是变量,结果就在堆中。
  • 如果拼接的结果调用 intern()方法,返回值就在常量池中
	public void test3() {
        String s1 = "JavaEE";
        String s2 = "Hadoop";

        String s3 = "JavaEEHadoop";
        String s4 = "JavaEE" + "Hadoop";
        String s5 = s1 + "Hadoop";
        String s6 = "JavaEE"+ s2;
        String s7 = s1 + s2;
        final String s8 = "Hadoop";
        String s9 = "JavaEE" + s8;

        System.out.println(s3 == s4);//true
        System.out.println(s3 == s5);//false
        System.out.println(s3 == s6);//false
        System.out.println(s3 == s7);//false
        System.out.println(s3 == s9);//true
        System.out.println(s5 == s6);//false
        System.out.println(s5 == s7);//false

        String s10 = s5.intern();//返回值得到的 s8 使用的常量值中已经存在的 "JavaEEHadoop"
        System.out.println(s3 == s10);//true
    }
  • String 类与其他结构之间的转换

    public class StringTest2 {
    
        /*
        String 与 byte[] 之间的转换
        String -> byte[]:调用 String 的 getBytes()
         */
        @Test
        public void test3() {
            String s1 = "abc123中国";
            byte[] bytes = s1.getBytes();
            //[97, 98, 99, 49, 50, 51, -28, -72, -83, -27, -101, -67]
            System.out.println(Arrays.toString(bytes));
            byte[] gbks = null;
            try {
                //[97, 98, 99, 49, 50, 51, -42, -48, -71, -6]
                gbks = s1.getBytes("GBK");
                System.out.println(Arrays.toString(gbks));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
    
            String s2 = new String(bytes);
            System.out.println(s2);
    
            String s3 = null;
            try {
                //abc123�й�   编码集和解码集不一致
                assert gbks != null;
                s3 = new String(gbks, "GBK");//abc123中国
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            System.out.println(s3);
    
            String s4 = String.valueOf(bytes);
            System.out.println(s4);//[B@5e8c92f4
        }
    
        /*
        String 与 char[] 之间的转换
        String -> char[] 调用 toCharArray()
        char[] -> String:调用 String 的构造器
         */
        @Test
        public void test2() {
            String s1 = "abc123";
    
            char[] chars = s1.toCharArray();
            System.out.println(Arrays.toString(chars));
    
            char[] c1 = new char[]{'h', 'e', 'l', 'l', 'o'};
            String s2 = String.valueOf(c1);
            System.out.println(s2);
    
            String s3 = new String(c1);
            System.out.println(s3);
    
            //abc123 -> a21cb3
            System.out.print(s1.charAt(0));
            for (int i = s1.length()-2; i > 0; i--) {
                System.out.print(s1.charAt(i));
            }
            System.out.print(s1.charAt(s1.length()-1));
        }
    
        /*
        String 与基本数据类型、包装类之间的转换
         */
        //String -> 基本数据类型、包装类:调用包装类的静态方法:parseXxx(str)
        //基本数据类型、包装类 -> String:调用 String 的静态方法:valueOf(xxx)
        @Test
        public void test1() {
    
            String s1 = "123";
    
            int i1 = Integer.parseInt(s1);
            System.out.println(i1);
    
            int i2 = 1234;
            String s2 = String.valueOf(i2);
            System.out.println(s2);
            String s4 = i2 + "";
        }
    }
    

8.1.2 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(不包含)的一个子字符串。
	@Test
    public void test1() {

        String s1 = "HelloWorld";
        System.out.println(s1.length());

        System.out.println(s1.charAt(0));

        //s1 = "";
        System.out.println(s1.isEmpty());

        String s2 = s1.toLowerCase();
        System.out.println(s1);//HelloWorld
        System.out.println(s2);//helloworld

        String s3 = s1.toUpperCase();
        System.out.println(s3);//HELLOWORLD

        String s4 = " he llo world  ";
        System.out.println("---"+s4+"---");//--- he llo world  ---
        String s5 = s4.trim();
        System.out.println("---"+s5+"---");//---he llo world---
    }

    @Test
    public void test2() {
        String s1 = "HelloWorld";
        String s2 = "helloworld";

        System.out.println(s1.equals(s2));//false
        System.out.println(s1.equalsIgnoreCase(s2));//false

        String s3 = "abc";
        String s4 = s3.concat("def");
        System.out.println(s4);//abcdef

        String s5 = "abc";
        String s6 = new String("abe");
        //涉及到字符串排序
        System.out.println(s5.compareTo(s6));//-2

        String s7 = "人体软体组织";
        System.out.println(s7.substring(4));//组织
        System.out.println(s7.substring(2, 6));//软体组织
    }
  • 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):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
  • 注:indexOflastIndexOf方法如果未找到都是返回-1
	@Test
    public void test3() {
        String str1 = "helloworld";
        boolean b1 = str1.endsWith("ld");//true
        System.out.println(b1);

        System.out.println(str1.startsWith("he"));//true

        System.out.println(str1.startsWith("he", 1));//false
        System.out.println(str1.startsWith("el", 1));//true

        String str2 = "wo";
        System.out.println(str1.contains(str2));//true
        System.out.println(str2.contains(str1));//false

        System.out.println(str1.indexOf("lo"));//3
        System.out.println(str1.indexOf("lol"));//-1
        System.out.println(str1.indexOf("lo", 4));//-1

        System.out.println(str1.lastIndexOf("o"));//6
        System.out.println(str1.lastIndexOf("o", 2));//-1
        System.out.println(str1.lastIndexOf("o", 5));//4

        //什么情况下,indexOf(str) 和 lastIndexOf(str) 返回值相同?
        //情况一:存在唯一的一个 str。情况二:不存在 str
    }
  • 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个,如果超过了,剩下的全部都放到最后一个元素中。

    @Test
    public void test4() {
        String str1 = "H1e,l,l2o,P3y,th4,o5n,Ja,va,H6ad,oop";
        String str2 = str1.replace(",", "");
        System.out.println(str2);//H1ell2o,P3yth4o5n,Java,H6adoop

        String s3 = str2.replaceAll("\\d", "");
        System.out.println(s3);//Hello,Python,Java,Hadoop

        String str4 = "3192373@gmail.com";
        boolean matches = str4.matches("\\w+@gmail.com");
        System.out.println(matches);
        
    }

8.1.3 StringBuffer 与 StringBuilder

  • String、StringBuffer、StringBuilder 三者的异同?

    • String:不可变的字符序列;底层使用 char[] 进行存储
    • StringBuffer:可变的字符序列;线程安全的,效率低;底层使用 char[] 进行存储
    • 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();//char[] value = new char[16]
      sb1.append('a');//value[0] = 'a';
      sb1.append('b');//value[1] = 'b';
      StringBuffer sb2 = new StringBuffer("abc");//char[] value = new char["abc".length() + 16]
      
    • 扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层的数组。默认情况下,扩容为原来容量的 2 倍 + 2,同时将原有数组中的元素复制到新数组中

    • 指导意义:开发中建议使用:StringBuffer(int capacity) 或 StringBuilder(int capacity)

  • 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() :把当前字符序列逆转
    • public int indexOf(String str)
    • public String substring(int start,int end)
    • public int length()
    • public char charAt(int n)
    • public void setCharAt(int n ,char ch)
	/*	
	总结:
    增:append()
    删:delete()
    改:setCharAt(),replace()
    查:charAt()
    插入:insert()
    长度:length()
    遍历:for + charAt()
     */
    @Test
    public void test2() {
        StringBuffer s1 = new StringBuffer("abc");
        s1.append(1);
        s1.append('1');
        System.out.println(s1);//abc11

        s1.delete(3, 5);
        System.out.println(s1);//abc

        s1.replace(0, 1, "e");
        System.out.println(s1);//ebc

        s1.insert(2, false);
        System.out.println(s1);//ebfalsec
        System.out.println(s1.length());//8

        s1.reverse();
        System.out.println(s1);//ceslafbe

        String s2 = s1.substring(1, 3);
        System.out.println(s2);//es
    }
  • String、StringBuffer、StringBuilder 三者的效率:
    • 从高到低排序:StringBuilder > StringBuffer > String

8.2 日期时间 API

8.2.1 JDK8 之前日期时间 API

Date 类的使用
    @Test
    public void test1() {
        long time = System.currentTimeMillis();
        //返回当前时间与 1970 年 1 月 1 日 0 时 0 分 0 秒之间以毫秒为单位的时间差
        //称为时间戳
        System.out.println(time);
    }
/*
    java.util.Date 类
    1.两个构造器的使用
        >构造器一:Date():创建一个对应当前时间的 Date 对象
        >构造器二:创建指定毫秒数的 Date 对象

    2.两个方法的使用
        >toString():显示当前的年月日、时分秒
        >getTime():获取当前 Date 对象对应的时间戳

    3.java.sql.Date 对应着数据库中的日期类型的变量
        >如何实例化
        >如何将 java.util.Date 对象转换为 java.sql.Date 对象
     */
    @Test
    public void test2() {
        //构造器一:Date():创建一个对应当前时间的 Date 对象
        Date date1 = new Date();
        System.out.println(date1.toString());//Sun Jan 02 13:36:33 CST 2022

        System.out.println(date1.getTime());//1641101880288

        //构造器二:创建指定毫秒数的 Date 对象
        Date date2 = new Date(273423987423L);
        System.out.println(date2);//Thu Aug 31 23:06:27 CST 1978

        //创建java.sql.Date 对象
        java.sql.Date date3 = new java.sql.Date(2324294324239L);
        System.out.println(date3);

        //如何将 java.util.Date 对象转换为 java.sql.Date 对象
        Date date4 = new Date();
        java.sql.Date date5 = new java.sql.Date(date4.getTime());
        System.out.println(date5);
    }
Calendar 日历类(抽象类)的使用
public class CalendarTest {

    @Test
    public void calendarTest1() {
        //1.实例化
        //方式一:创建其子类(GregorianCalendar)的对象
        //方式二:调用其静态方法 getInstance()
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);//java.util.GregorianCalendar[time=1641198572341,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=0,WEEK_OF_YEAR=2,WEEK_OF_MONTH=2,DAY_OF_MONTH=3,DAY_OF_YEAR=3,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=29,SECOND=32,MILLISECOND=341,ZONE_OFFSET=28800000,DST_OFFSET=0]
        System.out.println(calendar.getClass());//class java.util.GregorianCalendar

        //2.常用方法
        //get()
        //DAY_OF_MONTH 本月的第几天
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        //DAY_OF_WEEK 本周的第几天
        int week_day = calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println(week_day);

        //set()
        //设置今天为本月的第22天
        calendar.set(Calendar.DAY_OF_MONTH, 22);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //add()
        calendar.add(Calendar.DAY_OF_MONTH, -3);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);

        //getTime():日历类 -> Date
        Date date = calendar.getTime();
        System.out.println(date);

        //setTime():Date -> 日历类
        Date date1 = new Date();
        calendar.setTime(date1);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
    }
}
SimpleDateFormat 类的使用
public class SimpleDateFormatTest {

    /*
    SimpleDateFormat 的使用:SimpleDateFormat 对日期 Date 类的格式化和解析
    1.两个操作:
    1.1 格式化:日期 -> 字符串
    1.2 解析:格式化的逆过程,字符串 -> 日期
     */
    @Test
    public void SimpleDateFormatTest1() throws ParseException {
        //实例化 SimpleDateFormat
        SimpleDateFormat sdf = new SimpleDateFormat();
        System.out.println(sdf);//java.text.SimpleDateFormat@b5341f2a

        Date date = new Date();
        System.out.println(date);//Sun Jan 02 20:35:36 CST 2022

        //格式化:日期 -> 字符串
        String format = sdf.format(date);
        System.out.println(format);//22-1-2 下午8:35

        //解析:格式化的逆过程,字符串 -> 日期
        String str = "22-1-2 下午8:39";
        Date date1 = sdf.parse(str);
        System.out.println(date1);

        //按照指定的方式进行格式化
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdf1.format(date));//2022.01.02

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println(sdf2.format(date));//2022-01-02 08:42:37
    }

    /*
    练习一:字符串 "2020-01-02" 转换为 java.sql.Date
     */
    @Test
    public void test2() throws ParseException {
        String s1 = "2020-01-02";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(s1);

        java.sql.Date date2 = new java.sql.Date(date.getTime());
        System.out.println(date2);
    }

    /*
    练习二:“三天打渔两天晒网” 2001-01-01  2021-03-14 打渔?晒网
     */
    @Test
    public void test3() throws ParseException {
        String startTime = "2001-01-01";
        String endTime = "2021-03-14";

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date startParse = format.parse(startTime);
        Date endParse = format.parse(endTime);

        long day = (endParse.getTime() - startParse.getTime()) / (1000 * 60 * 60 * 24) + 1;
        System.out.println(day);
        int c = (int) day % 5;
        System.out.println(day % 5);
        switch (c) {
            case 0:
            case 4:
                System.out.println("晒网");
                break;
            default:
                System.out.println("打渔");
        }
    }

    @Test
    public void test4() throws ParseException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = format.parse("2019-12-31 23:59:59");
        System.out.println(parse);

        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse2 = format2.parse("2019-12-31 23:59:59");
        System.out.println(parse2);

        SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd");
        Date parse3 = format3.parse("2020-01-01");
        System.out.println(parse3);
    }
}

8.2.2 JDK8 日期时间 API

LocalDate、LocalTime、LocalDateTime 的使用
	@Test
    public void localTest() {
        //now():获取当前的日期、时间、日期 + 时间
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);//2022-01-03

        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);//16:52:44.516

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);//2022-01-03T16:52:44.516

        //of():设置指定的时间。没有偏移量
        LocalDateTime time = LocalDateTime.of(2020, 10, 6, 12, 15);
        System.out.println(time);//2020-10-06T12:15

        //getXxx()
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDate.getDayOfYear());
        System.out.println(localDate.getDayOfYear());

        //体现不可变性
        //withXxx():设置相关的属性
        LocalDate localDate1 = localDate.withDayOfMonth(12);
        System.out.println(localDate);//2022-01-03
        System.out.println(localDate1);//2022-01-12

        LocalDateTime localDateTime1 = localDateTime.withHour(13);
        System.out.println(localDateTime1);//2022-01-03T13:02:51.496
        System.out.println(localDateTime);//2022-01-03T17:02:51.496

        //不可变性
        LocalDateTime localDateTime2 = localDateTime.plusMonths(3);
        System.out.println(localDateTime2);//2022-04-03T17:04:25.763
        System.out.println(localDateTime);//2022-01-03T17:04:25.763
    }
Instant 的使用
	@Test
    public void test2() {
        //now():获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);//2022-01-03T09:18:44.509Z

        //添加时间的偏移量
        OffsetDateTime dateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(dateTime);

        //toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数
        long milli = instant.toEpochMilli();
        System.out.println(milli);//1641201958675

        Instant instant1 = Instant.ofEpochMilli(1641201807377L);
        System.out.println(instant1);//2022-01-03T09:23:27.377Z
    }
DateTimeFormatter 的使用
	@Test
    public void test3() {
        //方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
        System.out.println(formatter);
        //格式化:日期 -> 字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);//2022-01-03T17:32:11.319
        String format = formatter.format(localDateTime);
        System.out.println(format);//2022-01-03
        //解析:字符串 -> 日期
        TemporalAccessor parse = formatter.parse("2022-01-03");
        System.out.println(parse);//{},ISO resolved to 2022-01-03


        //方式二:本地化相关的格式。
        //如:ofLocalizedDateTime(FormatStyle.LONG)
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        System.out.println(formatter1);//Localized(SHORT,SHORT)
        String format1 = formatter1.format(localDateTime);
        System.out.println(format1);//22-1-3 下午5:46

        //如:ofLocalizedDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        String format2 = formatter2.format(localDateTime);
        System.out.println(format2);//2022-1-3


        //方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format3 = formatter3.format(localDateTime);
        System.out.println(format3);//2022/01/03
        //解析
        TemporalAccessor parse1 = formatter3.parse("2022-01-03 06:09:18");
        System.out.println(parse1);//{},ISO resolved to 2001-01-03
    }

8.3 Java 比较器

Java中的对象,正常情况下,只能进行比较:== 或 != 不能使用 > 或 < 但是在开发场景中,我们需要对多个对象进行排序,言外之意,就需要比较对象的大小

如何实现?使用两个接口中的任意一个:Comparable 或 Comparator

  • Java 实现对象排序的方式有两种
    • 自然排序:java.lang.Comparable
    • 定制排序:java.util.Comparator
  • Comparable 接口与 Comparator 的使用的对比:
    • Comparable 接口的方式一旦一定,保证 Comparable 接口实现类的对象在任何位置都可以比较大小
    • Comparator 接口属于临时性的比较

8.3.1 Comparable 的使用

  • String、包装类实现了 Comparable 接口,重写了 compareTo(obj) 方法
  • String、包装类重写 compareTo() 方法以后,进行了从小到大的排列
  • 重写 compareTo(obj) 的规则:
    • 如果当前对象 this 大于形参对象 obj,则返回正整数,
    • 如果当前对象 this 小于形参对象 obj,则返回负整数,
    • 如果当前对象 this 等于形参对象 obj,则返回零。
  • 对于自定义类来说,如果需要排序,我们可以让自定义类实现 Comparable 接口,重写 compareTo(obj) 方法。在 compareTo(obj) 方法中指明如何排序
    @Test
    public void test2() {
        Goods[] arr = new Goods[5];
        arr[0] = new Goods("联想", 20);
        arr[1] = new Goods("华为", 60);
        arr[2] = new Goods("小米", 10);
        arr[3] = new Goods("戴尔", 40);
        arr[4] = new Goods("微软", 40);
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }

	@Override
    public int compareTo(Object o) {
        if (o instanceof Goods) {
            Goods goods = (Goods) o;
            //方式一:
            if (goods.price > this.price) {
                return 1;
            } else if (goods.price < this.price) {
                return -1;
            } else {
                //return 0;
                return this.name.compareTo(goods.name);
            }

            //方式二:
            //return Double.compare(goods.price, this.price);
        }
        throw new RuntimeException("传入的数据类型不一致");
    }

8.3.2 Comparator 的使用

当元素的类型没有实现 java.lang.Comparable 接口而又不方便修改代码,或者实现了 java.lang.Comparable 接口的排序规则不适合当前的操作,那么可以考虑使用 Comparator 的对象来排序,强行对多个对象进行整体排序的比较。

  • 重写compare(Object o1,Object o2)方法,比较 o1 和 o2 的大小:
    • 如果方法返回正整数,则表示 o1 大于 o2
    • 如果返回 0,表示相等
    • 返回负整数,表示 o1 小于 o2
    @Test
    public void test1() {
        String[] arr = new String[]{"aa", "zz", "cc", "bb", "uu", "ww", "dd", "nn"};
        Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return -o1.compareTo(o2);
            }
        });
        System.out.println(Arrays.toString(arr));
    }
	@Test
    public void test2() {
        Goods[] arr = new Goods[5];
        arr[0] = new Goods("联想", 20);
        arr[1] = new Goods("华为", 60);
        arr[2] = new Goods("小米", 10);
        arr[3] = new Goods("戴尔", 40);
        arr[4] = new Goods("微软", 40);
        Arrays.sort(arr, new Comparator<Goods>() {
            @Override
            public int compare(Goods o1, Goods o2) {
                if (o1.getName().equals(o2.getName())) {
                    return -Double.compare(o1.getPrice(), o2.getPrice());
                } else {
                    return -o1.getName().compareTo(o2.getName());
                }
            }
        });
        System.out.println(Arrays.toString(arr));
    }

8.4 System 类

  • System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang包。

  • 成员方法:

    • native long currentTimeMillis():返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数

    • void exit(int status):退出程序。其中status的值为0代表正常退出,非零代表异常退出

    • void gc():请求系统进行垃圾回收。至于系统是否立刻回收,则取决于系统中垃圾回收算法的实现以及系统执行时的情况

    • String getProperty(String key):获得系统中属性名为 key 的属性对应的值。系统中常见的属性名以及属性的作用如下表所示:

      属性名属性说明
      java.versionJava 运行时环境版本
      java.homeJava 安装目录
      os.name操作系统的名称
      os.version操作系统的版本
      user.name用户的账户名称
      user.home用户的家目录
      user.dir用户的当前工作目录
      	@Test
          public void test1() {
              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);
          }
      

8.5 Math 类

	@Test
    public void test2() {
        //Math 类中包含 E 和 PI 两个静态常量,正如它们名字所暗示的,
        //它们的值分别等于 e(自然对数)和 π(圆周率)。
        System.out.println("E 常量的值:" + Math.E);
        System.out.println("PI 常量的值:" + Math.PI);

        //abs()返回绝对值
        System.out.println(Math.abs(23));
        //max():返回较大的数
        System.out.println(Math.max(12, 23));
        //max():返回较小的数
        System.out.println(Math.min(12, 23));
        //将参数加上 1/2 后返回与参数最近的整数,强制转换为长整型
        System.out.println(Math.round(44.6));//45
        //返回以 3 为底数,以 4 为指数的幂值
        System.out.println(Math.pow(3, 4));
        //sqrt():返回平方根
        System.out.println(Math.sqrt(16));
        //cbrt():返回立方根
        System.out.println(Math.cbrt(125));
    }

8.6 BigInteger 与 BigDecimal

    @Test
    public void test3() {
        //java.math包的BigInteger可以表示不可变的任意精度的整数
        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));
        System.out.println(bd.divide(bd2, BigDecimal.ROUND_HALF_UP));
        System.out.println(bd.divide(bd2, 2, BigDecimal.ROUND_HALF_UP));

        BigDecimal bigDecimal = new BigDecimal("1232423.23472934273");
        BigDecimal decimal = bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP);//保留一位小数
        System.out.println(decimal);

        //保留两位小数
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        String format = decimalFormat.format(234293742.234729372);
        System.out.println(format);

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值