天天看點

date

Date類:日期類
        ·1 Data類有兩大功能,1把日期解釋為年月日時分秒
                                                2運作格式化和解析日期字元串
        ·2 Date類中的以上兩大功能分别被Calendar類和DateFormat類替換掉了
        ·3 在date類中出現了大量的以棄用的和過時的方法。
DataFormat類:Java.text包中
                        是一個抽象類
                        直接子類SimpleDateFormat
        作用:格式化日期并解析日期時間,通常用于日期和string字元串之間的轉換
                日期--》文本   文本--》日期
                借助Date對象
Calendar類  月曆類:實作日期和時間之間的轉換
                        是一個抽象類
                        作用擷取月曆上的時間字段值
                        Calendar的對象的建立通過靜态的getInstance()           

Date:

public static void main(String[] args) {
        //構造函數 Data()
        Date date = new Date();
        //擷取的目前系統時間
        System.out.println( date.toString());
        //Wed  Nov 04  15:35:43  CST 2020
        //星期 月份 日期 時:分:秒 cst 年份
        //構造函數 Date
        //1970年1月1日00:00:00 GMT起的指定毫秒數。--标準基準時間 曆元 
        Date date1 = new Date(45484784l);
        System.out.println(date1);
        //日期和毫秒值的轉換getTime()
        long time = date1.getTime();
        System.out.println("time = " + time);
    }           

運作結果:

Wed Nov 04 22:47:17 CST 2020
        Thu Jan 01 20:38:04 CST 1970
        time = 45484784           

DateFormat

public static void main(String[] args) {
        Date date = new Date();//日期對象
        //2020/11/4 16:01:22
        //使用DateFormat的子類SimpleDateFormat
        /*
         * G --》代表年代
         * y --》年
         * M -->月
         * m --》分鐘
         * d --》一月中的第幾天
         * D --》一年中的第幾天
         * h --》12小時值
         * H --》24小時值
         * s --》秒
         * S --》毫秒
         * E --》星期
         * F --》一月中的第幾個星期幾
         * W --》一月中的第幾個星期幾
         * w --》一年中第幾星期
         * z --》時區
         * */
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日-HH時mm分ss秒");//多态
        //把日期資料類型轉換成字元串資料類型  格式化format(Date date)
        String format = df.format(date);
        System.out.println("format = " + format);//format = 2020年11月04日-16時19分24秒

        //把字元串轉換成日期格式
        String time = " 2020年11月04日";//轉換成Date類型 cst
        //parse(String str);
        //指定日期格式,參照字元串的格式
        SimpleDateFormat time2 = new SimpleDateFormat("yyyy年MM月dd日");
        try {
            Date parse = time2.parse(time);
            System.out.println(parse);
            //Wed Nov 04 00:00:00 CST 2020
            //00:00:00是因為在字元串定義沒有給時分秒設定值
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
           
format = 2020年11月04日-23時05分31秒
    Wed Nov 04 00:00:00 CST 2020           
public static void main(String[] args) {
        //建構一個Calendar類月曆對象
        //通過靜态方法getInstance()
        Calendar instance = Calendar.getInstance();
        //年月日 時分秒 星期
        int i = instance.get(Calendar.YEAR);
        System.out.println("i = " + i);//2020   年
        int i1 = instance.get(Calendar.MARCH);
        System.out.println("i1 = " + i1);//10   月
        int i2 = instance.get(Calendar.DATE);
        System.out.println("i2 = " + i2);//4    日
        int i3 = instance.get(Calendar.HOUR);
        System.out.println("i3 = " + i3);//4    12時
        int i4 = instance.get(Calendar.MINUTE);
        System.out.println("i4 = " + i4);//58   分
        int i5 = instance.get(Calendar.SECOND);
        System.out.println("i5 = " + i5);//59   秒
        System.out.println(i + "/" + (i1 + 1) + "/" + i2 + "/" + " " + i3 + ":" + i4 + ":" + i5);

        //修改目前的時間為7後的值
        instance.add(Calendar.DATE,7);
        //後去目前時間
        int i6 = instance.get(Calendar.DATE);
        System.out.println("i6 = " + i6);
        //修改目前時間為5個小時後
        instance.add(Calendar.HOUR,5);
        //擷取目前的小時
        int i7 = instance.get(Calendar.HOUR);
        System.out.println("i7 = " + i7);
        //月曆對象轉換成日期對象 Date格式
        Date time = instance.getTime();
        System.out.println("time = " + time);

    }           
i = 2020
i1 = 10
i2 = 4
i3 = 11
i4 = 7
i5 = 53
2020/11/4/ 11:7:53
i6 = 11
i7 = 4
time = Thu Nov 12 04:07:53 CST 2020           
上一篇: date
下一篇: date