Syste类 及常用方法

本文介绍了Java中BigInteger和BigDecimal两大类的使用,包括构造方法和算术运算,并展示了日期Date的基本操作,如通过getTime()获取毫秒值和通过setTime()设置时间。同时,提到了SimpleDateFormat和Calendar类在日期格式化和日历管理中的应用。
摘要由CSDN通过智能技术生成

实例化 : 实例化对象, 创建对象 , new 类型();

初始化 : 赋值

1、常用字段(静态常量):

System.in:标准输入流,默认关联到键盘上

举例 : Scanner sc = new Scanner(System.in);

System.out:标准输出流,默认关联到控制台上

举例 : System.out.println(数据);

System.err:标准错误输出流,默认关联到控制台上,用于打印错误信息,在eclipse中,使用该流打印的内容是红色

举例 : 如果代码中发生异常, 那么使用System.err将异常信息进行输出

  1. currentTimeMillis():返回当前时间毫秒值1970-01-01-0-0-0

  2. static void exit(int status): 退出JVM虚拟机,零表示正常终止


大型数据类型

Biginteger类及其常用方法
  1. BigInteger类常用构造方法
  1. BigInteger(String val): 将指定字符串转换成BigInteger对象

  2. BigInteger(String val,int radix): 根据指定的radix进制,将指定字符串转换成BigInteger对象

  1. BigInteger类常用方法
  1. BigInteger abs(): 返回BigInteger对象的绝对值,返回值结果BigInteger

  2. BigInteger negate(): 取该对象的相反数

  3. BigInteger add(BigInteger val): 加法

  4. BigInteger subtract(BigInteger val): 减法

  5. BigInteger multiply(BigInteger val): 乘法

  6. BigInteger divide(BigInteger val): 除法

 public static void main(String[] args) {
        // 1. 定义出一个超出long类型范围的整数
        // long lo = 1234567890987678978987L;
        // 2. 因为long类型可以表示的数据范围也是有限, 于是对于开发中
        // 很大整数类型, 可以引用BigInteger类表示
        // 3. 构造方法
        BigInteger big1 = new BigInteger("1234567890987678978987");
        System.out.println(big1);

        // 将指定进制的字符串数据, 转换成BigInteger类型
        BigInteger big2 = new BigInteger("12",8);
        System.out.println(big2);// 10

        // 4. BigInteger中常用方法:
        BigInteger big3 = new BigInteger("-1234");
        // 1) 获取到大整数对应绝对值
        System.out.println(big3.abs());// 1234

        // 2) negate(): 取该对象的相反数
        System.out.println(big1.negate());// -1234567890987678978987

        // 3) BigInteger add(BigInteger val): 加法
        //4)BigInteger subtract(BigInteger val): 减法
        //5)BigInteger multiply(BigInteger val): 乘法
        //6)BigInteger divide(BigInteger val): 除法

        BigInteger big4 = new BigInteger("200");
        BigInteger big5 = new BigInteger("100");
        System.out.println(big4.add(big5));// 300
        System.out.println(big4.subtract(big5));// 100
        System.out.println(big4.multiply(big5));// 20000
        System.out.println(big4.divide(big5));// 2
        // 100 / 200 = 0.5  但是毕竟都是整数类型操作, 除法没有小数点出现
        System.out.println(big5.divide(big4));
    }
BigDecial类及其常用方法
  1. 构造方法:
  1. BigDecimal(double val): 将double类型的数据转换成BigDecimal对象

  2. BigDecimal(String val): 将String类型的数据转换成BigDecimal对象

  1. BigDecimal类常用方法
  1. BigDecimal add(BigDecimal augend): 加法

  2. BigDecimal subtract(BigDecimal subtrahend): 减法

  3. BigDecimal multiply(BigDecimal multiplicand): 乘法

  4. BigDecimal divide(BigDecimal divisor): 除法

  5. BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)

​ 除法三个参数分别表示:除数、精确小数位、舍入模式

​ 常用舍入模式:

​ BigDecimal.ROUND_UP 向上取整

​ BigDecimal.ROUND_FLOOR 向下取整法

​ BigDecimal.ROUND_HALF_UP 四舍五入

public static void main(String[] args) {
        // 1. 创建出BigDecimal大浮点类型(精准浮点类型)对象
        BigDecimal bd = new BigDecimal(3.899);
        System.out.println(bd);

        BigDecimal bd1 = new BigDecimal("3.899");
        System.out.println(bd1);

        // 1)BigDecimal add(BigDecimal augend): 加法
        //2)BigDecimal subtract(BigDecimal subtrahend): 减法
        //3)BigDecimal multiply(BigDecimal multiplicand): 乘法
        //4)BigDecimal divide(BigDecimal divisor): 除法
        BigDecimal bd2 = new BigDecimal("2");
        System.out.println(bd1.add(bd2));// 5.899
        System.out.println(bd1.subtract(bd2));// 1.899
        System.out.println(bd1.multiply(bd2));// 7.798
        System.out.println(bd1.divide(bd2));// 1.9495

        // 5)BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
       /* 除法三个参数分别表示:除数、精确小数位、舍入模式
        常用舍入模式:
        BigDecimal.ROUND_UP  向上取整
        BigDecimal.ROUND_FLOOR 向下取整法
        BigDecimal.ROUND_HALF_UP 四舍五入*/
        System.out.println(bd1.divide(bd2,2,BigDecimal.ROUND_HALF_UP));
        System.out.println(bd1.divide(bd2,2,BigDecimal.ROUND_UP));
        System.out.println(bd1.divide(bd2,2,BigDecimal.ROUND_FLOOR));
    }

时间类

日期Date类型及其常用方法
Date类构造方法:
  1. public Date(): 分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)

  2. public Date(long date): 分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为"历元(epoch)",即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数

Date类常用方法
  1. long getTime():

返回自1970 年1月1日 00:00:00 GMT 以来此 Date 对象表示的毫秒数

  1. void setTime(long time)

​ 设置此Date对象,以表示 1970 年1月1日 00:00:00 GMT 以后 time 毫秒的对应时间点

代码:

public static void main(String[] args) {
        // 1. 创建出一个日期对象
        Date d = new Date();
        System.out.println(d);// Thu Mar 25 13:58:52 GMT+08:00 2021
        // 2021-03-25 13:58:52

        // 2. Date(long time) : time表示毫秒值, 设置一个日期类型
        // 具体时间 : 从1970年01-01,00:00:00 开始进行计算, time毫秒值对应的时间
        Date d1 = new Date(1000);
        System.out.println(d1);

        // 3. getTime() : 表示Date日期对象,对应毫秒值对多少
        System.out.println(d1.getTime());// 1000
        System.out.println(d.getTime());// 1616652427902
        System.out.println(System.currentTimeMillis());// 1616652427922

        // 4. setTime(long time) : 表示将参数time毫秒值所表示的时间,设置给一个Date日期类型
        // 相当于修改的概念
        d.setTime(10000);
        System.out.println(d);// Thu Jan 01 08:00:10 GMT+08:00 1970
    }
SimpleDateFormat类及其常用方法
SimpleDateFormat类常用构造方法
  1. SimpleDateFormat(): 用默认的模式和默认语言环境的日期格式创建对象

  2. SimpleDateFormat(String pattern)

​ 用给定的模式和默认语言环境的日期格式创建对象,

​ 一般pattern传递的是 “yyyy-MM-dd HH:mm:ss”

​ 例如:2021-03-02 16:48:22

SimpleDateFormat类常用方法
  1. final String format(Date date): 将一个 Date 格式化为日期/时间字符串

  2. Date parse(String source) throws ParseException:

从给定字符串解析文本,以生成一个日期

Calender类及其常用方法
Calendar类及其常用方法:
  1. static Calendar getInstance(): 使用默认时区和语言环境获得一个Calendar类对象

  2. void set(int field,int value)

​ 将给定的日历字段设置为给定值

例如 : Calender.YEAR 表示年, 在Calendar日历类的字段摘要中, 有对于时间划分的字段, 都是静态的, 类名直接调用即可

  1. void set(int year, int month, int date)

设置日历字段的年月日

  1. int get(int field): 返回给定日历字段的值

  2. abstract void add(int field,int amount)

​ 根据日历的规则,为给定的日历字段添加或减去指定的时间量。减去的amount写成负数。

  1. final Date getTime()

​ 返回一个表示此 Calendar 时间值(从历元至现在的毫秒偏移量)的 Date 对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值