day13常用API2Calendar、System類、StringBuilder、StringBuiffer、包裝類、Collection集合

Calendar類

Calendar是抽象類,由于語言的敏感性,Calendar類在創(chuàng)建對象時并非直接創(chuàng)建,而是通過靜態(tài)方法創(chuàng)建,返回子類對象。
使用默認(rèn)時區(qū)和語言環(huán)境獲得一個日歷

Calendar calendar = Calendar.getInstance();

常用方法


  • public int get(int field),返回給定字段的值
public class TestCalendar {
    public static void main(String[] args) {
        //創(chuàng)建Calendar對象
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);
        //設(shè)置年
        int year = calendar.get(Calendar.YEAR);
        //設(shè)置月
        int month = calendar.get(Calendar.MONTH) + 1;
        //設(shè)置日
        int day = calendar.get(calendar.DAY_OF_MONTH);
        System.out.println(year + "月" + month + "月" + day + "日");

    }
}

  • public void set(int field,int value) 將給定的日歷字段設(shè)置成給定的值
public class TestCalendar2 {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH) ;
        System.out.println(year+"年"+month+"月"+dayOfMonth+"日"); // 2020年7月28日
        calendar.set(Calendar.YEAR, 2021);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH) ;
        System.out.println("set之后");
        System.out.println(year+"年"+month+"月"+dayOfMonth+"日"); // 2020年7月28日
    }
}
  • public abstract void add(int field,int amount) :根據(jù)日歷的規(guī)則,為給定的日歷字典添加或者減去指定的時間量
public class TestCalendar3 {
    public static void main(String[] args) {
        Calendar calendar=Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(year+"年"+month+"月"+dayOfMonth+"日");//2020年7月28日

        calendar.add(Calendar.DAY_OF_MONTH,2);
        calendar.add(Calendar.YEAR,-3);
        year = calendar.get(Calendar.YEAR);
        month= calendar.get(Calendar.MONTH)+1;
        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println("add之后");
        System.out.println(year+"年"+month+"月"+dayOfMonth+"日");

    }
}

  • public Date getTime(): 返回一個表示此Calendar時間值(從歷元到現(xiàn)在的毫秒偏移量)的Date對象
public class TestCalendar4 {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        System.out.println(date); // Tue Jul 28 09:13:15 CST 2020

        Date date1 = new Date();
        System.out.println(date1); // Tue Jul 28 09:13:15 CST 2020
    }
}

案例獲取當(dāng)月的最后一天

public class TestCalendar5 {
    public static void main(String[] args) throws Exception {
        // calendar.getActualMaximum();// 獲取給定日歷的最大可能值
        String lastDayOfMonth = getLastDayOfMonth("2020-06");
        System.out.println(lastDayOfMonth);
        int lastDay = lastDay(2020, 07);
        System.out.println(lastDay);
    }
    /**
     * 編寫一個方法, 根據(jù)給定字符串yyyy-MM獲取當(dāng)月的最后一天
     */
    public static String getLastDayOfMonth(String yearmonth) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = sdf.parse(yearmonth);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return "" + lastDay;
    }
    public static int lastDay(int year, int month){
        Calendar calendar = Calendar.getInstance();
        calendar.clear(); // 清除所有日期數(shù)據(jù)
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return lastDay;
    }
}

System類

是java.lang包下的類,提供大量的靜態(tài)方法,可以獲取與系統(tǒng)相關(guān)的信息或者系統(tǒng)級操作,常用方法有;

  • 1.System.currentTimeMillis() 獲取當(dāng)前系統(tǒng)時間與1970年1月1日00點00分之間的毫秒值
public class TestScanner {
    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis()); //1595902117467
    }
}

練習(xí):
驗證一下for循環(huán)打印1-9999

public class TestSystem1 {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 9999; i++) {
            System.out.print(i);
        }
        System.out.println();
        long end = System.currentTimeMillis();
        System.out.println("耗時" + (end - start));

    }
}
  • 2.arrayCopy將數(shù)組中指定的數(shù)據(jù)拷貝到另一個數(shù)組中


public class TestSystem2 {
    public static void main(String[] args) {
        //將src中前三個元素,賦值到dest的前三個位置上復(fù)制元素前
        //操作之后:[1,2,3,4,5] dest[1,2,3,9,10]
        int[] src = new int[]{1,2,3,4,5};
        int[] dest = new int[]{6,7,8,9,10};
        System.arraycopy(src,0,dest,0,3);

        System.out.println(Arrays.toString(dest));
    }
}

StringBuilder類

由于String對象的不可變,字符串在進行拼接時,總會創(chuàng)建新的對象,字符串是常量,它的值在創(chuàng)建之后不能修改,如果對字符串進行頻繁的操作,每次都會創(chuàng)建新的String對象,既耗時又浪費空間,為了解決這一問題,可以使用Java.lang.StringBuilder類.

StringBuilder又稱為可變字符序列,通過某些方法調(diào)用可以改變該序列的長度和內(nèi)容。

構(gòu)造方法:

  • public StringBuilder()
  • public StringBuilder(String str)
append方法
public class TestStringBuilder1 {
    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder();
        StringBuilder builder1 = builder.append("hello");
        System.out.println("builder =" + builder );
        System.out.println("builder1 =" + builder1 );
        System.out.println(builder == builder1); //true
        //可以添加任何類型
        builder.append("啥呀");
        builder.append("world");
        builder.append(true);
        builder.append(1000);
        //在我們開發(fā)中,會遇到掉用一個方法后,返回一個對象的情況,
        // 然后使用返回的對象繼續(xù)調(diào)用方法
        //這種我們可以使用鏈?zhǔn)骄幊痰姆绞?        builder.append("haha").append("哈哈").append("true").append(1212);
        System.out.println("builder =" + builder );
    }
}
toString方法

通過toString方法,StringBuilder對象將會轉(zhuǎn)換為不可變的String對象


三者區(qū)別

包裝類

Java提供兩種類型系統(tǒng),基本類型和引用類型。使用基本類型在于效率,然而,很多情況會創(chuàng)建對象使用,因為對象可以做更多的功能,也就有了對應(yīng)的包裝類。


  • 裝箱和拆箱
    基本類型與對應(yīng)的包裝類來回轉(zhuǎn)換的過程
    裝箱:基本到包裝
    拆箱:包裝到基本
public class BaoZhuangDemo {
    public static void main(String[] args) {
        Integer integer = new Integer(4); //基本====》包裝
        int i = integer.intValue();  //包裝變基本
    }
}

  • 自動裝箱和自動拆箱
    基本類型和包裝類的裝箱和拆箱可以自定義完成
//        Integer integer = new Integer(4); //基本====》包裝
//        int i = integer.intValue();  //包裝變基本
        Integer i = 4;   //自動裝箱
        i = i + 4; //自動拆箱
        int a = 100;
        System.out.println("a的值" + a);  

基本類型與字符串之間的轉(zhuǎn)換

public class Demo {
    public static void main(String[] args) {
        //“123”   123
        //基本類型轉(zhuǎn)為String類型
        int a = 100;
        String  b = " " + 100;
        System.out.println(b); //字符串100
        //String 類型轉(zhuǎn)換為基本類型
        //包裝類都有parseXxx靜態(tài)方法可以將字符串參數(shù)轉(zhuǎn)換為對應(yīng)的基本類型
        int c = Integer.parseInt("120");
        System.out.println(c);
        Integer d = Integer.valueOf("133");
        System.out.println(d);
    }
}

Collection集合

  • 集合:集合是Java中提供的一種容器,可以用來存儲多個數(shù)據(jù)
    與數(shù)組的區(qū)別:
  • 數(shù)組的length是固定的,集合可變
  • 數(shù)組中存儲的是同一類型的元素,集合存儲的都是對象,而且對象的類型可以不一致,在開發(fā)的過程中,當(dāng)對象多的時候,使用集合進行存儲。

集合分類

按照存儲結(jié)構(gòu)分為兩大類:

  • 單列集合:Java.util.Collection
  • 雙列集合: Java.util.Map

Collection單列集合類的根接口,它有兩個重要的子接口

  • java.util.List 特點:元素有序且可重復(fù),主要實現(xiàn)類,ArrayList,LinkedList
  • java.util.Set 特點:元素?zé)o序且不重復(fù),主要實現(xiàn)類,HashSet,TreeSet


Collection常用功能

主要方法:

  • public boolean add(E e) 把給定的對象元素e添加到當(dāng)前的集合中
  • public void clear() 清空集合中所有的元素
  • public boolean remove(E e);把給定的對象元素e從集合中刪除
  • public boolean contain(E e);判斷集合中是否包含給定的元素
  • public boolean isEmpty();判斷當(dāng)前集合是否為空
  • public int size();返回集合中元素的個數(shù)
  • public Object [ ] toArray();把集合中的元素返回為Object[ ]類型的數(shù)組
public class TestCollection {
    public static void main(String[] args) {
        //使用多態(tài)形式創(chuàng)建集合對象
        Collection<String> coll = new ArrayList<>();
        //add
        coll.add("小魯班");
        coll.add("后羿");
        coll.add("安琪拉");
        System.out.println(coll);
        // contain(E e);判斷集合中是否包含給定的元素
        System.out.println(coll.contains("小魯班")); //true
        System.out.println(coll.contains("王昭君"));//false
        // remove(E e);把給定的對象元素e從集合中刪除
        System.out.println(coll.remove("小魯班")); //true
        System.out.println("刪除之后" + coll);
        // isEmpty();判斷當(dāng)前集合是否為空
        System.out.println(coll.isEmpty()); //false
        // size();返回集合中元素的個數(shù)
        System.out.println(coll.size()); //2
//        Object [ ] toArray();把集合中的元素返回為Object[ ]類型的數(shù)組
        Object[] objects = coll.toArray();
        for (int i = 0; i < objects.length; i++) {
            System.out.print(objects[i] + "\t");
        }
        System.out.println();
        //clear() 清空集合中所有的元素
        coll.clear();
        System.out.println(coll);


    }
}  

Iterater接口

在程序開發(fā)中我們經(jīng)常要遍歷集合中所有的元素,針對這種需求JDK提供Java.util.iterater也是Java集合的一員,,它與CollectionMap不同,CollectionMap是存儲元素的,Iterater適用于迭代訪問(即遍歷)Collection中的元素,Iterater又被稱為迭代器。
Collection 中:

Iterator常用方法:

  • boolean hasNext();如果仍然有元素可以迭代,返回true
  • E next() 返回迭代


增強for循環(huán)

增強for循環(huán)(也稱foreach循環(huán))是jdk1.5之后出現(xiàn)的高級for循環(huán),專門用來遍歷數(shù)組和集合的,內(nèi)部原理實際上是一個迭代器,不能對集合中的元素進行增刪操作
格式:

for (元素的數(shù)據(jù)類型 變量  :集合或數(shù)組){
         //操作代碼
}

簡單記憶格式:

for( 當(dāng)前取出的值   :   我要迭代誰){
     //當(dāng)前取出的值
}

舉例

public class TestForEach {
    public static void main(String[] args) {
        int[] arr = {1, 4, 6, 7, 8};
        for (int a : arr) {
            System.out.println(a);
        }
        Collection<String> coll = new ArrayList<>();
        //add
        coll.add("小魯班");
        coll.add("后羿");
        coll.add("安琪拉");
        for (String hero : coll) {
            System.out.println("當(dāng)前英雄" + hero);
        }
        
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容