【185天】黑马程序员27天视频学习笔记【Day14-下】

叨逼叨两句

  1. 不容易,白天被叫去帮忙,不得已晚上来挑灯夜战,熬到2点,总算完成任务了。
  2. 我打算下周开始换一个更新时间,每次把deadline设置为晚上12点,都会接近或者超过这个时间,之后改成中午12点为警戒线,晚上6点为deadline,这样可以倒逼自己的身体去早点完成任务,不至于晚上熬夜影响第二天的工作学习。

14-(13-21):常用类

System

  1. public static void gc():提醒垃圾回收器执行finalize()方法回收垃圾。【finalize()方法不能由程序员手动调用,只能由垃圾回收器来调用】

        
        package com.test.regex;
    
    public class Demo14 {
        public static void main(String[] args) {
            for(int i = 0; i < 1000; i++){
                new Demo();
                System.gc();
            }
        }
    }
    
    class Demo {
        public void finalize(){
            System.out.println("垃圾被清扫了");
        }
    }
    
  2. public static void exit(int status)

    1. 将状态码设置为0,是正常终止当前运行的JVM
    2. 将状态码设置为非0,是异常终止当前运行的JVM
        package com.test.regex;
    
    public class Demo15 {
        public static void main(String[] args) {
            System.exit(0);
            System.out.println("111111");
        }
    }
    
  3. public static long currentTimeMillis()

    package com.test.regex;
    
    public class Demo15 {
        public static void main(String[] args) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < 1000 ; i++) {
                System.out.println(i);
            }
            long end = System.currentTimeMillis();
            System.out.println(end - start);
        }
    }
  4. public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

    package com.test.regex;
    
    public class Demo15 {
        public static void main(String[] args) {
            int[] src = {11,22,33};
            int[] dest = new int[8];
            for (int i = 0; i < dest.length; i++) {
                System.out.println(dest[i]);
            }
            
            System.arraycopy(src, 0, dest, 0, src.length);
            System.out.println("--------------");
            
            for (int i = 0; i < dest.length; i++) {
                System.out.println(dest[i]);
            }
            
        }
    }
    
    

BigInteger

不可变的任意精度的整数

  1. public BigInteger add(BigInteger val):加
  2. public BigInteger subtract(BigInteger val):减
  3. public BigInteger multiply(BigInteger val):乘
  4. public BigInteger divide(BigInteger val):除
  5. public BigInteger[] divideAndRemainder(BigInteger val)
package com.test.regex;

import java.math.BigInteger;

public class Demo15 {
    public static void main(String[] args) {
        BigInteger bi1 = new BigInteger("100");
        BigInteger bi2 = new BigInteger("2");
        
        System.out.println(bi1.add(bi2));        //+
        System.out.println(bi1.subtract(bi2)); //-
        System.out.println(bi1.multiply(bi2)); //*
        System.out.println(bi1.divide(bi2));       ///(除)
        
        BigInteger[] arr = bi1.divideAndRemainder(bi2);
        
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        
    }
}

BigDecimal

  1. float和double类型运算时非常容易丢失精度

    package com.test.regex;
    
    public class Demo15 {
        public static void main(String[] args) {
            System.out.println(2.0-1.1); 
        }
    }
    
  2. BigDecimal是不可变的、任意精度的有符号十进制数。
  3. 加减乘除和BigInteger相同

    package com.test.regex;
    
    import java.math.BigDecimal;
    
    public class Demo15 {
        public static void main(String[] args) {
    
        BigDecimal bd1 = new BigDecimal(2.0);
        BigDecimal bd2 = new BigDecimal(1.1);
        System.out.println(bd1.subtract(bd2)); //开发不推荐,依然不精确
        
        BigDecimal bd3 = new BigDecimal("2.0");
        BigDecimal bd4 = new BigDecimal("1.1");
        System.out.println(bd3.subtract(bd4)); //开发推荐
        
        BigDecimal bd5 = BigDecimal.valueOf(2.0);
        BigDecimal bd6 = BigDecimal.valueOf(1.1);
        System.out.println(bd5.subtract(bd6)); //开发推荐
        
        }
    }
    

Date

  1. Date():空参构造表示当前时间。
  2. Date(long date):有参构造表示特定瞬间,精确到毫秒。
package com.test.regex;

import java.util.Date;

public class Demo15 {
    public static void main(String[] args) {
        
        Date d1 = new Date();
        System.out.println(d1);
        
        Date d2 = new Date(0);
        System.out.println(d2);
    
    }
}
  1. public long getTime():返回毫秒数

    package com.test.regex;
    
    import java.util.Date;
    
    public class Demo15 {
        public static void main(String[] args) {
            Date d1 = new Date();
            System.out.println(d1.getTime()); //用Date对象来获取当前时间的毫秒值
            System.out.println(System.currentTimeMillis());//用System对象来获取当前时间的毫秒值
        }
    }

  1. public void setTime(long time):
package com.test.regex;

import java.util.Date;

public class Demo15 {
    public static void main(String[] args) {
        Date d1 = new Date();
        d1.setTime(1000);
        System.out.println(d1);
    }
}

SimpleDateFormat

  1. DateFormat是SimpleDateFormat的父类
package com.test.regex;

import java.text.DateFormat;

public class Demo15 {
    public static void main(String[] args) {
        //DateFormat df = new DateFormat(); //DateFormat是抽象类,无法创建实例
        DateFormat df1 = DateFormat.getDateInstance(); //相当于父类引用指向子类对象,右边的方法返回一个子类对象
    }
}
  1. 将日期对象转换为时间字符串
package com.test.regex;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo15 {
    public static void main(String[] args) {
        Date d = new Date(); //获取当前时间对象
        SimpleDateFormat sdf = new SimpleDateFormat(); //创建日期格式化类对象
        System.out.println(sdf.format(d));  //17-8-10 上午1:02
        
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(sdf2.format(d));//2017/08/10 01:04:53

        
    }
}
  1. 将时间字符串转换为日期对象
package com.test.regex;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo15 {
    public static void main(String[] args) throws ParseException {
        String str = "2000年08月08日08:08:08";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
        Date d = sdf.parse(str);
        System.out.println(d);
    }
}

练习

package com.test.regex;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo15 {
    public static void main(String[] args) throws ParseException {
        String birthday = "1990年5月21日";
        String today = "2090年6月6日";
        SimpleDateFormat sdf = www.rcsx.org new SimpleDateFormat("yyyy年MM月dd日");
        Date d1 = sdf.parse(birthday);
        Date d2 = sdf.parse(today);
        long time = d2.getTime() - d1.getTime();
        System.out.println(time/1000/60/60/24/365);

    }
}

Calendar

  1. public static Calendar getInstance()
  2. public int get(int field)
package com.test.regex;

import java.util.Calendar;

public class Demo15 {
    public static void main(String[] args)  {
        Calendar c = Calendar.getInstance();
        System.out.println(c.get(Calendar.YEAR));  // 返回年
        System.out.println(c.get(Calendar.MONTH)); //返回月,0代表1月
        System.out.println(c.get(Calendar.DAY_OF_MONTH)); // 月中的第几天
        System.out.println(c.get(Calendar.DAY_OF_WEEK)); // 一周的第几天,周日是1
        System.out.println(c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DAY_OF_MONTH)+
                "日"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
        System.out.println(c.get(Calendar.YEAR)+"年"+getNum(c.get(Calendar.MONTH)+1)+"月"+getNum(c.get(Calendar.DAY_OF_MONTH)
                )+"日"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
        
    }
    
    public static String getWeek(int week){
        String[] arr = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        return arr[week];
    }
    
    public static String getNum(int num){
        if(num > 9){
             return "" + num;
        } else {
            return "0" + num;
        }
    }
    
}
  1. public void add(int field,int amount)
  2. public final void set(int year,int month,int date)
package com.test.regex;

import java.util.Calendar;

public class Demo15 {
    public static void main(String[] args)  {
        Calendar c = Calendar.getInstance();
        //c.add(Calendar.YEAR, -1); //对指定字段进行操作,加或者减
        //c.set(Calendar.YEAR,2000); //对指定字段进行设置
        c.set(2000,8,8); //年、月、日
        
        System.out.println(c.get(Calendar.YEAR)+"年"+getNum(c.get(Calendar.MONTH)+1)+"月"+getNum(c.get(Calendar.DAY_OF_MONTH)
                )+"日"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
    }
    
    public static String getWeek(int week){
        String[] arr = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        return arr[week];
    }
    
    public static String getNum(int num){
        if(num > 9){
             return "" + num;
        } else {
            return "0" + num;
        }
    }
    
}

练习:键盘录入任意一个年份,判断是闰年还是平年

 
package com.test.regex;

import java.util.Calendar;
import java.util.Scanner;

public class Demo16 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份,判断该年份是闰年还是平年:");
        //int year = sc.nextInt();
        String line = sc.nextLine();
        int year = Integer.parseInt(line);
        boolean b = getYear(year);
         System.out.println(b);
    }
    
    public static boolean getYear(int year) {
        Calendar c = Calendar.getInstance();
        c.set(year, 2,1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        return c.get(Calendar.DAY_OF_MONTH) == 29;
    }
}
posted @ 2017-08-10 10:15  天师符  阅读(309)  评论(0编辑  收藏  举报