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 里面包含了一系列的静态方法,用于管理或操作数组(比如排序或者搜索)。
二、常用方法
-
Arrays.toString(); 返回数组的字符串形式;
-
Arrays.sort() ; 自然排序和定制排序排序返回值为 void;
可以通过重写 compare() 方法定制排序;
-
Arrays.binarySearch ();通过二分搜索法进行查找,要求必须排好序;
-
Arrays.copyOf( );数组复制;
-
Arrays.fill() ;数组填充;
-
Arrays.equals();比较两个数组内容是否完全一样;
-
Arrays.asList();将一组值转换成list;
System 类
- exit() 退出当前程序;
- System.arraycopy() :复制数组元素,比较适合底层调用;
一般使用 Arrays.copyOf 完成复制数; - System.currentTimeMills(); 返回当前时间距离 1970-1-1 的毫秒数;
BigInteger 和 BigDecimal 常见方法
1. BigInteger
- 当我们编程中,需要处理很大的整数,long 不够用 ,可以使用 BigInteger 的类来搞定;
- 在对 BigInteger 进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * /;
- 可以创建一个 要操作的 BigInteger 然后进行相应的操作;
- bigInteger1.add(bigInteger2);
- bigInteger1.subtract(bigInteger2);
- bigInteger1.multiply(bigInteger2);
- bigInteger1.divide(bigInteger2);
2. BigDecimal
- 当我们需要保存一个精度很高的数时,double 不够用;
- 在对 BigDecimal 进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * /;
- 可以创建一个 要操作的 BigDecimal 然后进行相应的操作;
- bigDecimal1.add(bigDecimal2);
- bigDecimal1.subtract(bigDecimal2);
- bigDecimal1.multiply(bigDecimal2);
- bigDecimal1.divide(bigDecimal2);
日期类
一、第一代日期类(Date)
SimpleDateFormate : 格式和解析日期的类;它允许进行格式化(日期 <====> 文本)
字母对应表:
字母 | 日期或时间元素 |
---|---|
G | Era 标识符(AD) |
y | 年 |
M | 年中的月份 |
w | 年中的周数 |
W | 月份中的周数 |
D | 年中的天数 |
d | 月份中的天数 |
F | 月份中的星期 |
E | 星期中的天数 |
a | AM/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)
- Calendar 是一个抽象类, 并且构造器是 private ;
- 可以通过 getInstance() 来获取实例;
- 提供大量的方法和字段提供给程序员;
- Calendar 没有提供对应的格式化的类,因此需要程序员自己组合来输出(灵活);
- 如果我们需要按照 24 小时进制来获取时间, Calendar.HOUR ==改成=> Calendar.HOUR_OF_DAY;
- 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));
三、第三代日期类
一、总结前两代的不足
-
可变性:
日期和时间这种类应该是不可变的; -
偏移性:
Date中的年份是从1990开始的,而月份是从0开始的;
-
格式化:格式化只对Date游泳,Calendar 不行;
-
他们都不是线程安全的,不能处理闰秒;(每隔2天多出1s);
二、三种格式比较
-
LocalDate()
只包含日期,可以获取日期字段;
-
LocalTime()
只包含时间,可以获取时间字段
-
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();