day-16-

今日内容:

  • Calendar类
  • System类

  • StringBuilder类

  • 包装类

  • Collection集合

  • 迭代器Iterator

  • 增强for循环

  • 泛型

Calendar类

	#### 		概念

​ java.util.Calendar 日历类,抽象类,在Date类后出现的,替换掉了很多Date类中的方法。该类将所有的可能用到的时间信息封装为静态成员变量,通过类名.静态成员变量获取时间字段值

  ##### 	获取方式

​ 由于Calendar类是一个抽象类,语言敏感性,Calendar类并不是直接创建对象来获取时间属性值,而是通过静态方法创建,返回子类对象。

静态方法如下:

  • ​ public static Calendar getInstance():使用默认时区和默认的语言环境获取一个日历对象

    例如:

    import java.util.Calendar;
    pubic class Demo01Calendar {
        public static void main(String[] args) {
            Calendar calendar = Calendar.getInstance();// 获取一个日历的对象     
        }
    }
    
常用的方法
  • ​ public int get(int field):获取给定的日历字段值
  • ​ public void set(int field,int value):将给定的字段设定为给定的值
  • ​ public abstract void add(int field,int amount):根据日历规则,将给定的日历字段添加或者减少指定的时间值
  • ​ public Date getTime():把日历对象转换成日期对象
  • ​ public long getTimeInMillis():获取日历对象对应的毫秒值

Calendar类中提供了很多个成员常量,代表给定的日历字段:

字段值 含义
YEAR
MONTH 月份
DATE 月中的某一天(几号)
DAY_OF_MONTH 月中的第几天
HOUR 时(12小时制)
HOUR_OF_DAY 时(24小时制)
MINUTE
SECOND
DAY_OF_WEEK 一周中的第几天(周几,周日为1)

备注:

​ 1. 在西方的星期,开始为周日,中国为周一

​ 2. 在Calendar类中,月份的表示是以0-11代表的是1-12月

          3. 日期是有大小关系,时间靠后,时间越大。

代码如下

package com.zhiyou100.calendar;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/*
    Calendar类的常用的成员方法:
         public int get(int field):返回的是一个给定的日历字段值
         public void set(int field,int value):将给定的日历字段设置为给定的值
         public abstract void add(int field,int amount):根据日历规则,为给定的日历字段添加或者是减去指定的时间量值
         public Date getTime(): 返回的是一个表示此Calendar时间值(从历元到现在的毫秒偏移量)的Date对象
        成员方法的参数:
                int field : 日历类的字段,可以通过Calendar类的静态成员变量获取
                public static final int YEAR = 1;    年
                public static final int MONTH= 2;    月
                public static final int DATE = 5;    月中的某一天
                public static final int DAY_OF_MONTH; 月中的第几天
                public static final int HOUR = 10;   时
                public static final int MINUTE = 12; 分
                public static final int SECOND = 13; 秒

  */
public class Demo01Calendar {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        demo04(calendar);

    }
    // 获取一个Calendar日历对象对应的Date日期对象
    public static void demo04(Calendar calendar) {
        // public Date getTime()  把日历对象转换成日期对象
        Date date = calendar.getTime();
        System.out.println(date);// Mon Nov 30 15:14:37 CST 2020
        long time = date.getTime();
        System.out.println("date对象对应的毫秒值:" + time);
        TimeZone timeZone = calendar.getTimeZone();
        System.out.println(timeZone);

        long timeInMillis = calendar.getTimeInMillis();
        System.out.println("calendar日历对象对应的毫秒值:"+timeInMillis);

        long timeMillis = System.currentTimeMillis();
        System.out.println("System类获取的毫秒值:" + timeMillis);

    }

    // 根据日历的规则,为给定的日历字段添加或者减去指定的时间量
    public static void demo03(Calendar calendar) {
        // public abstract void add(int field,int amount) 把指定的字段增加或者是减少指定的值
        /*
            参数:
                int field:指定的日历字段(YEAR,MONTH,DATE,....)
                int amount: 增加或者减少指定的数量
                     正数:增加指定的数量
                     负数:减少指定的数量
         */
        // 把当前的年份增加2年
        calendar.add(Calendar.YEAR, 2);
        // 把当前的月份减少3个月
        calendar.add(Calendar.MONTH, -3);

        // 获取更改之后的年份和月份
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int date = calendar.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");

    }

    // 将给定的日历字段设置为给定的值
    public static void demo02(Calendar calendar) {
        // public void set(int field,int value)
        /*
            1 3 5 7 8 10 12  31天
            4 6 9 11         30天
            2                29/28天
            int field:指定的日历字段(YEAR,MONTH,DATE....)
            int value:给指定的日历字段设置值
         */
        // 把当前的年份设置为2050年
        calendar.set(Calendar.YEAR, 2050);
        // 再次获取年份
        int year = calendar.get(Calendar.YEAR);
        System.out.println(year);
        // 把当前的月份设置为5月
        calendar.set(Calendar.MONTH, 4);// 0 -- 11
        //再次获取月份
        int month = calendar.get(Calendar.MONTH);
        System.out.println(month);
        // 设置日期为5日
        calendar.set(Calendar.DATE, 31);
        // 再次获取日期
        int date = calendar.get(Calendar.DATE);
        System.out.println(date);


    }

    // 获取给定的日历字段值
    public static void demo01(Calendar calendar) {
        // 使用public int get(int field) 获取给定的日历字段值
        // 获取年份
        int year = calendar.get(Calendar.YEAR);
        System.out.println(year);// 2020

        // 获取月份
        int month = calendar.get(Calendar.MONTH) + 1;
        System.out.println(month);// 10  西方的日历  月份:0--11  东方 1--12

        // 获取日期
        int date = calendar.get(Calendar.DATE);
        System.out.println(date);// 30

    }


}

System类

​ java.lang.System类中提供了大量的静态方法,主要是用来获取与系统相关的信息或者是进行系统级操作。

它不能被实例化。也不能被继承。

​ 常用的api方法:

  • ​ public static long currentTimeMills(): 获取当前系统时间对应的毫秒值

  • ​ public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length):将数组中指定的数据拷贝到另一个数组当中

  • ​ public static void exit(int status): 终止当前正在执行的java虚拟机 0代表正常退出,非0异常退出

currentTimeMills()方法

​ 实际上,它获取到的是距1970年1月1日 0时0分0秒到当前时间的毫秒差值

arraycopy()方法

​ 将一个数组当中的元素复制到另一个数据当中,数组的拷贝动作是一个系统级的操作,性能比较高。

arraycopy方法当中涉及到5个参数:

参数列表 参数名称 参数类型 参数含义
1 src Object 源数组
2 srcPos int 源数组索引的起始位置
3 dest Object 目标数组
4 destPos int 目标数组索引的起始位置
5 length int 复制元素的个数

代码如下

package com.zhiyou100.system;

import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BEncoderStream;

import java.util.Arrays;

// 定义一个系统练习类
/*
    java.lang.System 类中提供了大量的静态方法和变量,可以获取与系统相关的信息或者而系统级操作,System类不能被实例化
    通过查阅API文档,常用的方法有:
        public static long currentTimeMills(): 可以获取当前时间的毫秒值
        public static void arrarcopy(Object src,int srcPos,Object dest,int destPos,int length):将数组当中的数据拷贝到另一个数组当中
        public static void exit(int status):停止当前正在执行的虚拟机
 */
public class DemoSystem {

    public static void main(String[] args) {
        demo02();
        demo03();
        demo01();
    }

    // 停止正在执行的虚拟机
    public static void demo03(){
        // public static void exit(int status)
        for (int i = 1; i < 1000; i++) {
            if (i % 500 == 0) {
                System.out.println(i);
                System.exit(-100);
            }
        }
        System.out.println("正在往下执行了。。。。");
    }


    // 数组复制
    // public static void arrarcopy(Object src,int srcPos,Object dest,int destPos,int length):
    // java.lang.ArrayIndexOutOfBoundsException 可能出现数组索引越界异常
    public static void demo02(){
        /*
            参数:
                src: 源数组                                    source资源,来源
                srcPos: 源数组中的起始位置                       position  位置
                dest:目标数组                                  destination 目的地
                destPos:目标数据中的起始位置
                length: 要复制的数组元素的数量                    length  长度
            练习: 将源数组srcArr = {1,2,3,4,5,6,7}前三个元素 复制到目标数组的前三个位置上 destArr = {6,7,8,9,10,11}

         */
        // 定义源数组
        int[] srcArr = {1,2,3,4,5};
        // 定义目标数组
        int[] destArr = {6,7,8,9,10,11};
        // 复制前的目标数组的元素值为
        System.out.println("复制前:" + Arrays.toString(destArr));
        // 复制数据
        System.arraycopy(srcArr, 0, destArr, 0, 5);
        System.out.println("复制后:" + Arrays.toString(destArr));

    }

    public static void demo01() {
        // 测试JDK8的版本循环1万次耗费的时间  打印循环的次数
        // 先获取当前的时间毫秒值
        long begin = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            System.out.println(i);
        }
        // 程序执行后,再次获取当前的时间
        long end = System.currentTimeMillis();
        // 时间差 = 结束的时间 - 开始的时间
        System.out.println("循环10000次共耗时:" + (end - begin) + "ms");// 115毫秒
    }


}

包装类

package com.zhiyou100.wrapper;

/*
    装箱:把基本数据类型的数据包装到包装类中(基本数据类型---->包装类型)
       以Integer  int的包装类
          构造方法:
                Integer(int value):构造一个新分配的Integer对象,它表示的指定的int类型的值      123
                Integer(String s): 构造一个新分配的Integer对象,他表示的String参数类型指定的int值  "123"
          静态方法:
                static Integer valueOf(int i): 将指定的int类型值转换成Integer实例
                static Integer valueOf(String s):将String类型的int值转换成Integer对象
    拆箱:在包装类中取出基本数据类型的数据(包装类型------>基本数据类型)
          成员方法:
                int intValue():将指定的包装类型的对象转换成int类型的值

 */
public class Demo01Integer {

    public static void main(String[] args) {
        // 装箱:把基本数据类型的数据包装到包装类中(基本数据类型---->包装类型)
        // 构造方法
        Integer int01 = new Integer(123);
        System.out.println(int01);// 内存地址值   123  重写Object类中的toString方法
        System.out.println("=========================");

        Integer int02 = new Integer("123");
        System.out.println(int02.equals(int01));// true
        System.out.println("==============================");

        // 静态方法
        Integer int03 = Integer.valueOf(127);
        System.out.println(int03);// 123
        Integer int04 = Integer.valueOf("127");
        System.out.println( int03 == int04 );// true
        System.out.println("===============================");

        // 传递一个真的字符串 java.lang.NumberFormatException: For input string: "abc" 数字格式化异常
        //Integer int05 = Integer.valueOf("abc");
        //System.out.println(int05);
        System.out.println("-------------------------");

        // 拆箱:从包装类中取出基本数据类型的数据(包装类------>基本数据类型)
        Integer int06 = new Integer(123);
        int num = int06.intValue();
        System.out.println(num);// 123


    }


}

package com.zhiyou100.wrapper;

import java.util.ArrayList;
/*

    在JDK1.5之后出现了新特性:自动装箱、自动拆箱----> 基本数据类型和包装类型可以自由的相互转换

 */
public class Demo02Integer {

    public static void main(String[] args) {

        /*
            自动装箱:直接把int类型值赋值给包装类
            如:
                Integer int01 = 123; 相当于  Integer int01 = new Integer(123);
         */
        Integer int01 = 123;
        System.out.println(int01);// 123

        /*
            自动拆箱:int01是包装类,无法直接参与运算,可以自由转换成基本数据类型,在进行相关的运算
            int01 + 2 就相当于  int01.intValue() + 2
         */
        int num01 = int01 + 2;
        /*
            当我们把基本数据类型中存储到容器当中,一般不能直接存储  ArrayList集合
            容器当中一般存储的都是引用数据类型(对象)
            包装类型属于引用数据类型 JDK1.5之后包装类型和基本数据类型可以自由切换
         */
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(123);
        list.add(124);
        list.add(125);
        /*  for (int i = 0; i < Integer.MAX_VALUE; i++) {
            list.add(i);
        }*/
        System.out.println(list);

        // 取出125数值
        Integer int02 = list.get(2);// 自动装箱  Integer.valueOf(list.get(2))
        int num02 = list.get(2);// 自动拆箱      list.get(2).intValue();
        System.out.println(int02);

        // 对象操作
        boolean remove = list.remove(new Integer(125));
        boolean remove1 = list.remove(Integer.valueOf(125));
        Integer remove2 = list.remove(125);
        System.out.println(remove);


    }


}

package com.zhiyou100.wrapper;
/*
    基本数据类型和字符串之间的自由切换:
           基本数据---->字符串(String)
           1.   基本数据类型值 + "" ----> 最简单的方法(常用)
           2.   使用Integer类中的toString(参数): 返回的是表示指定整数的String对象+
                static String toString(int i)
           3.   String类中的静态方法valueOf(参数)
                static String valueOf(int i)  返回的是int参数的字符串表示形式
    字符串--->基本数据类型
            使用包装类中的静态方法parseXxx(字符串);
            Integer类:   static int parseInt(String s)
            Double类:    static double parseDouble(String s)


 */
public class Demo03Integer {

    public static void main(String[] args) {
        // 基本数据类型-----》字符串类型
        int num01 = 123;
        String str01 = num01 + "" ;
        System.out.println(str01.equals("123"));// true

        String str02 = Integer.toString(123);
        System.out.println(str01.equals(str02));// true

        String str03 = String.valueOf(123);
        System.out.println(str02 == str03);//  false
        System.out.println( str01 == str03);// false
        // == 什么时候会相等
        String str04 = "123";
        System.out.println(str01 == str04);// false
        String str05 =  ""+ 123;
        System.out.println(str01 == str05);// false

        String str = "123";
        System.out.println(str == str04);// true

        // 字符串---》基本数据类型
        int num02 = Integer.parseInt("123");
        System.out.println(num01 == num02);// true
        // java.lang.NumberFormatException 数字格式化异常
        int str06 = Integer.parseInt("abc");
        System.out.println(str06);


    }


}

StringBuilder类

package com.zhiyou100.StringBuilder;

// 定义字符串缓冲区练习类
/*
      构造方法:
            public StringBuilder(): 构造一个不带任何字符的字符串生成器,其初始容器为16个字符
            public StringBuilder(String str):构造一个字符串生成器,并初始化为指定的字符串内容

 */
public class StingBuilderDemo01 {

    public static void main(String[] args) {

        String str = new String("abc");
        StringBuilder stringBuilder = new StringBuilder("abc");
        System.out.println("使用带参的构造方法生成的字符串缓冲区:" + stringBuilder);//

        // 空参构造方法
        StringBuilder builder = new StringBuilder();
        System.out.println("空参构造的字符串缓冲区:" + builder);// ""
        String s1 = builder.toString();//

        StringBuilder builder2 = new StringBuilder();
        System.out.println("空参构造的字符串缓冲区:" + builder);// ""
        String s2 = builder2.toString();

        // equals()
        boolean result = builder.equals(builder2);
        System.out.println(result);
        boolean result2 = s1.equals(s2);
        System.out.println(result2);


    }




}

package com.zhiyou100.StringBuilder;
/*
    常用方法:
        public StringBuilder append(....): 添加任意类型数据的字符串形式,并返回当前对象本身
        返回的容量至少与给定的最小容量一样大。
        返回当前容量增加相同数量+ 2,如果那就足够了。
        不会返回大于{@code MAX_ARRAY_SIZE}的容量  Integer.MAX_ARRAY_SIZE-8
        除非给定的最小容量大于该容量。
        如果这个最大的容量还不够,内存溢出。 throw new OutOfMemoryError();
 */
public class StringBuilderDemo02 {

    public static void main(String[] args) {
        // 构建一个字符串缓冲区对象
        StringBuilder bu = new StringBuilder();
        // 使用append方法往字符串缓冲区添加数据
        // append方法返回的是this,相当于把bu的地址值赋给了bu2
       /* StringBuilder bu2 = bu.append("abc");
        System.out.println("bu的内容:"+bu);
        System.out.println("bu2的内容:"+ bu2);
        System.out.println(bu == bu2);// true  == 比较的是地址

        // 使用append方法再添加一些数据,无需接收返回值
        bu.append(1);
        bu.append(true);
        bu.append(3.14);
        bu.append('王');
        System.out.println(bu2);// bu2的内容值为: abc1true3.14王  等同于bu*/

        /*
            如果一个方法返回值是自己本身,那么我们可以进行链式调用,或者可以进行链式编程
            如果一个方法返回值是对象,可以继续调用方法。
         */
        int capacity = bu.append("abc").append(1).append(true).append(3.14).append("王1234").capacity();
        System.out.println(bu.length());
        System.out.println("此时bu的容量大小为:"+capacity);// 16 * 2 + 2 = 34
        System.out.println(bu);
        String str = "abc".toUpperCase().toLowerCase().toUpperCase().toLowerCase();
        System.out.println(str);
        System.out.println(str == "abc");// false
        System.out.println(3 << 1);// 3 * 2 * 2

    }


}

package com.zhiyou100.StringBuilder;
/*
    StringBuilder可以和String进行想换转换
        String--->StringBuilder:可以使用StringBuilder的带参构造方法
                StringBuilder(String str) 构造一个字符串生成器,并初始化为指定的字符串内容
        StringBuilder---->String:可以使用StringBuilder类中的toString()方法
                public String toString():将当前的StringBuilder对象转换成String对象

 */
public class StringBuilderDemo03 {

    public static void main(String[] args) {
        // String--->StringBuilder
        String str = "Hello World";
        System.out.println("str:" + str);
        StringBuilder bu = new StringBuilder(str);
        // 添加一些数据
        bu.append("---Java");
        System.out.println("bu:" + bu);

        // StringBuilder---->String
        String s = bu.toString();
        System.out.println("s:" + s);

        System.out.println(s.equals(bu));// false  Hello World---Java

    }



}

package com.zhiyou100.StringBuilder;

import jdk.management.resource.ResourceType;

/*
    StringBuilder类的常用方法:
          public StringBuilder insert(int offset,...) 根据指定的位置插入任意类型数据的字符串形式,返回的是当前对象
          不管存储什么类型的数据,进入字符串缓冲区都变成了字符值。

 */
public class Demo04StringBuilder {

    public static void main(String[] args) {
        // 构建StringBuilder对象
        StringBuilder bu = new StringBuilder();// ""  0
        // 调用insert方法
        System.out.println(bu.length());
        /*
             if ((offset < 0) || (offset > length()))
                 throw new StringIndexOutOfBoundsException(offset);
         */
        StringBuilder bu2 = bu.insert(0, "A");
        System.out.println(bu2);
        System.out.println(bu == bu2);
        System.out.println(bu2.length());
        bu.insert(1, "B");
        System.out.println("bu2:" + bu2);
        // 插入一个布尔类型值
        bu.insert(2, true);
        System.out.println(bu);
        System.out.println("bu字符串缓冲区的长度为:" + bu.length());
        System.out.println("bu字符串缓冲区的容量大小为:" + bu.capacity());
        Student student = new Student();
        student.setAge(10);
        student.setName("小王");
        bu.insert(6, student);
        System.out.println("=================================");
        System.out.println(bu.length());
        System.out.println(bu);
        System.out.println("=================================");
        System.out.println(bu.capacity());// miniCapacity.lenght()  Integer.MAX_VALUE < minCapacity
        // throw
    }


}
加一个 student 类
    package com.zhiyou100.StringBuilder;

public class Student {

    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

}

posted @ 2020-12-01 23:02  小迷糊学it  阅读(39)  评论(0编辑  收藏  举报