在 Java 開發中,java.lang 與 java.util 包下的核心工具類是我們每天打交道最頻繁的 API。掌握 System、Runtime、Math、Date 以及 Calendar 的正確用法,不僅能提升開發效率,更能避免許多常見的坑。本文將通過實戰代碼示例,系統梳理這五大核心類的關鍵方法與最佳實踐。
System 類的字段和方法均為 static,提供了標準輸入/輸出、系統屬性訪問、環境變量讀取、GC 觸發以及高精度計時等功能。
line.separator、java.version)。Properties 對象。public class SystemDemo {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static void main(String[] args) {
// 1. 計時代碼執行時間
long start = System.currentTimeMillis();
// 模擬業務邏輯
int sum = 0;
for (int i = 0; i < 1_000_000; i++) sum += i;
long end = System.currentTimeMillis();
System.out.println("耗時:" + (end - start) + " ms");
// 2. 遍歷所有系統屬性
Properties props = System.getProperties();
for (String key : props.stringPropertyNames()) {
System.out.println(key + " = " + props.getProperty(key));
}
// 3. 自定義屬性與換行符
System.setProperty("app.name", "MyApp");
System.out.println("App Name: " + System.getProperty("app.name"));
System.out.println("Hello" + LINE_SEPARATOR + "World");
}
}

Runtime 採用單例模式(getRuntime()),每個 Java 應用僅有一個實例,提供了與運行環境交互的能力:執行外部命令、獲取內存信息、添加關閉鉤子等。
exec("notepad.exe") 或 exec("cmd /c dir")。totalMemory()、freeMemory()、maxMemory()。addShutdownHook(Thread hook) 在 JVM 關閉前執行清理邏輯。public class RuntimeDemo {
public static void main(String[] args) throws IOException, InterruptedException {
Runtime rt = Runtime.getRuntime();
// 啟動 notepad 並打開指定文件
Process p = rt.exec("notepad.exe D:\\1.txt");
// 等待 5 秒
Thread.sleep(5000);
// 銷毀進程
p.destroy();
System.out.println("進程已終止");
}
}
Math 所有方法均為 static,涵蓋了取整、冪運算、三角函數、對數、隨機數等常用數學運算。
| 方法 | 說明 | 示例 |
|---|---|---|
| ceil(double a) | 向上取整(返回 ≥ 參數的最小整數) | Math.ceil(12.3) → 13.0 |
| floor(double a) | 向下取整(返回 ≤ 參數的最大整數) | Math.floor(12.9) → 12.0 |
| round(double a) | 四捨五入 | Math.round(12.5) → 13 |
| pow(double a, double b) | a 的 b 次冪 | Math.pow(2, 10) → 1024.0 |
| random() | 返回 [0.0, 1.0) 隨機 double | 0.732... |
| abs(int a) | 絕對值 | Math.abs(-5) → 5 |
| max/min | 最大/最小值 | Math.max(3, 7) → 7 |
對於需要生成指定範圍整數、高性能或多線程場景,推薦使用 java.util.Random 或 ThreadLocalRandom(JDK 7+)。
Random r = new Random();
// 生成 0~9 的隨機整數
for (int i = 0; i < 10; i++) {
System.out.println(r.nextInt(10));
}
// 生成高斯分佈隨機數
System.out.println(r.nextGaussian());
注意:java.util.Date 及 SimpleDateFormat 為舊版 API(JDK 1.0/1.1),存在線程不安全、設計混亂(年份從 1900 開始、月份 0~11)等問題。新項目強烈建議使用 JDK 8+ 的 java.time 包(LocalDateTime、DateTimeFormatter 等)。本節僅作為維護舊代碼的參考。
long 型毫秒時間戳(UTC 1970-01-01 起)。new Date()(當前時間)、new Date(millis)(指定毫秒值)。getTime() → 毫秒值;setTime(millis) → 修改時間。Date now = new Date();
// 1. 內建風格格式化
DateFormat df1 = DateFormat.getDateInstance(DateFormat.FULL); // 2024年1月15日 星期一
DateFormat df2 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT); // 2024年1月15日 14:30
// 2. 自定義模式(線程不安全,請勿作為靜態共享變量)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(now); // 日期 → 字符串
Date parsed = sdf.parse("2024-01-15 14:30:00"); // 字符串 → 日期
public static int daysBetween(String d1, String d2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse(d1);
Date date2 = sdf.parse(d2);
long diff = Math.abs(date1.getTime() - date2.getTime());
return (int) (diff / (1000 * 60 * 60 * 24));
}
// 測試
System.out.println(daysBetween("2012-03-17", "2012-04-06")); // 輸出 20
Calendar 為抽象類,通過 getInstance() 獲取具體實現(通常是 GregorianCalendar)。它提供了豐富的字段操作(年、月、日、時、分、秒、毫秒、星期等)與日期運算(add、roll)。
YEAR、MONTH(0~11)、DAY_OF_MONTH、HOUR_OF_DAY(24 制)、MINUTE、SECOND、MILLISECONDDAY_OF_WEEK:1=星期日 … 7=星期六WEEK_OF_YEAR、WEEK_OF_MONTHpublic class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
printCalendar(c); // 2024年1月15日 星期一 14:30:45
// 判斷 2024 年二月天數(閏年 29 天)
printFebDays(2024);
}
static void printCalendar(Calendar c) {
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH) + 1; // 月份需 +1
int d = c.get(Calendar.DAY_OF_MONTH);
int w = c.get(Calendar.DAY_OF_WEEK);
int hh = c.get(Calendar.HOUR_OF_DAY);
int mm = c.get(Calendar.MINUTE);
int ss = c.get(Calendar.SECOND);
System.out.printf("%d年%02d月%02d日 %s %02d:%02d:%02d%n",
y, m, d, weekName(w), hh, mm, ss);
}
static void printFebDays(int year) {
Calendar c = Calendar.getInstance();
c.set(year, Calendar.FEBRUARY, 1); // 設置為 2 月 1 日
c.add(Calendar.DAY_OF_MONTH, -1); // 回退一天 → 1 月 31 日 或 2 月 29/28 日
printCalendar(c);
}
static String weekName(int w) {
String[] weeks = {"", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
return weeks[w];
}
}

java.time 包:LocalDateTime、ZonedDateTime、DateTimeFormatter 線程安全、API 直觀、功能強大。System.nanoTime(),而非 currentTimeMillis()。ThreadLocalRandom;安全敏感用 SecureRandom;並發高用 Random 子類或 SplittableRandom。SimpleDateFormat,改用 ThreadLocal 或 DateTimeFormatter(不可變、線程安全)。掌握這些核心類的精髓,能讓你在日常開發、性能調優、系統集成等場景中遊刃有餘。建議收藏備查,並在實戰中不斷驗證與深化理解。