Java学习Day20(更新)--API

Math类

    public static void main(String[] args) {
    
    
        System.out.println(Math.PI);
        System.out.println("取绝对值"+Math.abs(-2));
        System.out.println("开方"+Math.sqrt(9));
        System.out.println("次方"+Math.pow(4,3));
        System.out.println("向上取整"+Math.ceil(9.1));
        System.out.println("向下取整"+Math.floor(9.9));
        System.out.println("四舍五入"+Math.round(0.6));
        System.out.println("0-1随机数"+Math.random());
    }

Random类

    public static void main(String[] args) {
    
    
        Random r= new Random();
        System.out.println("随机Boolean:::"+r.nextBoolean());
        System.out.println("随机int数值范围内取值:::"+r.nextInt());
        System.out.println("0-9:::"+r.nextInt(10));//不包含10
        System.out.println(r.nextInt(11)+10);//10-20随机

        /*
        向数组中装入b.length个byte随机数
         */
        byte[] b = new byte[10];
        r.nextBytes(b);
        System.out.println(Arrays.toString(b));
    }

System类

    public static void main(String[] args) {
    
    
        /*
            public final static PrintStream out = null;
            println()是PrintStream中的方法
         */
        System.out.println();
        /*System.exit(0);//关闭程序退出jvm
        System.out.println("后面的代码");*/
        System.out.println(System.currentTimeMillis());//从1970年1.1 0.0.0 开始到现在毫秒差


        //记录运行次循环的所需时间
        long s = System.currentTimeMillis();
        String s1="";
        for (int i = 0; i < 100000; i++) {
    
    
            s1+=i;
            System.out.println(s1);
        }
        System.out.println(System.currentTimeMillis()-s);

        //将指定数组复制到新建数组中
        int [] a = {
    
    1,2,3,4,5};
        int [] b = new int[10];
        System.arraycopy(a,0,b,0,a.length);
        System.out.println(Arrays.toString(b));
    }

Date类

    public static void main(String[] args) {
    
    
        Date date = new Date();//获取系统时间
        System.out.println(date);

        //date.setTime(1646246462564L);//设置时间
        Date date1 = new Date(1646246462564L);//毫秒转年月日
        System.out.println(date1);

        System.out.println(date.getYear()+1900);
        System.out.println(date.getMonth()+1);
        System.out.println(date.getDate());
        System.out.println(date.getDay());
        System.out.println(date.getHours());
        System.out.println(date.getMinutes());
        System.out.println(date.getTime());//==System.currentTimeMillis();
    }

Calendar类

    public static void main(String[] args) {
    
    
        Calendar rightNow = Calendar.getInstance();//获取系统时间
        System.out.println(rightNow);

        Calendar rightNow1 = new GregorianCalendar();//第二种获取系统时间
        rightNow1.set(2022,5,23,10,10,10);//自己设定时间
        System.out.println(rightNow1.getTime());
        System.out.println(rightNow1.get(Calendar.YEAR));
        System.out.println(rightNow1.get(Calendar.MONTH));
        System.out.println(rightNow1.get(Calendar.DATE));
        System.out.println(rightNow1.get(Calendar.DAY_OF_MONTH));
        System.out.println(rightNow1.get(Calendar.WEEK_OF_YEAR));
        System.out.println(rightNow1.get(Calendar.DAY_OF_MONTH));
    }

SimpleDateFormat类

public static void main(String[] args) {
    
    
        /*
        1.日期转为指定格式的字符串
         */
        Date date = new Date();
        System.out.println(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss  E", Locale.CHINA);//设置格式和地区
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss  E", Locale.JAPAN);
        String s = sdf.format(date);
        String s1 = sdf1.format(date);
        System.out.println(s);
        System.out.println(s1);
        /*
        1.字符串转日期
         */
        String str = "2020-11-17 12.23.45";
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");//此处格式必须要与字符串中的日期格式相同
        try {
    
    
            Date d1=d.parse(str);
            System.out.println(d1);
        } catch (ParseException e) {
    
    
            e.printStackTrace();
        }
    }

猜你喜欢

转载自blog.csdn.net/XiaoFanMi/article/details/111403664
今日推荐