1.System類常見方法和案例
- exit:退出當前程序
- arraycopy:復制數組元素,比較適合底層調用,一般使用 Arrays.copyOf 完成復制數組
- int[] src={1,2,3};int[]?dest = new int[3]; System.arraycopy(src, 0, dest, 0, 3);
- currentTimeMilens:返回當前時間距離1970-1-1 的毫秒數
- gc:運行垃圾回收機制 System.gc();
package com.logic.system_;import java.util.Arrays;public class System01 {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]//currentTimeMillis:返回當前時間距離1970-1-1 的毫秒數System.out.println(System.currentTimeMillis());}
}
2.BigInteger和BigDecimal介紹
- Biglnteger適合保存比較大的整型
- BigDecimal適合保存精度更高的浮點型(小數)
3.BigInteger和BigDecimal
package com.logic.bignum;import java.math.BigInteger;public class BigInteger_ {public static void main(String[] args) {//當我們編程中,需要處理很大的整數,long 不夠用//可以使用BigInteger的類來搞定//long l = 23788888899999999999999999999l;//System.out.println("l=" + l);BigInteger bigInteger = new BigInteger("23788888899999999999999999999");BigInteger bigInteger2 = new BigInteger("10099999999999999999999999999999999999999999999999999999999999999999999999999999999");System.out.println(bigInteger);//1. 在對 BigInteger 進行加減乘除的時候,需要使用對應的方法,不能直接進行 + - * ///2. 可以創建一個 要操作的 BigInteger 然后進行相應操作BigInteger add = bigInteger.add(bigInteger2);System.out.println(add);//BigInteger subtract = bigInteger.subtract(bigInteger2);System.out.println(subtract);//減BigInteger multiply = bigInteger.multiply(bigInteger2);System.out.println(multiply);//乘BigInteger divide = bigInteger.divide(bigInteger2);System.out.println(divide);//除}
}
package com.logic.bignum;public class BigDecimal {public static void main(String[] args) {//當我們需要保存一個精度很高的數時,double 不夠用//可以是 BigDecimal
// double d = 1999.11111111111999999999999977788d;
// System.out.println(d);java.math.BigDecimal bigDecimal = new java.math.BigDecimal("1999.11");java.math.BigDecimal bigDecimal2 = new java.math.BigDecimal("3");System.out.println(bigDecimal);//1. 如果對 BigDecimal進行運算,比如加減乘除,需要使用對應的方法//2. 創建一個需要操作的 BigDecimal 然后調用相應的方法即可System.out.println(bigDecimal.add(bigDecimal2));System.out.println(bigDecimal.subtract(bigDecimal2));System.out.println(bigDecimal.multiply(bigDecimal2));//System.out.println(bigDecimal.divide(bigDecimal2));//可能拋出異常ArithmeticException//在調用divide 方法時,指定精度即可. BigDecimal.ROUND_CEILING//如果有無限循環小數,就會保留 分子 的精度System.out.println(bigDecimal.divide(bigDecimal2, java.math.BigDecimal.ROUND_CEILING));}}
4.第一代日期類
- Date:精確到毫秒,代表特定的瞬間
- SimpleDateFormat:格式和解析日期的類?SimpleDateFormat 格式化和解析日期的具體類 它允許進行格式化(日期->文本)、解析(文本->日期)和規范化

package com.logic.date_;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class Date01 {public static void main(String[] args) {//1. 獲取當前系統時間//2. 這里的Date 類是在java.util包//3. 默認輸出的日期格式是國外的方式, 因此通常需要對格式進行轉換Date d1 = new Date(); //獲取當前系統時間System.out.println("當前日期=" + d1);Date d2 = new Date(9234567); //通過指定毫秒數得到時間System.out.println("d2=" + d2); //獲取某個時間對應的毫秒數//1. 創建 SimpleDateFormat對象,可以指定相應的格式//2. 這里的格式使用的字母是規定好,不能亂寫SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");String format = sdf.format(d1); // format:將日期轉換成指定格式的字符串System.out.println("當前日期=" + format);//1. 可以把一個格式化的String 轉成對應的 Date//2. 得到Date 仍然在輸出時,還是按照國外的形式,如果希望指定格式輸出,需要轉換//3. 在把String -> Date , 使用的 sdf 格式需要和你給的String的格式一樣,否則會拋出轉換異常String s = "1996年01月01日 10:20:30 星期一";Date parse = null;try {parse = sdf.parse(s);} catch (ParseException e) {throw new RuntimeException(e);}System.out.println("parse=" + sdf.format(parse));}
}
5.第二代日期類
- 第二代日期類,主要是Calender類(日歷)
- Calendar 類是一個抽象類,它為特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日歷字段之間的轉換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。