常用API
# Math
Math 包含执行基本数字运算的方法。Math 类中无构造方法,但内部的方法都是静态的,则可以通过 类名。进行调用
方法名 | 说明 |
---|---|
public static int abs(int a) | 返回参数的绝对值 |
public static double ceil(double a) | 返回大于或等于参数的最小 double 值,等于一个整 数 |
public static double floor(double a) | 返回小于或等于参数的最大 double 值,等于一个整 数 |
public static int round(float a) | 按照四舍五入返回最接近参数的 int |
public static int max(int a,int b) | 返回两个 int 值中的较大值 |
public static int min(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() | 返回当前时间(以毫秒为单位) |
# 案例
需求:在控制台输出 1-10000 ,计算这段代码执行了多少毫秒
public class SystemDemo {
public static void main(String[] args) {
// 获 取 开 始 的 时 间 节 点
long start = System.currentTimeMillis();
for (int i = 1;i <= 10000; i++) {
System.out.println(i);
}
// 获取代 码 运 行 结 束 后 的 时 间 节 点
long end = System.currentTimeMillis();
System.out.println("共 耗 时 : " + (end start) + "毫秒"); }
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# Arrays
工具类的设计思路
构造方法用 private 修饰
成员用 public static 修饰
方法名 | 描述 |
---|---|
public static String toString(int[] a) 返 | 返回指定数组的内容的字符串表示形式 |
public static void sort(int[] a) | 按照数字顺序排列指定的数组 |
# 时间日期
#### Date 类
Date 代表了一个特定的时间,精确到毫秒
方法名 | 说明 |
---|---|
public Date() | 分配一个 Date 对象,并初始化,以便它代表它被分配的时间,精确到毫秒 |
public Date(long date) | 分配一个 Date 对象,并将其初始化为表示从标准基准时间起指定的毫秒数 |
# SimpleDateFormat 类
SimpleDateFormat 是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。
方法名 | 说明 |
---|---|
public SimpleDateFormat() | 构造一个 SimpleDateFormat ,使用默认模式和日期格式 |
public SimpleDateFormat(Stringpattern) | 构造一个 SimpleDateFormat 使用给定的模式和默认的日期格式 |
# Calendar 类
Calendar 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
Calendar 提供了一个类方法 getInstance 用于获取这种类型的一般有用的对象。该方法返回一个 Calendar 对象。
其日历字段已使用当前日期和时间初始化:Calendar rightNow = Calendar.getInstance();
方法名 | 说明 |
---|---|
public int get(int field) | 返回给定日历字段的值 |
public abstract void add(int field, int amount) | 根据日历的规则,将指定的时间量添加或减去给定的日 历字段 |
public final void set(int year,int month,int date) | 设置当前日历的年月日 |