Java学习17

目录

一.System类:

1.System类常见方法

二.BigInteger和BigDecima类:

三.日期类:

1.Date:精确到毫秒,代表特定的瞬间。

2.Calendar:

(1)简介:

3.第三代日期类:

1.第三代日期类的常用方法:

2.使用DateTimeFormatter对象来进行格式化:

3.时间戳:


一.System类:

1.System类常见方法

System.exit(0);//0表示一个状态
        int [] src = {1,2,3};
        int [] dest = new int [3];//dest当前是{0,0,0}
        System.arraycopy(src,0,dest,0,3);//[1,2,3]//参数的含义:原数组、从原数组的哪个索引开始拷贝、目标数组、把原数组的的数据拷贝到目标数组的哪个索引、拷贝数据的数量
        System.arraycopy(src,0,dest,1,2);//[0,1,2]
        System.currentTimeMillis();//返回当前时间距离1970-1-1的毫秒数

二.BigInteger和BigDecima类:

BigInteger适合保存比较大的整形,BigDecimal适合保存精度较高的浮点型(小数)。

BigInteger bigInteger1 = new BigInteger("2333333333999999999999");
BigInteger bigInteger2 = new BigInteger("100000000000");
BigInteger add = bigInteger1.add(bigInteger2);
System.out.println(add);
BigInteger subtract = bigInteger1.subtract(bigInteger2);
System.out.println(subtract);
BigInteger multiply = bigInteger1.multiply(bigInteger2);
System.out.println(multiply);
BigInteger divide = bigInteger1.divide(bigInteger2);
System.out.println(divide);

BigDecimal bigDecimal1 = new BigDecimal("199.11111111111111199999998888");
BigDecimal bigDecimal2 = new BigDecimal("3");
System.out.println(bigDecimal1.divide(bigDecimal2));//除不尽,可能抛出异常
//在调用divide方法时,指定精度即可,BigDecimal.ROUND_CEILING
 System.out.println(bigDecimal1.divide(bigDecimal2,BigDecimal.ROUND_CEILING));

三.日期类:

1.Date:精确到毫秒,代表特定的瞬间。

Date d1 = new Date();
System.out.println("当前日期"+d1);//Mon May 27 16:48:23 CST 2024
Date d2 = new Date(9234567);//通过毫秒数得到时间
System.out.println(d2);
//将String->Date,使用的sdf的格式需要和你给的String的格式一样,否则会抛出转换异常
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
String format = sdf.format(d1);
System.out.println("当前日期"+format);
String s = "1996年01月01日 10:20:30 星期一";
Date parse = sdf.parse(s);
System.out.println(parse);//Mon Jan 01 10:20:30 CST 1996        

2.Calendar:

(1)简介:

Calendar类是一个抽象类,它为特定的瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR等日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法。

  • 如果需要按照24小时进制来获取时间,,Calendar.HOUR=>Calendar.HOUR_OF_DAY
  • Calendar返回类的时候,是按照0开始编号。

3.第三代日期类:

1.第三代日期类的常用方法:

//第三代日期:
//使用now()返回表示当前时间日期的对象
LocalDateTime ldt = LocalDateTime.now();
System.out.println("年="+ldt.getYear());//2024
System.out.println("月="+ldt.getMonth());//MATCH
System.out.println("日="+ldt.getMonthValue());///3
LocalDate now1 = LocalDate.now();//可以获取年月日
LocalTime now2 = LocalTime.now();//可以获取时分秒
2.使用DateTimeFormatter对象来进行格式化:
LocalDateTime ldt = LocalDateTime.now(); 
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH小时mm分钟ss秒");
 String format1 = dateTimeFormatter.format(ldt);
 System.out.println(format1);//格式化后的日期
3.时间戳:

//通过静态方法now()获取表示当前时间戳的对象
        Instant now = Instant.now();
        System.out.println(now);
        //通过from方法可以把Instant转成Date
        Date date = Date.from(now);
        System.out.println(date);
        //通过date的toInstant方法可以把date转成Instant对象
        Instant instant = date.toInstant();//通过静态方法now()获取表示当前时间戳的对象
        Instant now = Instant.now();
        System.out.println(now);
        //通过from方法可以把Instant转成Date
        Date date = Date.from(now);
        System.out.println(date);
        //通过date的toInstant方法可以把date转成Instant对象
        Instant instant = date.toInstant();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值