常用API
Math类概述:执行基本数字运算的方法
Math中方法的条用方式:Math类中无构造方法,但内部的方法都是静态的,则可以通过 类名.进行调用
Math类的常用方法:
Math.abs(int a) 返回参数的绝对值
Math.ceil(double a) 返回大于或等于参数的最小double值,等于一个整数
Math.floor(double a) 返回小于或等于参数的最小都变了值,等于一个整数
Math.round(float a) 按照四舍五入返回最接近参数的int
Math.max(int a,int b) 返回两个int值中的较大值
Math.min(int a,int b) 返回两个int值中的较大值
Math.pow(double a,double b) 返回a的b次幂
Math.random() 返回值为double的正值,[0.0,1.0)
System类的常用方法:
System.exit(int staus) 终止当前运行的Java虚拟机,非零表示异常终止
System.currentTimeMillis() 返回当前时间(以毫秒为单位)
Object类的toString方法
作用:以良好的形式,更方便的展示对象中的属性值
Object类的equals方法
作用:用于对象之间的比较,返回true和false的结构
举例:s1.equals(s2);s1和s2是两个对象
重写equals方法的场景:不希望比较对象的地址值,想要结合对象属性进行比较的时候。
Object的常用方法:
Object.toString(对象) 返回参数中对象的字符串表现形式。
Object.toString(对象,默认字符串) 返回对象的字符串表现形式。如果对象为空,那么返回第二个参数。
Object.isNull(对象) 判断对象是否为空
Object.ninNull(对象) 判断对象是否不为空
BigDecimal的常用方法:(可以用来精确计算)
BigDecimal.add(另一个BigDecimal对象) 加法
BigDecimal.subtract(另一个BigDecimal对象) 减法
BigDecimal.multiply(另一个BigDecimal对象) 乘法
BigDecimal.divide(另一个BIgDecimal对象) 除法
BigDecimal.divide(另一个BigDecimal对象,精确几位,舍入模式) 除法
总结:BigDecimal是用来进行精确计算的
创建BigDecimal的对象,构造方法使用参数类型为字符串的。
四则运算中的除法,如果除不尽请使用divide的三个参数的方法。
包装类
基本包装类的作用:
将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
基本操作之一:用于基本数据类型与字符串之间的转换
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Integer类的概述:包装一个对象中原始类型int的值
Integer类的构造方法:
Integer(int value) 根据int值创建Integer对象
Integer(String s) 根据String值创建Integer对象
Integer.valueOf(int i) 根据表示指定的int值的Integer实例
Integer.valueOf(String s) 返回一个保存指定值的Integer对象String
自动装箱:把基本数据类型转换为对应的包装类类型
自动拆箱:把包装类类型转换为对应的基本数据类型
Integer i=100; //自动装箱
i += 200; //i=i+200; i+200自动拆箱; i=i+200;是自动装箱
int和String类型的相互转换
int转换为String转换方法:
1.直接在数字后面加一个空字符串
2.通过String类静态方法valueOf(int a);
String 转换为int转换方法”
1.先将字符串数字转成Integer,再调用valueOf()方法
2.通过Integer静态方法parseInt()进行转换
//方式一:
String s="100";
Integer i=Integer.valueOf(s);
//i.intvalue() 以int类型返回Integer的值
int x=i.intvalue();
//方式二:
int y=Integer.parseInt(s);
Arrays
Arrays的常用方法:
Arrays.toString(int[] a) 返回指定数组的内容的字符串表示形式
Arrays.sort(int[] a) 返回数字顺序排列指定的数组
Arrays.binarySearch(int[] a,int key) 利用二分查找返回指定元素的索引
Date类的常用方法:
Date.getTime() 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
Date.setTime(long time) 设置时间,给的是毫秒值
SimpleDateFormat类的构造方法:
SimpleDateFormat() 构造一个SimpleDateFormat,使用默认模式和日期格式
SimpleDateFormat(String pattern)
构造一个SimpleDateFormat使用给定的模式和默认的日期格式
SimpleDateFormat类的常用方法
格式化(从Date到String)
public final String format(Date date):将日期格式化成日期/时间字符串
解析(从String到Date)“1988-09-09”
public Date parse(String source):从给定字符串的开始解析文本以生成日期
LocalDateTime创建方法
LocalDateTime .now() 获取当前系统时间
LocalDateTime.of(年,月,日,时,分,秒) 使用指定年月日时分秒初始化一个LocalDateTime对象
LocalDateTime的获取方法
方法名 | 说明 |
---|---|
public int getYear() | 获取年 |
public int getMonthValue() | 获取月份(1-12) |
public int getDayOfMonth() | 获取月份中的第几天(1-31) |
public int getDayOfYear() | 获取一年中的第几天(1-366) |
public DayOfWeek getDayOfWeek() | 获取星期 |
public int getMinute() | 获取分钟 |
public int getHour() | 获取小时 |
LocalDateTime的转换方法
toLocalDate() 转换成为一个LocalDate对象
toLocalTime() 转换成为一个LocalTime对象
LocalDateTime格式化和解析
方法名 | 说明 |
---|---|
public String format (指定格式) | 把一个LocalDateTime格式化成为一个字符串 |
public LocalDateTime parse (准备解析的字符串, 解析格式) | 把一个日期字符串解析成为一个LocalDateTime对象 |
public static DateTimeFormatter ofPattern(String pattern) | 使用指定的日期模板获取一个日期格式化器DateTimeFormatter对象 |
LocalDateTime增加或者减少时间的方法
方法名 | 说明 |
---|---|
public LocalDateTime plusYears (long years) | 添加或者减去年 |
public LocalDateTime plusMonths(long months) | 添加或者减去月 |
public LocalDateTime plusDays(long days) | 添加或者减去日 |
public LocalDateTime plusHours(long hours) | 添加或者减去时 |
public LocalDateTime plusMinutes(long minutes) | 添加或者减去分 |
public LocalDateTime plusSeconds(long seconds) | 添加或者减去秒 |
public LocalDateTime plusWeeks(long weeks) | 添加或者减去周 |
LocalDateTime修改方法
方法名 | 说明 |
---|---|
public LocalDateTime withYear(int year) | 直接修改年 |
public LocalDateTime withMonth(int month) | 直接修改月 |
public LocalDateTime withDayOfMonth(int dayofmonth) | 直接修改日期(一个月中的第几天) |
public LocalDateTime withDayOfYear(int dayOfYear) | 直接修改日期(一年中的第几天) |
public LocalDateTime withHour(int hour) | 直接修改小时 |
public LocalDateTime withMinute(int minute) | 直接修改分钟 |
public LocalDateTime withSecond(int second) | 直接修改秒 |
Period方法
方法名 | 说明 |
---|---|
public static Period between(开始时间,结束时间) | 计算两个“时间"的间隔 |
public int getYears() | 获得这段时间的年数 |
public int getMonths() | 获得此期间的总月数 |
public int getDays() | 获得此期间的天数 |
public long toTotalMonths() | 获取此期间的总月数 |
Duration方法
方法名 | 说明 |
---|---|
public static Durationbetween(开始时间,结束时间) | 计算两个“时间"的间隔 |
public long toSeconds() | 获得此时间间隔的秒 |
public int toMillis() | 获得此时间间隔的毫秒 |
public int toNanos() | 获得此时间间隔的纳秒 |