Java基础第21天——实用类简介
一、System类
exit退出当前程序
arraycopy:复制数组元素,比较适合底层调用,一般使用
Arrays.copyOf完成复制数组. int[] src = {1, 2, 3}; int[] dest = new int[3]; 当前是{0,0,0} System.arraycopy(src,0,dest,0,3);
currentTimeMillens:返回当前时间距离 1970-1-1 的毫秒数
gc:运行垃圾回收机制 System.gc();
二、BigInteger和BigDecimal类
应用场景:
BigInteger适合保存比较大的整型
BigDecimal适合保存精度更高的浮点型(小数)
在对 BigInteger 进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * /
可以创建一个 要操作的 BigInteger 或BigDecimal然后进行相应操作
- add 加
- subtract 减
- multiply 乘
- divide 除
BigInteger bigInteger = new BigInteger("23777775465464564");
BigInteger bigInteger2 = new BigInteger("100");
System.out.println(bigInteger);
BigInteger add = bigInteger.add(bigInteger2); //加法
System.out.println(add);
三、日期类(Date、Calendar、LocalDate)
Date:精确到毫秒,代表特定的瞬间
SimpleDateFormat:格式和解析日期的类SimpleDateFormat 格式化和解析日期的具体类。它允许进行格式化(日期->文本)、解析(文本->日期)和规范化。
package date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Date01 { public static void main(String[] args) throws ParseException { //默认输出的日期格式为国外的方式,因此通常需要对格式进行转换 Date d1=new Date(); //获取当前系统时间 System.out.println("当前时间为:"+ d1); Date d2= new Date(99214567); //通过指定毫秒数得到时间 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); //可以把一个格式化的String 转成对应的 Date //得到Date 仍然在输出时,还是按照国外的形式,如果希望指定格式输出,需要转换 //在把String - > Date ,使用 sdf 格式需要和你给的String格式一样,否则会抛出转换异常 String s="1996年01月01日 10:20:30 星期一"; Date parse = sdf.parse(s); System.out.println("parse=" + parse); } }
第二代日期类
package date; import java.util.Calendar; public class Calendar_ { public static void main(String[] args) { //1. Calendar 是一个抽象类 ,并且构造器时private //2. 可以通过getInstance() 来获取实例 //2. 提供大量的方法和字段给程序员 Calendar c= Calendar.getInstance(); //创建日历类对象 比较简单自由 System.out.println(c); //获取日历对象的某个日历字段 System.out.println("年: "+ c.get(Calendar.YEAR)); System.out.println("月: "+ (c.get(Calendar.MONTH)+1)); System.out.println("日: "+c.get(Calendar.DAY_OF_MONTH)); System.out.println("小时:"+ c.get(Calendar.HOUR)); System.out.println("分钟:"+ c.get(Calendar.MINUTE)); System.out.println("秒: " + c.get(Calendar.SECOND)); //Calendar没有专门的格式化方法,所以需要程序员自己来组合显示 System.out.println(c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DAY_OF_MONTH)+"日"); } }
前两代日期类的不足分析
可变性:像日期和时间这样的类应该是不可变的。
偏移性:Date中的年份是从1900开始的,而月份都从0开始给。
格式化:格式化只对Date有用,Calendar则不行。
此外,它们也不是线程安全的;也不能处理闰秒等(每隔两天,多出1秒)。
第三代日期类
LocalDate(日期/年月日),LocalTime(时间/时分秒)、LocalDateTime(日期时间/年月日/时分秒) JDK8加入
package date; import java.time.LocalDateTime; public class LocalDate_ { public static void main(String[] args) { //第三代日期 //1.使用now() 返回表示当前日期时间的对象 LocalDateTime ldt = LocalDateTime.now(); System.out.println(ldt); System.out.println("年=" +ldt.getYear()); System.out.println("月=" +ldt.getMonthValue()); System.out.println("月=" +ldt.getMonth()); System.out.println("日=" +ldt.getDayOfMonth()); System.out.println("时=" +ldt.getHour()); System.out.println("分=" +ldt.getMinute()); System.out.println("秒=" +ldt.getSecond()); } }
DateTimeFormatter格式日期类
DateTimeFormat dtf = DateTimeFormatter.ofPattern(格式); String str = dtf.format(日期对象);
- 案例显示
LocalDateTime ldt = LocalDateTime.now(); //关于 DateTimeFormatter 的各个格式参数,需要看jdk8的文档。 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH小时mm分钟ss秒"); String strDate = dtf.format(ldt); //使用DateTimeFormatter 对象来进行格式化 //创建 DateTimeFormatter对象 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH小时mm分钟ss秒"); String format = dateTimeFormatter.format(ldt); System.out.println("格式化的日期: " + format);
Instant时间戳
//类似于Date //提供了一系列和Date类转换的方式 Instance----> Date: Date date = Date.from(instant); Date------> Instant: Instant instant = date.toInstant(); 案例演示: //1、静态方法 now() 获取表示当前时间戳的对象 Instant now = Instant.now(); System.out.println(now); //2、通过 from 可以把 Instant转成 Date Date date = Date.form(now); //3、通过 date的toInstant() 可以把 date 转成Instant对象 Instant instant = date.toInstant();
第三代日期类更多方法
- LocalDateTime类
- MonthDay类:检查重复事件
- 是否时闰年
- 增加日期的某个部分
- 使用plus方法测试增加时间的某个部分
- 使用minus方法测试查看一年前和一年后的日期
本文含有隐藏内容,请 开通VIP 后查看