0
点赞
收藏
分享

微信扫一扫

学习韩顺平老师java基础笔记(自用)3.29

ZSACH 2022-03-30 阅读 76
java

学习内容:

例如:

  1. System类
  2. BigInteger 和 BigDecimal 类
  3. 日期类

学习产出:

1. System类

1.1 System 类常见方法和案例

1) exit退出当前程序
2) arraycopy :复制数组元素,比较适合底层调用,一般使用
Arrays.copyOf完成复制数组.
int[] src={1,2,3};
int[] dest = new int[3];
System.arraycopy(src, 0, dest, 0, 3);
3) currentTimeMillens:返回当前时间距离1970-1-1的毫秒数
4) gc:运行垃圾回收机制System.gc0;

案例:

public static void main(String[] args) {
//exit 退出当前程序
// System.out.println("ok1");
// //1. exit(0) 表示程序退出
// //2. 0 表示一个状态 , 正常的状态
// System.exit(0);//
// System.out.println("ok2");
//arraycopy :复制数组元素,比较适合底层调用,
// 一般使用 Arrays.copyOf 完成复制数组
int[] src={1,2,3};
int[] dest = new int[3];// dest 当前是 {0,0,0}
//1. 主要是搞清楚这五个参数的含义
//2. // 源数组
// * @param src the source array. // srcPos: 从源数组的哪个索引位置开始拷贝
// * @param srcPos starting position in the source array. // dest : 目标数组,即把源数组的数据拷贝到哪个数组
// * @param dest the destination array. // destPos: 把源数组的数据拷贝到 目标数组的哪个索引
// * @param destPos starting position in the destination data. // length: 从源数组拷贝多少个数据到目标数组
// * @param length the number of array elements to be copied. System.arraycopy(src, 0, dest, 0, src.length);
// int[] src={1,2,3};
System.out.println("dest=" + Arrays.toString(dest));//[1, 2, 3]
//currentTimeMillens:返回当前时间距离 1970-1-1 的毫秒数
System.out.println(System.currentTimeMillis());
}

2.BigInteger 和 BigDecimal 类

2.1 BigInteger 和 BigDecimal 介绍

2.2 BigInteger 和 BigDecimal 常见方法
BigDecimal代码实现:

public class BigDecimal_ {
    public static void main(String[] args) {
        //当我们需要保存一个精度很高的数时,double 不够用
        //可以是 Bigdecimal
        //double d = 1999.1111111111111111d;
        BigDecimal bigDecimal = new BigDecimal("1999.1111111119898989898989898989898989898989898");
        BigDecimal bigDecimal1 = new BigDecimal("1.212");
        System.out.println(bigDecimal);

        //1.如果对BigDecimal进行运算,比如加减乘除,需要使用对应的方法
        //2.创建一个需要操作的 BigDecimal,然后调用相应的方法即可
        System.out.println(bigDecimal.add(bigDecimal1));
        System.out.println(bigDecimal.subtract(bigDecimal1));
        System.out.println(bigDecimal.multiply(bigDecimal1));
        //System.out.println(bigDecimal.divide(bigDecimal1));//可能会抛出异常ArithmeticException
        //在调用divide方法时,指定精度即可 BigDecimal.ROUND_CEILING
        //如果有无限循环小数,就会保留分子的精度
        System.out.println(bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING));
    }
}

BigInteger代码实现:

public class BigInteger_ {
    public static void main(String[] args) {
        //当我们在编程中,需要处理很大的整数,long不够用
        //可以使用BigInteger的类来搞定
       //long l =2378888885555555555555l;
        BigInteger bigInteger = new BigInteger("2378888885555555555555");
        BigInteger bigInteger1 = new BigInteger("100");
        System.out.println(bigInteger);
        //1.在对 BigInteger进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * /
        //2.可以创建一个要操作的BigInteger 然后进行相应操作
        BigInteger add = bigInteger.add(bigInteger1);//加
        System.out.println(add);
        BigInteger subtract = bigInteger.subtract(bigInteger1);//减
        System.out.println(subtract);
        BigInteger multiply = bigInteger.multiply(bigInteger1);//乘
        System.out.println(multiply);
        BigInteger divide = bigInteger.divide(bigInteger1);//除
        System.out.println(divide);
    }
}

3. 日期类

3.1 第一代日期类

案例:

public class Date_ {
    public static void main(String[] args) throws ParseException {
        //1.//获取当前系统时间
        //2.这里的Date 类是在java.itil包下
        //3.默认输出的日期格式是国外的方式,因此通常需要对格式进行转换
        Date date = new Date();//获取当前系统时间
        System.out.println("当前日期="+date);
        Date date1 = new Date(9235456);//通过指定毫秒数得到时间
        System.out.println("date1="+date1);


        //1.创建SimpleDateFormat 对象,可以指定相应的格式
        //2.这里的格式使用的字母是规定好的,不能乱写
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
        String format = simpleDateFormat.format(date);
        System.out.println(format);

        //1.可以把一个格式化的String 转成对应的Date
        //2.得到Date 仍然在输出时,还是按照国外的形式,如果希望指定格式输出,需要转换
        //3.在把一个String->>Date 使用的是simpleDateFormat格式需要和给的String的格式一样,否则会抛出转换异常
         String s ="1996年01月01日 10:20:30 星期二";
         Date parse=simpleDateFormat.parse(s);
        System.out.println("parse="+parse);
    }
}

3.2 第二代日期类

案例:

public class Calendar_ {
    public static void main(String[] args) {
        //1.calendar时一个抽象类,并且构造器时private的
        //2.可以通过getInstance()来获取实例
        //3.提供大量的方法和字段提供给程序员
        //4.Calendar没有提供对应的格式化的类,因此需要程序员自己组合显示
        //5.如果我们需要按照 24小时进制来获取时间,Calendar.HOUR 改成HOUR_OF_DAY
        Calendar instance = Calendar.getInstance();
        System.out.println(instance);
        //获取日历对象的某个日历字段
        System.out.println("年"+instance.get(Calendar.YEAR));
        //这里为什么要+1 因为Calendar返回月的时候,是按照0开始编号
        System.out.println("年"+instance.get(Calendar.MONTH)+1);
    }
}

3.3 第三代日期类
➢前面两代日期类的不足分析

第三代日期类的方法
案例:

public class LocalDate_ {
    public static void main(String[] args) {
        //第三代日期
        //1.使用now()返回表示当前日期时间的对象
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        //2.使用DateTimeFormatter 类进行格式化
        //创建 DateTimeFormatter 对象
        DateTimeFormatter dtm = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss E");
        String format = dtm.format(now);
        System.out.println(format);
        System.out.println("年="+now.getYear());
        System.out.println("月="+now.getMonth());
        System.out.println("月="+now.getMonthValue());
        System.out.println("日="+now.getDayOfMonth());
        System.out.println("时="+now.getHour());
        //提供 plus 和minus方法可以对当前时间进行加或者减
        //看看898天后,是什么时候 把 年月日时分秒
        LocalDateTime localDateTime = now.plusDays(898);
        System.out.println("898天后="+dtm.format(localDateTime));
        //看看345分钟前是什么时候,把 年月日-时分秒输出
        LocalDateTime localDateTime1 = now.minusMinutes(345);
        System.out.println("345分钟前="+dtm.format(localDateTime1));
    }
}

3.4 DateTimeFormatter 格式日期类

3.5 Instant 时间戳

案例:

public class Instant_ {
    public static void main(String[] args) {
        //1.通过静态方法 now() 获取表示当前时间戳的对象
        Instant now = Instant.now();
        System.out.println(now);
        //2.通过from方法可以把Instant转成Date
        Date date = Date.from(now);
        //3.通过 date的toInstant(),可以把date转成Instant方法
        Instant instant = date.toInstant();
    }
}

3.6 第三代日期类更多方法

举报

相关推荐

0 条评论