可以兼職做設(shè)計(jì)的網(wǎng)站百度收錄入口
常用API
MATH
代表數(shù)學(xué),是一個(gè)工具類,里面提供的都是對(duì)數(shù)據(jù)進(jìn)行操作的一些靜態(tài)方法。
方法名 | 說明 |
---|---|
public static int abs(int a) | 獲取參數(shù)絕對(duì)值 |
public static double ceil(double a) | 向上取整 |
public static double floor(double a) | 向下取整 |
public static int round(float a) | 四舍五入 |
public static int max(int a,int b) | 獲取兩個(gè)int值中的較大值 |
public static double pow(double a,double b) | 返回a的b次冪的值 |
public static double random() | 返回值為double的隨機(jī)值,范圍[0.0,1.0) |
public static double sqrt(double a) | 返回a 的平方根 |
public static double cbrt(double a) | 返回a的立方根 |
System
System代表程序所在的系統(tǒng),也是一個(gè)工具類。
System類提供的常見方法
方法名 | 說明 |
---|---|
public static void exit(int status) | 終止當(dāng)前運(yùn)行的Java虛擬機(jī)。 |
public static long currentTimeMillis() | 返回當(dāng)前系統(tǒng)的時(shí)間毫秒值形式 |
- 這里的毫秒值指的是從1970年1月1日 00:00:00走到此刻的總的毫秒數(shù)(1s = 1000ms)。
Runtime
代表程序運(yùn)行所在的環(huán)境
Runtime類提供的常見方法
方法名 | 說明 |
---|---|
public static Runtime getRuntime() | 返回與當(dāng)前Java應(yīng)用程序關(guān)聯(lián)的運(yùn)行時(shí)對(duì)象 |
public void exit(int status) | 終止當(dāng)前運(yùn)行的虛擬機(jī) |
public int availableProcessors() | 返回Java虛擬機(jī)可用的處理器數(shù)。 |
public long totalMemory() | 返回Java虛擬機(jī)中的內(nèi)存總量 |
public long freeMemory() | 返回Java虛擬機(jī)中的可用內(nèi)存 |
public Process exec(String command) | 啟動(dòng)某個(gè)程序,并返回代表該程序的對(duì)象 |
BigDecimal
public static void main(String[] args) {// 浮點(diǎn)型運(yùn)算時(shí), 直接+ - * / 可能會(huì)出現(xiàn)運(yùn)算結(jié)果失真System.out.println(0.1 + 0.2);System.out.println(1.0 - 0.32);System.out.println(1.015 * 100);System.out.println(1.301 / 100);
}

構(gòu)造器 | 說明 |
---|---|
public BigDecimal(double val) 注意:不推薦使用這個(gè),無法總精確運(yùn)算 | 將 double轉(zhuǎn)換為BigDecimal |
public BigDecimal(String val) | 把String轉(zhuǎn)成BigDecimal |
方法名 | 說明 |
---|---|
public static BigDecimal valueOf(double val) | 轉(zhuǎn)換一個(gè) double成 BigDecimal |
public BigDecimal add(BigDecimal b) | 加法 |
public BigDecimal subtract(BigDecimal b) | 減法 |
public BigDecimal multiply(BigDecimal b) | 乘法 |
public BigDecimal divide(BigDecimal b) | 除法 |
public BigDecimal divide (另一個(gè)BigDecimal對(duì)象,精確幾位,舍入模式) | 除法、可以控制精確到小數(shù)幾位 |
public double doubleValue() | 將BigDecimal轉(zhuǎn)換為double |
JDK8之前傳統(tǒng)的日期. 時(shí)間
Date
代表的是日期和時(shí)間。
構(gòu)造器 | 說明 |
---|---|
public Date() | 創(chuàng)建一個(gè)Date對(duì)象,代表的是系統(tǒng)當(dāng)前此刻日期時(shí)間。 |
public Date(long time) | 把時(shí)間毫秒值轉(zhuǎn)換成Date日期對(duì)象。 |
常見方法 | 說明 |
---|---|
public long getTime() | 返回從1970年1月1日 00:00:00走到此刻的總的毫秒數(shù) |
public void setTime(long time) | 設(shè)置日期對(duì)象的時(shí)間為當(dāng)前時(shí)間毫秒值對(duì)應(yīng)的時(shí)間 |
SimpleDateFormat
**SimpleDateFormat:**代表簡(jiǎn)單日期格式化,可以用來把日期對(duì)象、時(shí)間毫秒值格式化成我們想要的形式。

SimpleDateFormat
常見構(gòu)造器 | 說明 |
---|---|
public SimpleDateFormat(String pattern) | 創(chuàng)建簡(jiǎn)單日期格式化對(duì)象,并封裝時(shí)間的格式 |
格式化時(shí)間的方法 | 說明 |
---|---|
public final String format(Date date) | 將日期格式化成日期/時(shí)間字符串 |
public final String format(Object time) | 將時(shí)間毫秒值式化成日期/時(shí)間字符串 |
時(shí)間格式的常見符號(hào)

SimpleDateFormat解析字符串時(shí)間成為日期對(duì)象
解析方法 | 說明 |
---|---|
public Date parse(String source) | 把字符串時(shí)間解析成日期對(duì)象 |
- SimpleDateFormat代表什么,有什么作用?
- 簡(jiǎn)單日期格式化對(duì)象
- 可以把日期對(duì)象及時(shí)間毫秒值格式化成我們想要的字符串形式。
- 可以把字符串的時(shí)間形式解析成Date日期對(duì)象。
- SimpleDateFormat的對(duì)象如何創(chuàng)建?
- public SimpleDateFormat(String pattern)
- SimpleDateFormat格式化,以及解析時(shí)間的方法是怎么樣的?
- public final String format(Date d):格式化日期對(duì)象
- public final String format(Object time):格式化時(shí)間毫秒值
- public Date parse(String source):解析字符串時(shí)間
Calendar
-
代表的是系統(tǒng)此刻時(shí)間對(duì)應(yīng)的日歷。
-
通過它可以單獨(dú)獲取、修改時(shí)間中的年、月、日、時(shí)、分、秒等。
-
代表的是系統(tǒng)此刻時(shí)間對(duì)應(yīng)的日歷,通過它可以單獨(dú)獲取、修改時(shí)間中的年、月、日、時(shí)、分、秒等
方法名 | 說明 |
---|---|
public static Calendar getInstance() | 獲取當(dāng)前日歷對(duì)象 |
public int get(int field) | 獲取日歷中的某個(gè)信息。 |
public final Date getTime() | 獲取日期對(duì)象。 |
public long getTimeInMillis() | 獲取時(shí)間毫秒值 |
public void set(int field,int value) | 修改日歷的某個(gè)信息。 |
public void add(int field,int amount) | 為某個(gè)信息增加/減少指定的值 |
**注意:**calendar是可變對(duì)象,一旦修改后其對(duì)象本身表示的時(shí)間將產(chǎn)生變化。
JDK8開始新增的日期、時(shí)間
為什么要學(xué)JDK8新增的時(shí)間?
JDK8新增的時(shí)間
LocalDate:年、月、日
LocalTime:時(shí)、分、秒
LocalDateTime:年、月、日、時(shí)、分、秒
ZoneId : 時(shí)區(qū)
ZonedDateTime:帶時(shí)區(qū)的時(shí)間
DateTimeFormatter : :用于時(shí)間的格式化和解析
java.time包下的類 :

LocalDate、LocalTime、LocalDateTime
LocalDate:代表本地日期(年、月、日、星期)
LocalTime:代表本地時(shí)間(時(shí)、分、秒、納秒)
LocalDateTime:代表本地日期、時(shí)間(年、月、日、星期、時(shí)、分、秒、納秒)它們獲取對(duì)象的方案
方法名 示例 public static Xxxx now(): 獲取系統(tǒng)當(dāng)前時(shí)間對(duì)應(yīng)的該對(duì)象 LocaDate ld = LocalDate.now(); LocalTime lt = LocalTime.now(); LocalDateTime ldt = LocalDateTime.now(); public static Xxxx of(…):獲取指定時(shí)間的對(duì)象 LocalDate localDate1 = LocalDate.of(2099 , 11,11); LocalTime localTime1 = LocalTime.of(9, 8, 59); LocalDateTime localDateTime1 = LocalDateTime.of(2025, 11, 16, 14, 30, 01);
轉(zhuǎn)換相關(guān)的API
LocalDateTime的轉(zhuǎn)換成LocalDate、LocalTime
方法名 | 說明 |
---|---|
public LocalDate toLocalDate() | 轉(zhuǎn)換成一個(gè)LocalDate對(duì)象 |
public LocalTime toLocalTime() | 轉(zhuǎn)換成一個(gè)LocalTime對(duì)象 |
LocalDate的常用API(都是處理年、月、日、星期相關(guān)的)。
方法名 | 說明 |
---|---|
public int geYear() | 獲取年 |
public int getMonthValue() | 獲取月份(1-12) |
public int getDayOfMonth() | 獲取日 |
public int getDayOfYear() | 獲取當(dāng)前是一年中的第幾天 |
Public DayOfWeek getDayOfWeek() | 獲取星期幾:ld.getDayOfWeek().getValue() |
方法名 | 說明 |
---|---|
withYear、withMonth、withDayOfMonth、withDayOfYear | 直接修改某個(gè)信息,返回新日期對(duì)象 |
plusYears、plusMonths、plusDays、plusWeeks | 把某個(gè)信息加多少,返回新日期對(duì)象 |
minusYears、minusMonths、minusDays,minusWeeks | 把某個(gè)信息減多少,返回新日期對(duì)象 |
equals isBefore isAfter | 判斷兩個(gè)日期對(duì)象,是否相等,在前還是在后 |
LocalTime的常用API (都是處理時(shí)、分、秒、納秒相關(guān)的)。
方法名 | 說明 |
---|---|
public int getHour() | 獲取小時(shí) |
public int getMinute() | 獲取分 |
public int getSecond() | 獲取秒 |
public int getNano() | 獲取納秒 |
方法名 | 說明 |
---|---|
withHour、withMinute、withSecond、withNano | 修改時(shí)間,返回新時(shí)間對(duì)象 |
plusHours、plusMinutes、plusSeconds、plusNanos | 把某個(gè)信息加多少,返回新時(shí)間對(duì)象 |
minusHours、minusMinutes、minusSeconds、minusNanos | 把某個(gè)信息減多少,返回新時(shí)間對(duì)象 |
equals isBefore isAfter | 判斷2個(gè)時(shí)間對(duì)象,是否相等,在前還是在后 |
LocalDateTime的常用API(可以處理年、月、日、星期、時(shí)、分、秒、納秒等信息)
方法名 | 說明 |
---|---|
getYear、getMonthValue、getDayOfMonth、getDayOfYear getDayOfWeek、getHour、getMinute、getSecond、getNano | 獲取年月日、時(shí)分秒、納秒等 |
withYear、withMonth、withDayOfMonth、withDayOfYear withHour、withMinute、withSecond、withNano | 修改某個(gè)信息,返回新日期時(shí)間對(duì)象 |
plusYears、plusMonths、plusDays、plusWeeks plusHours、plusMinutes、plusSeconds、plusNanos | 把某個(gè)信息加多少,返回新日期時(shí)間對(duì)象 |
minusYears、minusMonths、minusDays、minusWeeks minusHours、minusMinutes、minusSeconds、minusNanos | 把某個(gè)信息減多少,返回新日期時(shí)間對(duì)象 |
equals isBefore isAfter | 判斷2個(gè)時(shí)間對(duì)象,是否相等,在前還是在后 |
修改相關(guān)的API
- LocalDateTime 綜合了 LocalDate 和 LocalTime 里面的方法,所以下面只用 LocalDate 和 LocalTime 來舉例。
- 這些方法返回的是一個(gè)新的實(shí)例引用,因?yàn)長(zhǎng)ocalDateTime 、LocalDate 、LocalTime 都是不可變的。
方法名 | 說明 |
---|---|
plusDays, plusWeeks, plusMonths, plusYears | 向當(dāng)前 LocalDate 對(duì)象添加幾天、 幾周、幾個(gè)月、幾年 |
minusDays, minusWeeks, minusMonths, minusYears | 從當(dāng)前 LocalDate 對(duì)象減去幾天、 幾周、幾個(gè)月、幾年 |
withDayOfMonth, withDayOfYear, withMonth, withYear | 將月份天數(shù)、年份天數(shù)、月份、年 份 修 改 為 指 定 的 值 并 返 回 新 的 LocalDate 對(duì)象 |
isBefore, isAfter | 比較兩個(gè) LocalDate |
Instant時(shí)間戳
1、 Duration: 用于計(jì)算兩個(gè)“時(shí)間”間隔。
2、 Period: 用于計(jì)算兩個(gè)“日期”間隔。
ChronoUnit類可用于在單個(gè)時(shí)間單位內(nèi)測(cè)量一段時(shí)間,這個(gè)工具類是最全的了,可以用于比較所有的時(shí)間單位
ZoneId、ZonedDateTime
- ZoneId:代表時(shí)區(qū)Id
- 中國標(biāo)準(zhǔn)時(shí)間: 世界標(biāo)準(zhǔn)時(shí)間(UTC) + 8小時(shí)
ZoneId 時(shí)區(qū)的常見方法
方法名 | 說明 |
---|---|
public static Set getAvailableZoneIds() | 獲取Java中支持的所有時(shí)區(qū) |
public static ZoneId systemDefault() | 獲取系統(tǒng)默認(rèn)時(shí)區(qū) |
public static ZoneId of(String zoneId) | 獲取一個(gè)指定時(shí)區(qū) |
ZonedDateTime 帶時(shí)區(qū)時(shí)間的常見方法
方法名 | 說明 |
---|---|
public static ZonedDateTime now() | 獲取當(dāng)前時(shí)區(qū)的ZonedDateTime對(duì)象 |
public static ZonedDateTime now(ZoneId zone) | 獲取指定時(shí)區(qū)的ZonedDateTime對(duì)象 |
getYear、getMonthValue、getDayOfMonth、getDayOfYeargetDayOfWeek、getHour、getMinute、getSecond、getNano | 獲取年月日、時(shí)分秒、納秒等 |
public ZonedDateTime withXxx(時(shí)間) | 修改時(shí)間系列的方法 |
public ZonedDateTime minusXxx(時(shí)間) | 減少時(shí)間系列的方法 |
public ZonedDateTime plusXxx(時(shí)間) | 增加時(shí)間系列的方法 |
DateTimeFormatter
方法名 | 說明 |
---|---|
public static DateTimeFormatter ofPattern(時(shí)間格式) | 獲取格式化器對(duì)象 |
LocalDateTime提供的格式化、解析時(shí)間的方法
方法名 | 說明 |
---|---|
public String format(DateTimeFormatter formatter) | 格式化時(shí)間 |
public static LocalDateTime parse(CharSequence text,DateTimeFormatter formatter) | 解析時(shí)間 |
Arrays
用來操作數(shù)組的一個(gè)工具類。
Arrays類提供的的常見方法
方法名 | 說明 |
---|---|
public static String toString(類型[] arr) | 返回?cái)?shù)組的內(nèi)容(字符串形式) |
public static int[] copyOfRange(類型[] arr, 起始索引, 結(jié)束索引) | 拷貝數(shù)組(指定范圍) |
public static 類型 copyOf(類型[] arr, int newLength) | 拷貝數(shù)組 |
public static void setAll(double[] array, IntToDoubleFunction generator) | 把數(shù)組中的原數(shù)據(jù)改為新數(shù)據(jù) |
public static void sort(類型[] arr) | 對(duì)數(shù)組進(jìn)行排序(默認(rèn)是升序排序) |
public static void sort(類型[] a, Comparator<? super T> c) | 使用比較器對(duì)象自定義排序 |
public static int binarySearch(int[] a, int key) | 二分搜索數(shù)組中的數(shù)據(jù),存在返回索引,不存在返回-1 |
對(duì)數(shù)組中的數(shù)據(jù)進(jìn)行排序
double[] prices = {99.8, 128, 100};
Arrays.*sort*(prices);
System.out.println(Arrays.toString(prices));
99.8, 100.0, 128.0
如果數(shù)組中存儲(chǔ)的是對(duì)象, 如何排序?
Student[] students = new Student[4];
students[0] = new Student("蜘蛛精", 169.5, 23);students[1] = new Student("紫霞", 163.8, 26);students[2] = new Student("紫霞", 163.8, 26);students[3] = new Student("至尊寶", 167.5, 24);
- 方式一:
- 自然排序:讓該對(duì)象的類實(shí)現(xiàn)Comparable(比較規(guī)則)接口,重寫compareTo方法,制定比較規(guī)則
- 方式二:
- 比較器排序:使用下面這個(gè)sort方法,創(chuàng)建Comparator比較器接口的匿名內(nèi)部類對(duì)象,制定比較規(guī)則
public static void sort(T[] arr, Comparator<? super T> c) | 對(duì)數(shù)組進(jìn)行排序(支持自定義排序規(guī)則) |
---|
自定義排序規(guī)則時(shí),需要遵循的官方約定如下:
-
設(shè)置Comparator接口對(duì)應(yīng)的比較器對(duì)象,來定制比較規(guī)則
-
左邊對(duì)象大于右邊對(duì)象,返回正整數(shù);
-
左邊對(duì)象小于右邊對(duì)象,返回負(fù)整數(shù);
-
兩邊對(duì)象相等,返回0 這樣就可以得到升序
-