Syste类 及常用方法

Syste类 及常用方法本文介绍了 Java 中 BigInteger 和 BigDecimal 两大类的使用 包括构造方法和算术运算 并展示了日期 Date 的基本操作 如通过 getTime 获取毫秒值和通过 setTime 设置时间

大家好,欢迎来到IT知识分享网。

实例化 : 实例化对象, 创建对象 , 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 = L; // 2. 因为long类型可以表示的数据范围也是有限, 于是对于开发中 // 很大整数类型, 可以引用BigInteger类表示 // 3. 构造方法 BigInteger big1 = new BigInteger(""); 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());// - // 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());// 02 System.out.println(System.currentTimeMillis());// 22 // 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 对象

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/116454.html

(0)
上一篇 2025-11-28 20:10
下一篇 2025-11-28 20:20

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信