Skip to content

十二、常用API

一、Math

方法名说明
public static int abs(int a)获取参数绝对值
public static double ceil(double a)向上取整
public static double floor(double a)向下取整
public static double round(float a)四舍五入
public static double max(int a,int b)获取两个int值中的较大值
public static double pow(double a,double b)返回a的b次幂的值
public static double random()返回值为double的随机值,范围[0.0,1.0)

二、System

方法名说明
public static void exit(int status)终止当前运行的Java虚拟机
public static long currentTimeMillis()返回当前系统的时间毫秒值形式
public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数)数组拷贝

三、Runtime

方法名说明
public static Runtime getRuntime()当前系统的运行环境对象
public void exit(int status)停止虚拟机
public int availableProcessors()获取CPU的线程数
public long maxMemory()JVM能从系统中获取总内存大小(单位byte)
public long totalMemory()JVM已经从系统中获取总内存大小(单位byte)
public long freeMemory()JVM剩余内存大小(单位byte)
public Process exec(String command)运行cmd命令

1、获取Runtime的对象

java
Runtime r=Runtime.getRuntime();

2、exit 停止虚拟机

java
Runtime.getRuntime().exit(0);

四、Object和Objects

  • Object类是Java中的顶级父类。所有的类都直接或间接的继承与Object类
  • Object类中的方法可以被所有子类访问

1、Object

(1)构造方法

方法名说明
public Object()空参构造

(2)成员方法

方法名说明
public String toString()返回对象的字符串表现形式
public boolean equals(Object obj)比较两个对象是否相等
protected Object clone()对象克隆

2、Objects

方法名说明
public static boolean isNull(Object obj)检查指定对象是否为 null。如果对象为 null,则返回 true;否则返回 false。
public static boolean nonNull(Object obj)检查指定对象是否不为 null。如果对象不为 null,则返回 true;否则返回 false。
public static boolean equals(Object a, Object b)比较两个对象是否相等。如果两个对象相等,则返回 true;否则返回 false。
public static int hashCode(Object o)返回对象的哈希码。如果对象为 null,则返回 0。
public static String toString(Object o)返回对象的字符串表示形式。如果对象为 null,则返回字符串 "null"。

五、BigInteger和BigDecimal

1、BigInteger

在Java中,整数有四种类型:byte,short,int,long

在底层占用字节个数:byte1个字节、short2个字节、int4个字节、long8个字节

(1)创建对象

方式一:获取随机大整数
java
Random random = new Random();
BigInteger bd = new BigInteger(4,r);  //范围[0~(2^4-1)]
方式二:获取指定大整数
java
BigInteger bd = new BigInteger("9999999999999999999999999999999999999");
方式三:获取指定进制的大整数
java
BigInteger bd = new BigInteger("123",8);      //十进制为83
方式四:静态方法获取BigInteger对象
  • 表示范围较小,超出long范围会报错
java
BigInteger bd = BigInteger.valueOf(3232323L)
总结
  1. 如果BigInteger表示的数字没有超出long的范围,可以用静态方法获取
  2. 如果BigInteger表示的超出long的范围,可以用构造方法获取
  3. 对象一旦创建,BigInteger内部记录的值不能发生改变
  4. 只要进行计算都会产生一个新的BigIngeter对象

(2)成员方法

方法名说明
public BigInteger add(BigInteger val)加法
public BigInteger subtract(BigInteger val)减法
public BigInteger multiply(BigInteger val)乘法
public BigInteger divide(BigInteger val)除法,获取商
public BigInteger[] divideAndRemainder(BigInteger val)除法,获取商和余数
public boolean equals(Object x)比较是否相同
public BigInteger pow(int exponent)次幂
public BigInteger max/min(BigInteger val)返回较大值/较小值
public int intValue(BigInteger val)转为int类型整数,超出范围数据有误

2、BigDecimal

  • 用于小数的精确计算
  • 用来表示很大的小数

(1)创建方法

1、方式一:传递数字(不推荐,可能精度不精确)

java
BigDecimal bd = new BigDecimal(0.01);

2、方式二:传递字符串

java
BigDecimal bd = new BigDecimal("0.01");

3、方式三:通过静态方法获取对象(不超过double范围)

java
BigDecimal bd = BigDecimal.valueOf(10)

(2)成员方法

方法名说明
public static BigDecimal valueof(double val)获取对象
public BigDecimal add(BigDecimal val)加法
public BigDecimal subtract(BigDecimal val)减法
public BigDecimal multiply(BigDecimal val)乘法
public BigDecimal divide(BigDecimal val)除法
public BigDecimal divide(BigDecimal val,精确几位,舍入模式)除法

六、时间类(jdk7)

1、Date(时间)

(1)创建时间对象

java
Date d = new Date();		//当前系统时间

(2)创建指定时间对象

java
Date d = new Date(0L);		//1970年过去0毫秒

(3)setTime修改时间

java
Date d = new Date(0L);	
d.setTime(1000L);			//1970年过去1秒

(4)获取当前时间毫秒

java
Date d = new Date(0L);	
d.setTime(1000L);
d.getTime();				//1000

2、SimpleDateFormat(格式化时间)

  • 格式化:把时间变成我们喜欢的格式
  • 解析:把字符串表示的时间变成Date对象

(1)构造方法

构造方法说明
public SimpleDateFormat()构造一个SimpleDateFormat,使用默认格式
public SimpleDateFormat(String pattern)构造一个SimpleDateFormat,使用指定的格式

(2)成员方法

成员方法说明
public final String format(Date date)格式化(日期对象->字符串)
public Date parse(String source)解析(字符串 -> 日期对象)

3、Calendar(日历)

(1)获取 Calendar 实例

java
Calendar calendar = Calendar.getInstance();

(2)设置日期和时间

java
calendar.set(Calendar.YEAR, 2024);
calendar.set(Calendar.MONTH, Calendar.APRIL);// 使用了 Calendar.APRIL 常量来表示四月
//月份是从 0 开始计数的,所以 Calendar.APRIL 的值为 3。
calendar.set(Calendar.DAY_OF_MONTH, 3);
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);

(3)获取日期和时间的各个部分

java
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH); // 月份从 0 开始,0 表示一月
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);

(4)执行日期和时间的计算

java
calendar.add(Calendar.DAY_OF_MONTH, 1); // 在当前日期上增加一天
calendar.add(Calendar.MONTH, -1); // 在当前日期上减少一个月

(5)获取日期是星期几

java
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // 返回值为 1 表示星期日,2 表示星期一,依此类推

(6)获取当前日期和时间的时间戳

java
long timestamp = calendar.getTimeInMillis();

(7)将 Calendar 转换为 Date 对象

java
Date date = calendar.getTime();

(8)将时间戳转换为 Calendar 对象

java
calendar.setTimeInMillis(timestamp);

七、时间类(jdk8新增)

1、Date(时间)

方法名说明
static Set<String> getAvailableZoneIds()获取Java中支持的所有时区
static ZoneId systemDefault()获取系统默认时区
static ZoneId of(String zoneId)获取一个指定时区

(1)ZoneId(时区)

  • 获取所有的时区名称
java
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
  • 获取当前系统的默认时区
java
ZoneId zoneId = ZoneId.systemDefault();
  • 获取指定的时区
java
ZoneId zoneId = ZoneId.of("Asia/Pontianak");

(2)Instant(时间戳)

方法名说明
static Instant now()获取当前时间的Instant对象(标准时间)
static Instant ofXxxx(long epochMilli)根据(秒/毫秒/纳秒)获取Instant对象
ZonedDateTime atZone(ZoneId zone)指定时区
boolean isXxx(Instant otherInstant)判断系列的方法
Instant minusXxx(long millisToSubtract)减少时间系统的方法
Instant plusXxx(long millisToSubtract)增加时间系统的方法
  • 获取当前时间的Instant对象(标准时间)
java
Instant now = Instant.now();		//现在的时间戳
  • 根据(秒/毫秒/纳秒)获取Instant对象
    • 1 秒 = 1000 毫秒 = 1,000,000 纳秒

    • 1 毫秒 = 1,000,000 纳秒

java
Instant now = Instant.ofEpochMilli(0L);			//过去一毫秒的时间的时间戳

Instant now = Instant.ofEpochSecond(1L);		//过去一秒的时间的时间戳

Instant now = Instant.ofEpochSecond(1L,1000000000L);		//参数:秒,纳秒
  • 指定时区
    java
    ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
  • 用于时间的判断
    • isBefore

      java
      Instant instant1 =Instant.ofEpochMilli(0L);
      Instant instant2 =Instant.ofEpochMilli(1000L);
      
      boolean result = instant1.isBefore(instant2);			//true
    • isAfter

      java
      Instant instant1 =Instant.ofEpochMilli(0L);
      Instant instant2 =Instant.ofEpochMilli(1000L);
      
      boolean result = instant1.isAfter(instant2);			//false

(3)ZoneDateTime(带时区的时间)

方法名说明
static ZonedDateTime now()获取当前时间的ZoneDateTime对象
static ZonedDateTime ofXxxx(...)获取指定时间的ZoneDateTime对象
ZonedDateTime withXxx(时间)修改时间系列的方法
ZonedDateTime minusXxx(时间)减少时间系列的方法
ZonedDateTime plusXxx(时间)增加时间系列的方法

2、SimpleDateFormat

方法名说明
static DateTimeFormatter ofPattern(格式)获取格式对象
String format(时间对象)按照指定方式格式化

3、Calendar

(1)LocalDate(包含年月日)

  • 获取当前时间的日历对象
    java
    LocalDate nowDate = LocalDate.now();
  • 获取当前时间的日历对象
    java
    LocalDate ldDate = LocalDate.of(2024,4,4);		//2024-4-4
  • 获取年
    java
    int year = ldDate.getYear();
  • 获取月
    java
    int month1 = ldDate.getMonth();			//英文月,例如:JANUARY
    int month2= ldDate.getMonthValue();		//数值月,例如 :1
  • 获取日
    java
    int day = ldDate.getDayOfMonth();
  • 获取一年的第几天
    java
    int dayOfYear = ldDate.getDayOfYear();
  • 获取星期
    java
    DayOfWeek dayofweek = ldDate.getDayOfWeek();
  • is开头表示判断
    java
    ldDate1.isBefore(ldDate2);
    ldDate1.isAfter(ldDate2);
  • with开头表示修改
    java
    LocalDate withLocalDate = ldDate.withYear(2000);
  • minus开头表示减少,只能减少年月日
    java
    LocalDate withLocalDate = ldDate.minusYear(1);
  • plus开头表示增加,只能减少年月日
    java
    LocalDate withLocalDate = ldDate.plusYear(1);

(2)LocalTime(包括时分秒纳秒)

同上

(3)LocalDateTime(包括年月日时分秒纳秒)

同上

4、Duration

用于计算两个”时间“间隔(秒,纳秒)

5、Period

用于计算两个”日期“间隔(年、月、日)

6、ChronoUnit

用于计算两个“日期”间隔