Math,Arrays,Date,Calendar,LocalDateTime 类

本文详细介绍了Java中的Math类,包括求绝对值、幂运算、取整和开方等基本数学操作。同时,讲解了Arrays类的数组管理方法,如排序、查找、复制和填充。接着,提到了BigInteger和BigDecimal类在处理大整数和高精度浮点数时的应用。在日期处理部分,从一代Date类到三代LocalDateTime类的演变,阐述了日期时间的格式化、获取和操作方法。此外,还提及了Instant类的时间戳处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Math 类:

一、基本介绍:

Math 类包含了用于执行基本数学运算的方法,如初等指数,对数,平方根和三角函数。

二、常见的方法

// 1.abs() 求绝对值
int abs = Math.abs(-9);

// 2.pow() 求幂
//2 的 4 次方
double pow = Math.pow(2, 4);

// 3.ceil() 向上取整,返回>=该参数的最小整数(转成 double);
double ceil = Math.ceil(3.9);

// 4.floor() 向下取整,返回<=该参数的最大整数(转成 double)
double floor = Math.floor(4.001);

// 5.round() 四舍五入 Math.floor(该参数+0.5)
long round = Math.round(5.51);
System.out.println(round);//6
// 6.sqrt() 求开方
double sqrt = Math.sqrt(9.0);

// 7.random() 求随机数
	// random 返回的是 0 <= x < 1 之间的一个随机小数
	// 返回 [a,b)之间的整数;
	// 公式就是 (int)(a + Math.random() * (b-a +1) )

// 8.max() , min() 返回最大值和最小值
int min = Math.min(1, 9);
int max = Math.max(45, 90);

Arrays 类

一、基本介绍

Arrays 里面包含了一系列的静态方法,用于管理或操作数组(比如排序或者搜索)。

二、常用方法

  1. Arrays.toString(); 返回数组的字符串形式;

  2. Arrays.sort() ; 自然排序和定制排序排序返回值为 void;

    可以通过重写 compare() 方法定制排序;

  3. Arrays.binarySearch ();通过二分搜索法进行查找,要求必须排好序;

  4. Arrays.copyOf( );数组复制;

  5. Arrays.fill() ;数组填充;

  6. Arrays.equals();比较两个数组内容是否完全一样;

  7. Arrays.asList();将一组值转换成list;

System 类

  1. exit() 退出当前程序;
  2. System.arraycopy() :复制数组元素,比较适合底层调用;
    一般使用 Arrays.copyOf 完成复制数;
  3. System.currentTimeMills(); 返回当前时间距离 1970-1-1 的毫秒数;

BigInteger 和 BigDecimal 常见方法

1. BigInteger

  1. 当我们编程中,需要处理很大的整数,long 不够用 ,可以使用 BigInteger 的类来搞定;
  2. 在对 BigInteger 进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * /;
  3. 可以创建一个 要操作的 BigInteger 然后进行相应的操作;
    1. bigInteger1.add(bigInteger2);
    2. bigInteger1.subtract(bigInteger2);
    3. bigInteger1.multiply(bigInteger2);
    4. bigInteger1.divide(bigInteger2);

2. BigDecimal

  1. 当我们需要保存一个精度很高的数时,double 不够用;
  2. 在对 BigDecimal 进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * /;
  3. 可以创建一个 要操作的 BigDecimal 然后进行相应的操作;
    1. bigDecimal1.add(bigDecimal2);
    2. bigDecimal1.subtract(bigDecimal2);
    3. bigDecimal1.multiply(bigDecimal2);
    4. bigDecimal1.divide(bigDecimal2);

日期类

一、第一代日期类(Date)

SimpleDateFormate : 格式和解析日期的类;它允许进行格式化(日期 <====> 文本)

字母对应表:

字母日期或时间元素
GEra 标识符(AD)
y
M年中的月份
w年中的周数
W月份中的周数
D年中的天数
d月份中的天数
F月份中的星期
E星期中的天数
aAM/PM 标记
H一天中的小时数(0~23)
k一天中的小时数(1~24)
K一天中的小时数(0~11)
h一天中的小时数(1~12)

使用案例:

// 获取当前日期
Date date = new Date();
System.out.println(date);
// 通过指定的毫秒数获得日期
Date date1 = new Date(999999);
System.out.println(date1);
// 创建 SimpleDateFormat 对象,指定格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy 年 MM 月 dd 日 hh:mm:ss");
String format = simpleDateFormat.format(date);
String format1 = simpleDateFormat.format(date1);
System.out.println(format);
System.out.println(format1);
// 将格式化的日期字符串转换成日期格式;
Date parse = simpleDateFormat.parse(format);
System.out.println(parse);

二、第二代日期类(Calendar)

  1. Calendar 是一个抽象类, 并且构造器是 private ;
  2. 可以通过 getInstance() 来获取实例;
  3. 提供大量的方法和字段提供给程序员;
  4. Calendar 没有提供对应的格式化的类,因此需要程序员自己组合来输出(灵活);
  5. 如果我们需要按照 24 小时进制来获取时间, Calendar.HOUR ==改成=> Calendar.HOUR_OF_DAY;
  6. Calender 没有专门的格式化方法,所以需要程序员自己来组合显示
// 获取实例
Calendar instance = Calendar.getInstance();
System.out.println("年:" + instance.get(Calendar.YEAR));
// Calendar 返回月时候,0开始编号
System.out.println( (instance.get(Calendar.MONTH) + 1));
System.out.println( instance.get(Calendar.DAY_OF_MONTH));
System.out.println(instance.get(Calendar.HOUR_OF_DAY));
System.out.println(instance.get(Calendar.MINUTE));
System.out.println(instance.get(Calendar.SECOND));

三、第三代日期类

一、总结前两代的不足

  1. 可变性:
    日期和时间这种类应该是不可变的;

  2. 偏移性:

    Date中的年份是从1990开始的,而月份是从0开始的;

  3. 格式化:格式化只对Date游泳,Calendar 不行;

  4. 他们都不是线程安全的,不能处理闰秒;(每隔2天多出1s);

二、三种格式比较

  1. LocalDate()

    只包含日期,可以获取日期字段;

  2. LocalTime()

    只包含时间,可以获取时间字段

  3. LocalDateTime()

    包含 日期 + 时间, 可以获取日期和时间字段;

三、常用方法

1.LocalDateTime():

// 获取当前时间
LocalDateTime now = LocalDateTime.now();
System.out.println(now);

// 创建DateTimeFormatter 对象,对日期进行格式化;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy 年 MM 月 dd 日 HH:mm:ss");
String format = dateTimeFormatter.format(now);
System.out.println(format);

// 使用方法获得日期时间的各个值;
System.out.println("年=" + now.getYear());
System.out.println("月=" + now.getMonth());
System.out.println("月=" + now.getMonthValue());
System.out.println("日=" + now.getDayOfMonth());
System.out.println("时=" + now.getHour());
System.out.println("分=" + now.getMinute());
System.out.println("秒="+ now.getSecond());
// 使用 plus 和 minus 
LocalDateTime localDateTime = now.plusDays(88);
LocalDateTime localDateTime1 = now.plusMonths(1);
LocalDateTime localDateTime2 = now.plusYears(15);
LocalDateTime localDateTime3 = now.minusDays(88);
LocalDateTime localDateTime4 = now.minusMonths(1);
LocalDateTime localDateTime5 = now.minusYears(15);
LocalDateTime localDateTime6 = now.plusMinutes(15);
LocalDateTime localDateTime7 = now.plusHours(15);
LocalDateTime localDateTime8 = now.plusSeconds(15);
LocalDateTime localDateTime9 = now.minusMinutes(15);
LocalDateTime localDateTime10 = now.minusHours(15);
LocalDateTime localDateTime11 = now.minusSeconds(15);

2.LocalDate():

// 获取当前日期
LocalDate now = LocalDate.now();

// 使用方法获得日期的各个值;
System.out.println("年=" + now.getYear());
System.out.println("月=" + now.getMonth());
System.out.println("月=" + now.getMonthValue());
System.out.println("日=" + now.getDayOfMonth());
// 使用 plus 和 minus 
LocalDate localDate = now.plusDays(88);
LocalDate localDate1 = now.plusMonths(1);
LocalDate localDate2 = now.plusYears(15);
LocalDate localDate3 = now.minusDays(88);
LocalDate localDate4 = now.minusMonths(1);
LocalDate localDate5 = now.minusYears(15);

3.LocalTime()

// 获取当前时间
LocalTime now1 = LocalTime.now();

// 使用方法获得时间的各个值;
System.out.println("时=" + now1.getHour());
System.out.println("分=" + now1.getMinute());
System.out.println("秒=" + now1.getSecond());
// 使用 plus 和 minus 
LocalTime localTime = now1.plusMinutes(15);
LocalTime localTime1 = now1.plusHours(15);
LocalTime localTime2 = now1.plusSeconds(15);
LocalTime localTime3 = now1.minusMinutes(15);
LocalTime localTime4 = now1.minusHours(15);
LocalTime localTime5 = now1.minusSeconds(15);

四、Instant 时间戳

// 获得当前时间戳
Instant instant = Instant.now();
System.out.println(instant);
// 通过Date.from() 可以把 Instant对象 转换成 Date对象
Date from = Date.from(instant);
// 通过 toInstant() 将 Date对象 转换成 Instant对象
Instant instant1 = from.toInstant();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值