目录
23、System类
1、常用方法
-
exit 退出当前程序
public static void main(String[]args){ //exit 退出当前程序 System.out.println("ok1"); // exit(0)代表程序退出 // 0表示一个状态,正常退出的状态 System.exit(0); System.out.println("ok2"); }
-
arraycopy :复制数组元素,比较适合底层调用,一般我们使用Arrays.copyOf()完成赋值数组
public static void main(String[]args){ //arraycopy 复制数组元素 int[] arr = {1,2,3}; int[] arr2 = new int[3]; // 0 0 0 /** * @param src the source array. * 源数组(待复制的数组) * @param srcPos starting position in the source array. * 源数组那个开始索引拷贝 * @param dest the destination array. * 目标数组(把源数组的数据拷贝到那个数组) * @param destPos starting position in the destination data. * 把源数组的数据拷贝到目标数组的那个索引位置 * @param length the number of array elements to be copied. * 从源数组拷贝长度 * 如果length超出源数组的范围---会出现异常 */ System.arraycopy(arr,0,arr2,1,2); System.out.println(Arrays.toString(arr2)); }
-
currentTimeMillens :返回当前时间距离1970-1-1的毫秒数
-
gc : 提醒jdm运行垃圾回收机制
24、BigInteger 和 BigDecimal类
1、BigInteger适合保存比较大的整数
public static void main(String[]args){
/**
* 在编程中需要处理很大的整数 long的范围不够用的时候
* 我们使用BigInteger的类来搞定
*/
// long l = 999999999999999999999999l;
BigInteger bigInteger = new BigInteger("999999999999999999999999");
/**
* 在对BigInteger进行加减乘除的还是,需要使用对应的方法,不能直接使用 + - * /
* add加 subtract减 multiply乘 divide除
*/
bigInteger.add(new BigInteger("10"));
bigInteger.subtract(new BigInteger("10"));
bigInteger.multiply(new BigInteger("10"));
bigInteger.divide(new BigInteger("10"));
}
2、BigDecimal 适合保存精度更高的浮点型(小数)
跟BigInteger类似 注意的是除法 如果除不尽会抛出一个 ArithmeticException异常 解决方案是,在调用divide方法的时候 指定精度即可
public static void main(String[]args){
BigDecimal bigInteger = new BigDecimal("99.99999991111911");
System.out.println(bigInteger.add( new BigDecimal("1.1")));
System.out.println(bigInteger.subtract( new BigDecimal("1.1")));
System.out.println(bigInteger.multiply( new BigDecimal("1.1")));
//如过有无限循环小数,就会保留分子的精度
System.out.println(bigInteger.divide( new BigDecimal("1.1"),BigDecimal.ROUND_CEILING));
}
25、日期类
1、第一代日期类
-
Date:精确到毫秒,代表特定的瞬间
-
SimpleDateFormat: 格式和解析日期的类 (允许: 日期 -》文本 、 文本 -》日期 )
public static void main(String[] args) throws ParseException {
//1、获取当前系统时间
//2、引入的是java.util.Date
//3、默认输出的日期格式是国外方式,因此通常需要对格式进行转换
Date date = new Date();
System.out.println(date);
//通过毫秒数指定时间
Date date1 = new Date(9234567);
System.out.println(date1);
//创建SimpleDateFormat进行格式转换
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
String format = sf.format(date);
System.out.println(format);
//将字符串准换成时间 (字符串的格式要跟 (sf)转换的格式相同) 否则会抛出一个转换异常
//得到的Date对象输出的时候,还是按照国外的形式
//如果希望指定格式输出,需要转换
String str = "2000-1-1 10:23:31 星期六";
Date parse = sf.parse(str);
System.out.println(parse);
}
2、第二代日期类
-
第二代日期类,主要就是Calendar类(日历)
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar>
-
Calendar类是一个抽象类,它为特定瞬间与一组 如YEAR、MONTH、DAY_OF_MONTH、HOUR等日历字段之间提供了转换方法
public static void main(String[]args){
//1、Calendar类抽象类 ,并且构造器是私有化的
//2、可以通过getInstance()获取实例
//3、提供了大量的字段和方法供我们使用
//4、Calendar没有提供对应的格式化的类,需要程序员自己组合输出
//5.如果我们要按照24小时进制 我们使用 Calendar.HOUR ---> Calendar.HOUR_OF_DAY
Calendar c = Calendar.getInstance();
System.out.println(c);
//获取日历对象的相关字段
System.out.println("年:"+c.get(Calendar.YEAR));
//月要加1 因为Calendar默认是从0开始计算的(国外)
System.out.println("月:"+(c.get(Calendar.MONTH)+1));
System.out.println("日:"+c.get(Calendar.DAY_OF_MONTH));
System.out.println("时(12):"+c.get(Calendar.HOUR));
//24小时
System.out.println("时(24):"+c.get(Calendar.HOUR_OF_DAY));
System.out.println("分:"+c.get(Calendar.MINUTE));
System.out.println("秒:"+c.get(Calendar.SECOND));
}
3、前两代日期的不足分析
-
可变性:像日期和时间这样的类应该是不可变的
-
变异性:Date中的年份是从1970开始的,Calendar的月份是从0开始的
-
格式化:格式化只对Date有用 Calendar无法使用
-
此外,它们也不是线程安全的
4、第三代日期
-
LocalDate 日期 年月日
-
LocalTime 时间 时分秒
-
LocalDateTime 日期时间 年月日 时分秒 这三类是 jdk8加入
public static void main(String[]args){
//第三代日期
// private LocalDateTime(LocalDate date, LocalTime time) 构造器私有化了
//1. 使用now() 表示返回当前日期时间的对象
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
System.out.println("年:"+ldt.getYear());
//这个是英文
System.out.println("月:"+ldt.getMonth());
//数字
System.out.println("月:"+ldt.getMonthValue());
System.out.println("日:"+ldt.getDayOfMonth());
System.out.println("时:"+ldt.getHour());
System.out.println("分:"+ldt.getMinute());
System.out.println("秒:"+ldt.getSecond());
//使用DateTimeFormatter 进行格式化 上面三个类都行
//格式跟SimpleDateFormat一样 也可以开api文档
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(dtf.format(ldt));
//Date和Instant 转换
//Instant时间戳
Instant now = Instant.now();
System.out.println(now);
//1、Instant -->Date
Date date = Date.from(now);
//2、 Date --> Instant
Instant instant = date.toInstant();
System.out.println("==========");
//提供了大量的 plus 和minus方法 可以对当前时间进行 加或者减
//看看890天后 是什么时候
LocalDateTime l = ldt.plusDays(890);
System.out.println(dtf.format(l));
//100小时前
LocalDateTime ld = ldt.minusHours(100);
System.out.println(dtf.format(ld));
}