常用類和自動封裝

Math

  • 概述
    math是一個封裝了一系列數學運算和三角函數相關的類,執行基本數學運算的方法。數學類是一個最終類,不可以重寫。
    注意:都是static修飾的
  • 功能:
    1. Math.PI
    2. Math.E
    3. Math.abs(num),求絕對值
    4. Math.ceil(num)向上取整
    5. Math.floor(num) 向下取整
    6. Math.max(a,b)求最大值
    7. Math.min(a,b)求最小值
    8. Math.pow(a,b)求a的b次方
    9. Math.sqrt() 開根號
    10. Math.random()隨機生成(0,1)
    11. Math.toRadians() 角度->弧度
    12. Math.toDegrees() 弧度->角度

Data

  • 概述:表示特定的瞬間,是一個毫秒值

  • 構造函數

    1. new Date() 分配 Date 對象並初始化此對象,以表示分配它的時間(精確到毫秒)。
    2. new Date(long)
  • 常用方法:

    1. getTime()返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對象表示的毫秒數。
    2. compareTo(date) 比較兩個日期的順序。
    3. after()測試此日期是否在指定日期之後。
    4. before(date)測試此日期是否在指定日期之前
  • SimpleDateFormat

    1. 概念:它是DataFormat的子類,專門用於對Date進行格式化和解析

    2. 常見的時間模式
      y 年 M月 d 日 H 小時 m分鐘 s秒

    3. 格式化:將Date日期以指定的格式按字符串輸出format(Date)

    4. 解析:將已經格式化後的日期字符串解析成一個Date對象,parse(String)
      注意:要解析的日期字符串的格式必須與SimpleDateFormat指定的格式,否則會發生ParseException

        需求:時間以XXX年XX月XX日XX時XX分XX秒輸出
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日hh時mm分ss秒");
        String time= sdf.format(new Date());
        syso(time);//2019年08月05日07時15分18秒
      
        需求:解析
        String sj="2008年8月8號8時8分8秒";
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd號hh時mm分ss秒");
        Date date=sdf.parse(sj);
        syso(date);//Fri Aug 08 08:08:08 CST 2008
      
        需求是:查看你活到現在的天數
        public static void f1() throws ParseException {
      		Scanner sc = new Scanner(System.in);
      		System.out.println("請輸入你的出生年月:2000年1月1日");
      		String birthday = sc.next();
      		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
      		Date birthDate = sdf.parse(birthday);//將輸入的日期解析
      		Date nowDate = new Date();//獲取當前的日期對象
      		long birthTime = birthDate.getTime();
      		long nowTime = nowDate.getTime();
      		System.out.println((nowTime - birthTime) / 1000.0 / 3600 / 24);
      	}
        ```
      
        //計算距離國慶還有幾天
         String happyDay="2019年10月1日";
         SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
         Date happyDate=sdf.parse(happyDay);
         Date dates= new Date();//獲取當前時間
         long nowTime=dates.getTime();
         long happyDateTime=happyDate.getTime();
         System.out.println((happyDateTime-nowTime)/1000/3600/24);
      
      
      

Calendar

  • 概念:一個操作日曆字段的抽象類
    對象的獲取:getInstance()
    Calendar類是一個抽象類,Calendar提供了一個類方法getInstance,以獲得此類型的一個通用的 對象。Calendar 的 getInstance 方法返回一個 Calendar 對象,其日曆字段已由當前日期和時間初始化:
    Calendar rightNow = Calendar.getInstance();
  Calendar c=Calendar.getInstance();
   	System.out.println(c.get(Calendar.DAY_OF_YEAR));
   	//設置時間,底層是0-11月
   	c.set(Calendar.MONTH, 5);
   	System.out.println(c);
  • 常用方法
    1. getTime():獲取Date對象
    2. getTimeInMillis():獲取毫秒值,同Date的gettime(),同System.currentTimeMillis()
    3. set(field,value):對指定的時間域設置值
    4. set(year,month,date,hour,minute,second);
    5. get(filed):獲取指定時間域上的值
    6. add(filed,amount):爲指定的時間域設置偏移值
      需求是:查看你活到現在的天數
      private static void t1_2() {
       	Scanner sc = new  Scanner(System.in);
       	System.out.println("請輸入年份");
       	int year = sc.nextInt();
       	System.out.println("請輸入月份");
       	int month = sc.nextInt();
       	System.out.println("請輸入日子");
       	int date = sc.nextInt();
       	Calendar c = Calendar.getInstance();
       	c.set(year, month, date);
       	long birthTime = c.getTimeInMillis();
       	long nowTime = System.currentTimeMillis();
       	System.out.println((nowTime-birthTime) / 1000/ 3600 / 24);
       }
      
      //需求:求當前時間到國慶節的天數
       	Scanner sc =new Scanner(System.in);
       	System.out.println("請輸入年份");
       	int year =sc.nextInt();
       	System.out.println("請輸入月份");
       	int month =sc.nextInt();
       	System.out.println("請輸入日");
       	int day =sc.nextInt();
       	//獲取日曆對象
       	Calendar calendar= Calendar.getInstance();
       	calendar.set(year, month, day);
       	long scTime=calendar.getTimeInMillis();
       	long nowTime=System.currentTimeMillis();
       	System.out.println((scTime-nowTime)/3600/1000/24);
      

**基本數據類型的包裝類

  • 概述:
    基本數據類型的創建和使用非常方法,但是我們無法通過基礎數據類型調用一些功能來操作這些數據。
    現在將這些基本數據類型封裝成一個類,就可以在這個類中定義一些專門操作這些數據的功能
    這種類就是基本數據類型的包裝類

  • 基本類型與包裝類的對應關係
    byte Byte
    short Short
    int Integer
    long Long
    float Float
    double Double
    char Character
    boolean Boolean

  • +裝箱和拆箱
    裝箱:基本類型->包裝類

    1. 使用包裝類的構造函數
      new Integer(int)
      new Integer(string):當字符串不是整數形式的字符串時會發生NumberFormatException異常
         int i = 3;
         Integer i1 = new Integer(i);
         System.out.println(i1+1);//4
            
         Integer i2=new Integer("333");
         System.out.println(i2);//333
         
         Integer i2=new Integer("bbb");
         System.out.println(i2);//NumberFormatException
      
      
    2. 使用包裝類的靜態方法
      valueOf(int)
      valueOf(String)
      int j = 4;
      Integer i3 = Integer.valueOf(j);
      System.out.println(i3);//4
      Integer i4 = Integer.valueOf("444");
      System.out.println(i4);///4
      

    拆箱:包裝類->基本類型

    1. 使用包裝類的成員方法
      intValue(Integer);當字符串不是整數形式的字符串時會發生NumberFormatException異常
        System.out.println("===========拆箱===========");
        Integer i5 = new Integer(999);
        int k = i5.intValue();
        System.out.println(k*k);//998001
      
  • 自動裝箱和自動拆箱
    jdk1.5之後,基本類型和包裝類可以實現自動轉換
    自動裝箱:它隱含了new Integer(t);
    int t=3; Integer i=t;
    自動拆箱:它隱含了i.intValue();
    Integer i=new Integer(666); int k=i;

  • 包裝類的功能

    1. 獲取指定類型的最大、最小值
      Integer.MAX_VALUE
    2. toBinaryString(int)
    3. toOctalString(int)
    4. toHexString(int)
  • 基本類型/包裝類與字符串的轉換

    1. 空串的連接
    2. 包裝類的靜態tostring(數據)方法
    3. string的靜態valueOf(數據)方法
  • string->基本類型/包裝類

    1. 包裝類的構造方法
    2. 包裝類的靜態valueOf(string)方法
    3. 包裝類的靜態parseXXX(string)方法
       String s1 = Integer.toString(333);
     	String s2 = 333 + "";
     	String s3 = String.valueOf(444);
     	
     	System.out.println("------------------------");
     	
     	Integer i1 = new Integer("666");
     	int i2 = new Integer("666");
     	Integer i3 = Integer.valueOf("555");
     	int i4 = Integer.parseInt("999");
     	System.out.println(i4 + 1);
     	
    

注意點:在面試的的時候有點噁心的,

 System.out.println("-----------噁心心-----------");
 Integer i = Integer.valueOf("-5");
 System.out.println(i+5);//0

Integer i = Integer.valueOf("+5");
 System.out.println(i+5);//10

Integer i = Integer.valueOf("1-5");
 System.out.println(i+5);//這個是報錯的

 Integer i = Integer.valueOf("-5");
 System.out.println(i+5);//0

Integer i = Integer.valueOf("+5");
 System.out.println(i+5);//10

Integer i = Integer.valueOf("1-5");
 System.out.println(i+5);//這個是報錯的

自動裝拆箱案例
ArrayList<Integer> list= new ArrayList<>();
//ArrayList集合無法存儲基本數據類型,可以存儲Integer包裝類
list.add(1);//自動裝箱 list.add(new Integer(1));
int j=list.git(i);//自動拆箱,list.get(i).intValue()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章