数学工具类
a.构造私有
b.方法都是静态的
Math . 类名直接调用
static int abs(int a) -> 求参数的绝对值
static double ceil(double a) -> 向上取整
static double floor(double a) ->向下取整
static long round(double a) -> 四舍五入
static int max(int a, int b) ->求两个数之间的较大值
static int min(int a, int b) ->求两个数之间的较小值
public class Exercise {
public static void main(String[] args) {
//static int abs(int a) -> 求参数的绝对值
System.out.println("Math.abs(-98) = " + Math.abs(-98)); //98
//static double ceil(double a) -> 向上取整
System.out.println("Math.ceil(9.2) = " + Math.ceil(9.2)); //10.0
System.out.println("Math.ceil(-2.8) = " + Math.ceil(-2.8)); //-2.0
//static double floor(double a) ->向下取整
System.out.println("Math.floor(2.2) = " + Math.floor(2.2)); //2.0
System.out.println("Math.floor(-2.2) = " + Math.floor(-2.2)); //-3.0
//static long round(double a) -> 四舍五入
System.out.println("Math.round(-3.2) = " + Math.round(-3.2)); //-3
System.out.println("Math.round(3.2) = " + Math.round(3.2)); //3
System.out.println("Math.round(2.8) = " + Math.round(2.8)); //3
System.out.println("Math.round(-2.8) = " + Math.round(-2.8)); //-3
//static int max(int a, int b) ->求两个数之间的较大值
System.out.println("Math.max(2,3) = " + Math.max(2, 3)); //3
//static int min(int a, int b) ->求两个数之间的较小值
System.out.println("Math.min(2,3) = " + Math.min(2, 3)); //2
}
}
1.将来我们会遇到非常大的整数,大到比long的值还有大,我们将这类数据称之为"对象"
2.作用:BigInteger用于处理超大整数
3.构造方法:
BigInteger(String val)
4.方法:
BigInteger add(BigInteger val) 返回其值为 (this + val) 的 BigInteger
BigInteger subtract(BigInteger val) 返回其值为 (this - val) 的 BigInteger
BigInteger multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger
BigInteger divide(BigInteger val) 返回其值为 (this / val) 的 BigInteger
public class Exercise {
public static void main(String[] args) {
BigInteger b1 = new BigInteger("12121212121212121212121212121212");
BigInteger b2 = new BigInteger("12121212121212121212121212121212");
//BigInteger add(BigInteger val) 返回其值为 (this + val) 的 BigInteger
System.out.println("b1.add(b2) = " + b1.add(b2));
//BigInteger subtract(BigInteger val) 返回其值为 (this - val) 的 BigInteger
System.out.println("b1.subtract(b2) = " + b1.subtract(b2));
//BigInteger multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger
System.out.println("b1.multiply(b2) = " + b1.multiply(b2));
//BigInteger divide(BigInteger val) 返回其值为 (this / val) 的 BigInteger
System.out.println("b1.divide(b2) = " + b1.divide(b2));
}
}
1.描述:之前我们说过不要用float或者double直接参与运算,否则会出现精度损失的问题,尤其是涉及到钱,绝对不能用float或者double直接计算
2.作用:BigDecimal可以解决float和double直接参与运算而产生的精度损失问题
3.构造:
BigDecimal(String val)
1.常用方法:
BigDecimal add(BigDecimal val) 返回其值为 (this + val) 的 BigDecimal
BigDecimal subtract(BigDecimal val) 返回其值为 (this - val) 的 BigDecimal
BigDecimal multiply(BigDecimal val) 返回其值为 (this * val) 的 BigDecimal
BigDecimal divide(BigDecimal val) 返回其值为 (this / val) 的 BigDecimal
2.注意:
如果除不尽会报错
public class Exercise {
public static void main(String[] args) {
BigDecimal b1 = new BigDecimal("2.35");
BigDecimal b2 = new BigDecimal("3.21");
//BigDecimal add(BigDecimal val) 返回其值为 (this + val) 的 BigDecimal
System.out.println("b1.add(b2) = " + b1.add(b2));
//BigDecimal subtract(BigDecimal val) 返回其值为 (this - val) 的 BigDecimal
System.out.println("b1.subtract(b2) = " + b1.subtract(b2));
//BigDecimal multiply(BigDecimal val) 返回其值为 (this * val) 的 BigDecimal
System.out.println("b1.multiply(b2) = " + b1.multiply(b2));
//BigDecimal divide(BigDecimal val) 返回其值为 (this / val) 的 BigDecimal
//注意:如果除不尽会报错
// System.out.println("b1.divide(b2) = " + b1.divide(b2));
BigDecimal b3 = new BigDecimal("3.22");
BigDecimal b4 = new BigDecimal("2.28");
BigDecimal b5 = b3.divide(b4, 2, BigDecimal.ROUND_HALF_UP);
System.out.println("b5 = " + b5);
}
}
1.概述:表示特定的瞬间,精确到毫秒
2.常识:
a.1000毫秒 = 1秒
b.北京经纬度: 东经116 北纬39.56
c.气候:温带大陆性季风气候
d.时区:一个时区差一个小时
e.0度经线:本初子午线
f.时间原点:1970年1月1日0时0分0秒
g.我们所在时区为东八区,比时间原点差8个小时
构造:
Date()->获取当前系统时间
Date(long time)->获取指定时间,从时间原点开始算,传递毫秒值
long getTime()-> 获取当前时间对应的毫秒值
void setTime(long time) -> 设置时间,从时间原点开始算起
public class Exercise {
public static void main(String[] args) {
// Date()->获取当前系统时间
Date date = new Date();
System.out.println("date = " + date);
//Date(long time)->获取指定时间,从时间原点开始算,传递毫秒值
Date date1 = new Date(1000L);
System.out.println("date1 = " + date1);
//void setTime(long time) -> 设置时间,从时间原点开始算起
//long getTime()-> 获取当前时间对应的毫秒值
date.setTime(2000);
System.out.println("date.getTime() = " + date.getTime());
}
}
//输出如下:
date = Mon Sep 11 17:52:32 CST 2023
date1 = Thu Jan 01 08:00:01 CST 1970 //东八区
date.getTime() = 2000
1.概述:日历类,是一个抽象类
2.获取:
static Calendar getInstance()
3.月份问题
0 1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12
public class Demo03Calendar {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();//多态
System.out.println(calendar);
}
}
//输出如下:
java.util.GregorianCalendar[time=1694427161108,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=31,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2023,MONTH=8,WEEK_OF_YEAR=37,WEEK_OF_MONTH=3,DAY_OF_MONTH=11,DAY_OF_YEAR=254,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=6,HOUR_OF_DAY=18,MINUTE=12,SECOND=41,MILLISECOND=108,ZONE_OFFSET=28800000,DST_OFFSET=0]
字段值 | 含义 |
---|---|
YEAR | 年 |
MONTH | 月(从0开始 , 可以+1使用) |
DAY_OF_MONTH | 月中的天(几号) |
HOUR | 时(12小时制) |
HOUR_OF_DAY | 时(24小时制) |
MINUTE | 分 |
SECOND | 秒 |
DAY_OF_WEEK | 周中的天(周几 , 周日为1 ) |
常用方法:
int get(int field) ->返回给定日历字段的值
void set(int field, int value) :将给定的日历字段设置为指定的值
void add(int field, int amount) :根据日历的规则,为给定的日历字段添加或者减去指定的时间量
Date getTime():将Calendar转成Date对象
field:代表的是日历字段-> 年 月 日 星期等,都是静态的 通过Calendar. 来调用日历字段值
//今天是2023/9/11 int get(int field) ->返回给定日历字段的值
public class Exercise4 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
//采用int get(int field) ->返回给定日历字段的值
System.out.println("calendar.get(Calendar.YEAR) = " + calendar.get(Calendar.YEAR));
System.out.println("calendar.get(Calendar.MONTH) = " + calendar.get(Calendar.MONTH));
System.out.println("calendar.get(Calendar.DAY_OF_MONTH) = " + calendar.get(Calendar.DAY_OF_MONTH));
}
}
//输出
calendar.get(Calendar.YEAR) = 2023
calendar.get(Calendar.MONTH) = 8
calendar.get(Calendar.DAY_OF_MONTH) = 11
//void set(int field, int value) :将给定的日历字段设置为指定的值
public class Exercise5 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2020);
calendar.set(Calendar.MONTH,10);
calendar.set(Calendar.DAY_OF_MONTH,11);
// System.out.println(calendar);
System.out.println("calendar.get(Calendar.YEAR) = " + calendar.get(Calendar.YEAR));
System.out.println("calendar.get(Calendar.MONTH) = " + calendar.get(Calendar.MONTH));
System.out.println("calendar.get(Calendar.DAY_OF_MONTH) = " + calendar.get(Calendar.DAY_OF_MONTH));
}
}
//输出
calendar.get(Calendar.YEAR) = 2020
calendar.get(Calendar.MONTH) = 10
calendar.get(Calendar.DAY_OF_MONTH) = 11
//void add(int field, int amount) :根据日历的规则,为给定的日历字段添加或者减去指定的时间量
public class Exercise6 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR,1);
System.out.println("calendar.get(Calendar.YEAR) = " + calendar.get(Calendar.YEAR)); //2024
calendar.add(Calendar.YEAR,1);
System.out.println("calendar.get(Calendar.YEAR) = " + calendar.get(Calendar.YEAR)); //2025
calendar.add(Calendar.YEAR,-1);
System.out.println("calendar.get(Calendar.YEAR) = " + calendar.get(Calendar.YEAR)); //2024
//Date getTime():将Calendar转成Date对象
Date s = calendar.getTime();
System.out.println("s = " + s);
//s = Wed Sep 11 18:43:32 CST 2024
}
}
拓展方法:
void set(int year, int month, int date) -> 直接设置年月日
需求:键盘录入一个年份,判断这一年是闰年,还是平年 步骤: 1.创建Scanner对象,调用nextInt,录入一个年份 year 2.获取Calendar对象,设置年月日 set(year,2,1) -> 设置2月1日相当于咱们国内3月1日 3.对day减1,得到了2月的最后一天 4.判断是否为29号
public class Exercise7 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入一个年份:"); int year = sc.nextInt(); Calendar calendar = Calendar.getInstance(); calendar.set(year,2,1); //因为国外月份0月份是国内的1月 ,所以此处2月1日代表的国内3月1日 calendar.add(Calendar.DAY_OF_MONTH,-1); int day = calendar.get(Calendar.DAY_OF_MONTH); if(day==29){ System.out.println("是闰年"); }else{ System.out.println("是平年"); } sc.close(); } }
1.概述:日期格式化类
SimpleDateFormat extends DateFormat
2.构造:
SimpleDateFormat(String pattern)
pattern:传递的是我们指定的日期格式
字母不能变,连接符可以改变
比如: yyyy-MM-dd HH:mm:ss
时间字母表示 | 说明 |
---|---|
y | 年 |
M | 月 |
d | 日 |
H | 时 |
m | 分 |
s | 秒 |
String format(Date date) -> 将Date对象按照指定的格式转成String
Date parse(String source) -> 将符合日期格式的字符串转成Date
public class Exercise02 {
public static void main(String[] args) throws ParseException {
format(); //2023-09-11 18:57:05
parse(); //Mon Sep 11 18:56:32 CST 2023
}
private static void parse() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = "2023-9-11 18:56:32";
Date date = sdf.parse(s);
System.out.println(date);
}
private static void format() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String s = sdf.format(date);
System.out.println(s);
}
}
1.概述:LocalDate是一个不可变的日期时间对象,表示日期,通常被视为年月日
a.static LocalDate now() 从默认时区的系统时钟获取当前日期
b.static LocalDate of(int year, int month, int dayOfMonth) -> 从一年,一个月和一天获得一个 LocalDate的实例
public class Exercise06 {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println("now = " + now);
//now = 2023-09-11
LocalDate localDate = LocalDate.of(2020, 10, 11);
System.out.println("localDate = " + localDate);
//localDate = 2020-10-11
}
}
LocalDateTime:
1.概述:LocalDateTime是一个不可变的日期时间对象,代表日期时间,通常被视为年 - 月 - 日 - 时 - 分 - 秒
2.获取:
a.static LocalDateTime now() 从默认时区的系统时钟获取当前的日期时间
b.static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) 从年,月,日,小时,分钟和秒获得 LocalDateTime的实例
public class Exercise07 {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("dateTime = " + dateTime);
//dateTime = 2023-09-11T19:19:28.746068100
LocalDateTime dateTime1 = LocalDateTime.of(2020, 10, 11, 10, 10, 10);
System.out.println("dateTime1 = " + dateTime1);
//dateTime1 = 2020-10-11T10:10:10
}
}
int getYear()->获取年份
int getMonthValue()->获取月份
int getDayOfMonth()->获取月中的第几天
public class Exercise08 {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
int year = date.getYear();
System.out.println("year = " + year);
int month = date.getMonthValue();
System.out.println("month = " + month);
int day = date.getDayOfMonth();
System.out.println("day = " + day);
}
}
LocalDate withYear(int year):设置年份
LocalDate withMonth(int month):设置月份
LocalDate withDayOfMonth(int day):设置月中的天数
public class Exercise09 {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate localDate = date.withYear(2020);
LocalDate localDate1 = localDate.withMonth(12);
LocalDate localDate2 = localDate1.withDayOfMonth(30);
System.out.println("localDate2 = " + localDate2);
}
}
//localDate2 = 2020-12-30
public class Exercise09 {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate localDate = date.withYear(2020).withMonth(10).withDayOfMonth(10);
System.out.println("localDate = " + localDate);
}
}
设置日期字段的偏移量,方法名plus开头,向后偏移
设置日期字段的偏移量,方法名minus开头,向前偏移
public class Exercise10 {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate years = date.plusYears(1);
System.out.println("date.getYear() = " + years.getYear()); //2024
LocalDate years1 = date.plusYears(-1);
System.out.println("years1.getYear() = " + years1.getYear()); //2022
LocalDate years2 = date.minusYears(1);
System.out.println("years2.getYear() = " + years2.getYear()); //2022
LocalDate years3 = date.minusYears(-1);
System.out.println("years3.getYear() = " + years3.getYear()); //2024
}
}
方法:
static Period between(LocalDate d1,LocalDate d2):计算两个日期之间的差值
getYears()->获取相差的年
getMonths()->获取相差的月
getDays()->获取相差的天
public class Exercise03 {
public static void main(String[] args) {
LocalDate localDate1 = LocalDate.of(2020, 10, 8);
LocalDate localDate2 = LocalDate.of(2021, 9, 7);
Period period = Period.between(localDate1, localDate2);
System.out.println("period.getYears() = " + period.getYears());
System.out.println("period.getMonths() = " + period.getMonths());
System.out.println("period.getDays() = " + period.getDays());
}
}
//输出如下
period.getYears() = 0
period.getMonths() = 10
period.getDays() = 30
1.static Duration between(Temporal startInclusive, Temporal endExclusive) -> 计算时间差
2.Temporal是一个接口:
实现类: LocalDate , LocalDateTime
3.参数需要传递Temporal的实现类,需要传递LocalDateTime
因为Duration计算的是精确时间偏差,所以需要传递能操作精确时间的LocalDateTime
4.利用Duration获取相差的时分秒 -> to开头
toDays():获取相差天数
toHours():获取相差小时
toMinutes():获取相差分钟
toMillis():获取相差秒(毫秒)
public class Exercise04 {
public static void main(String[] args) {
LocalDateTime l1 = LocalDateTime.of(2020, 10, 11, 10, 10, 10);
LocalDateTime l2 = LocalDateTime.of(2021, 9, 10, 1, 20, 20);
Duration duration = Duration.between(l1, l2);
//toDays():获取相差天数
System.out.println("duration.toDays() = " + duration.toDays());
//toHours():获取相差小时
System.out.println("duration.toHours() = " + duration.toHours());
//toMinutes():获取相差分钟
System.out.println("duration.toMillis() = " + duration.toMinutes());
//toMillis():获取相差秒(毫秒)
System.out.println("duration.toMillis() = " + duration.toMillis());
}
}
注意:
如果想计算年月日的时间偏差用Period
如果想计算精确时间(时分秒)偏差用Duration
1.概述:日期格式化类
2.获取:
static DateTimeFormatter ofPattern(String pattern)
pattern:指定的格式
3.常用方法:
String format(TemporalAccessor temporal) ->将日期对象按照指定格式转成String
TemporalAccessor是一个接口,是Temporal接口的子接口
实现类有:LocalDate,LocalDateTime
TemporalAccessor parse(CharSequence text) -> 将符合格式的字符串日期转成日期对象
返回值:TemporalAccessor接口,实现类有:LocalDate LocalDateTime
static LocalDateTime from(TemporalAccessor temporal) -> 将 TemporalAccessor转成LocalDateTime
public class Demo13DateTimeFormatter {
public static void main(String[] args) {
//method01();
method02();
}
private static void method02() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
TemporalAccessor temporalAccessor = dtf.parse("2020-11-11 11:11:11");
System.out.println(temporalAccessor);
//将TemporalAccessor变成LocalDateTime
LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
System.out.println(localDateTime);
}
private static void method01() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now();
String time = dtf.format(localDateTime);
System.out.println("time = " + time);
}
}
1.概述:
System 类包含一些有用的类字段和方法。它不能被实例化
2.特点:
a.构造私有,不能new对象
b.方法都是静态的
3.使用:
类名直接调用~~~
方法 | 说明 |
---|---|
static void exit(status) | 终止当前正在运行 的Java虚拟机 |
staic void gc() | 运行垃圾回收器 |
static long currentTimeMillis() | 返回以毫秒为单位的当前时间 , 可以用来计算代码的效率 |
static void arraycopy(Object src ,int srcPos , Object dest , int destPos , int length) | 功能:完成数组复制 src:源数组 srcPos:从源数组的哪个索引开始复制 dest:目标数组 destPos:从目标数组的哪个索引开始粘贴 length:复制多少个 |
public class Exercise01 {
public static void main(String[] args) {
int[] arr = {1,2,3,45,68};
int[] arr2 = new int[10];
//数组复制
System.arraycopy(arr,0,arr2,0,arr.length);
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i]+" ");
}
}
}
1.概述:数组工具类
2.特点:
a.构造私有,不能new
b.方法为静态的
3.使用:
类名直接调用
方法 | 说明 |
---|---|
static int binarySearch(int[] a, int key) | 二分查找 |
static void sort(int[] a) | 升序排序 |
static String toString(int[] a) | 按照指定格式输出元素[元素1,元素2…] |
static int[] copyOf(int[] original, int newLength) | 数组扩容,返回新数组 |
public class Exercise {
public static void main(String[] args) {
int[] arr = {5,4,8,7,3,6,9};
//static void sort(int[] a)升序排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
int[] arr1 = {5,6,8,9,11,25};
//static int binarySearch(int[] a, int key)二分查找
int index = Arrays.binarySearch(arr1, 5);
System.out.println("index = " + index);
//static int[] copyOf(int[] original, int newLength)数组扩容,返回新数组
int[] copy = Arrays.copyOf(arr1, 10);
System.out.println(Arrays.toString(copy));
}
}