Skip to content

Commit c4d8778

Browse files
CopilotCopilot
andcommitted
Add zh-CN translations for io, errors, and datetime categories
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0b0ce3a commit c4d8778

23 files changed

+414
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: 日期格式化
3+
oldApproach: SimpleDateFormat
4+
modernApproach: DateTimeFormatter
5+
summary: "使用线程安全、不可变的 DateTimeFormatter 格式化日期。"
6+
explanation: "DateTimeFormatter 是不可变且线程安全的,与 SimpleDateFormat 不同。它可以作为静态常量安全共享,支持 java.time 类型并提供更丰富的格式化选项。"
7+
whyModernWins:
8+
- icon: 🛡️
9+
title: 线程安全
10+
desc: "跨线程共享格式化器,无需同步。"
11+
- icon: 🔒
12+
title: 不可变
13+
desc: "无并发 bug——格式化器不能被修改。"
14+
- icon: 📐
15+
title: 更丰富的 API
16+
desc: "支持所有 java.time 类型,具有更好的本地化支持。"
17+
support:
18+
description: 自 JDK 8 起广泛可用(2014 年 3 月)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Duration 和 Period
3+
oldApproach: 毫秒数学
4+
modernApproach: Duration / Period
5+
summary: "使用类型安全的 Duration 和 Period 计算时间差。"
6+
explanation: "Duration 用于基于时间的量(小时、分钟、秒)。Period 用于日期量(年、月、日)。两者都是不可变的,并且支持算术运算。"
7+
whyModernWins:
8+
- icon: 🎯
9+
title: 类型安全
10+
desc: "Duration 用于时间,Period 用于日期——不会混淆。"
11+
- icon: 📖
12+
title: 自我描述
13+
desc: "Duration.ofDays(7) 比 7 * 24 * 60 * 60 * 1000L 更清晰。"
14+
- icon: 🔒
15+
title: 不可变
16+
desc: "时间量是值类型——不能意外修改。"
17+
support:
18+
description: 自 JDK 8 起广泛可用(2014 年 3 月)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: HexFormat
3+
oldApproach: 手动十六进制转换
4+
modernApproach: HexFormat
5+
summary: "使用 HexFormat 在十六进制字符串和字节数组之间转换。"
6+
explanation: "HexFormat 提供字节、整数和长整型的双向十六进制编码/解码。它支持大写/小写、分隔符和前缀/后缀配置。"
7+
whyModernWins:
8+
- icon: 📐
9+
title: 双向
10+
desc: "用一个 API 实现字节→十六进制和十六进制→字节转换。"
11+
- icon: 🔧
12+
title: 可配置
13+
desc: "大写、分隔符、前缀/后缀均可自定义。"
14+
- icon: 📏
15+
title: 简洁
16+
desc: "替代脆弱的手动 String.format 十六进制代码。"
17+
support:
18+
description: 自 JDK 17 LTS 起广泛可用(2021 年 9 月)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: 纳秒精度的 Instant
3+
oldApproach: 毫秒
4+
modernApproach: 纳秒
5+
summary: "获取微秒或纳秒精度的时间戳。"
6+
explanation: "Java 9 改进了时钟分辨率,使 Instant.now() 在支持的平台上可以捕获微秒甚至纳秒精度。对于需要高精度计时的基准测试和诊断非常有用。"
7+
whyModernWins:
8+
- icon: 🎯
9+
title: 更高精度
10+
desc: "微秒/纳秒 vs 毫秒时间戳。"
11+
- icon: 🔧
12+
title: 诊断友好
13+
desc: "更精确的时间测量,用于性能基准测试。"
14+
- icon: 📐
15+
title: 平台利用
16+
desc: "在支持高分辨率的平台上自动使用。"
17+
support:
18+
description: 自 JDK 9 起广泛可用(2017 年 9 月)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: java.time API 基础
3+
oldApproach: Date + Calendar
4+
modernApproach: java.time.*
5+
summary: "使用不可变、清晰的日期/时间类型替代 Date 和 Calendar。"
6+
explanation: "java.time 提供 LocalDate、LocalTime、LocalDateTime、Instant、ZonedDateTime 等。它们是不可变的、线程安全的,并且设计清晰——与可变且混乱的 Date/Calendar 不同。"
7+
whyModernWins:
8+
- icon: 🔒
9+
title: 不可变
10+
desc: "日期/时间值不能被意外修改。"
11+
- icon: 📖
12+
title: 语义清晰
13+
desc: "LocalDate vs ZonedDateTime vs Instant——每种类型都有明确用途。"
14+
- icon: 🌐
15+
title: 时区安全
16+
desc: "时区处理是显式的,而非隐藏的。"
17+
support:
18+
description: 自 JDK 8 起广泛可用(2014 年 3 月)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Math.clamp()
3+
oldApproach: 嵌套 min/max
4+
modernApproach: Math.clamp()
5+
summary: "用单个清晰的调用将值限制在边界之间。"
6+
explanation: "Math.clamp(value, min, max) 将值约束在范围 [min, max] 内。如果 min > max,则抛出 IllegalArgumentException。它适用于 int、long、float 和 double。"
7+
whyModernWins:
8+
- icon: 📖
9+
title: 自我描述
10+
desc: "clamp(value, min, max) 无歧义。"
11+
- icon: 📏
12+
title: 简洁
13+
desc: "替代嵌套的 Math.min(max, Math.max(min, value))。"
14+
- icon: 🔒
15+
title: 验证
16+
desc: "当 min > max 时抛出,捕获错误范围。"
17+
support:
18+
description: 自 JDK 21 LTS 起广泛可用(2023 年 9 月)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: 有帮助的 NullPointerException
3+
oldApproach: 晦涩的 NPE
4+
modernApproach: 详细的 NPE
5+
summary: "JVM 自动告诉您哪个变量为 null。"
6+
explanation: "有帮助的 NPE 描述哪个表达式为 null 以及操作失败的原因。不再需要在链式方法调用中猜测哪个引用为 null。"
7+
whyModernWins:
8+
- icon: 🔍
9+
title: 精确变量
10+
desc: "消息在链中指出 null 变量的名称。"
11+
- icon:
12+
title: 更快调试
13+
desc: "立即知道是哪个链中的引用为 null。"
14+
- icon: 🆓
15+
title: 零代码更改
16+
desc: "JVM 自动生成消息,无需代码更改。"
17+
support:
18+
description: 自 JDK 14 起广泛可用(2020 年 3 月)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: 多异常捕获
3+
oldApproach: 单独的 catch 块
4+
modernApproach: 多重 catch
5+
summary: "在单个 catch 块中捕获多种异常类型。"
6+
explanation: "多重 catch 用相同的代码处理多种异常类型。异常变量是实际上 final 的,防止类型不安全的重新赋值。"
7+
whyModernWins:
8+
- icon: 📏
9+
title: DRY 原则
10+
desc: "相同的处理逻辑只写一次,而非三次。"
11+
- icon: 🔒
12+
title: 类型安全
13+
desc: "捕获的异常是实际上 final 的——无法被重新赋值。"
14+
- icon: 🧹
15+
title: 更少代码
16+
desc: "消除相同 catch 块的重复。"
17+
support:
18+
description: 自 JDK 7 起广泛可用(2011 年 7 月)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: switch 中的 null case
3+
oldApproach: switch 前的守卫
4+
modernApproach: case null
5+
summary: "直接将 null 作为 switch case 处理——无需单独的守卫。"
6+
explanation: "模式匹配 switch 可以将 null 作为 case 标签匹配。这消除了在进入 switch 之前的单独 null 检查,使 null 处理在 switch 中清晰可见。"
7+
whyModernWins:
8+
- icon: 🎯
9+
title: 显式
10+
desc: "null 处理直接在 switch 中可见。"
11+
- icon: 📏
12+
title: 更少代码
13+
desc: "无需在 switch 之前单独进行 null 检查。"
14+
- icon: 🔒
15+
title: 防止 NPE
16+
desc: "将 null 作为 case 处理,防止 NullPointerException。"
17+
support:
18+
description: 自 JDK 21 LTS 起广泛可用(2023 年 9 月)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Optional 链式调用
3+
oldApproach: 嵌套 null 检查
4+
modernApproach: Optional 管道
5+
summary: "用 Optional 管道替代嵌套 null 检查。"
6+
explanation: "Optional.map() 透明地处理 null 值进行链式调用,在第一个 empty 时短路。这消除了嵌套的 if-null 检查,使正确路径清晰可见。"
7+
whyModernWins:
8+
- icon: 🔗
9+
title: 可链式调用
10+
desc: "每个 .map() 步骤透明地处理 null。"
11+
- icon: 📖
12+
title: 快乐路径清晰
13+
desc: "正常流程线性可读,无嵌套。"
14+
- icon: 🚫
15+
title: 无显式 null 检查
16+
desc: "null 处理内置于 Optional 的行为中。"
17+
support:
18+
description: 自 JDK 8+ 起可用(JDK 9+ 中有所改进)

0 commit comments

Comments
 (0)