434. Java 日期时间 API - Period 基于日期的时间段

文章目录

  • [434. Java 日期时间 API - Period 基于日期的时间段](#434. Java 日期时间 API - Period 基于日期的时间段)
    • [1️⃣ 计算年龄](#1️⃣ 计算年龄)
    • [2️⃣ 计算距离下一个生日还有多久](#2️⃣ 计算距离下一个生日还有多久)
    • [3️⃣ ⚠️ 注意事项](#3️⃣ ⚠️ 注意事项)
    • 🎯小结

434. Java 日期时间 API - Period 基于日期的时间段

当你需要处理 年、月、日 这样的"日历时间",就应该使用 Period 类。

👉 重点特点:

  • Period 使用 年、月、日 来度量时间。
  • 你可以通过 getYears()、getMonths()、getDays() 来获取具体的部分。
  • 如果你只想要一个单一单位(比如总天数),可以配合 ChronoUnit.between() 使用。

1️⃣ 计算年龄

假设某人出生在 1960 年 1 月 1 日,我们要算出他今天的年龄。

🚀 示例:

java 复制代码
import java.time.*;

public class PeriodExample {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

        // 使用 Period 计算 年、月、日
        Period p = Period.between(birthday, today);

        // 使用 ChronoUnit 计算总天数
        long p2 = ChronoUnit.DAYS.between(birthday, today);

        System.out.println("You are " + p.getYears() + " years, " 
                           + p.getMonths() + " months, and " 
                           + p.getDays() + " days old. (" 
                           + p2 + " days total)");
    }
}

✅ 可能输出:

java 复制代码
You are 64 years, 8 months, and 15 days old. (23640 days total)

👉 在这里:

  • Period 给你「自然日历的表达」(年、月、日)。
  • ChronoUnit.DAYS 给你「纯粹的总天数」。

2️⃣ 计算距离下一个生日还有多久

我们还可以用 Period 来算「距离下一个生日还有多久」。

🚀 示例:

java 复制代码
import java.time.*;

public class NextBirthday {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

        // 今年的生日
        LocalDate nextBDay = birthday.withYear(today.getYear());

        // 如果今年的生日已经过去了,就加一年
        if (nextBDay.isBefore(today) || nextBDay.isEqual(today)) {
            nextBDay = nextBDay.plusYears(1);
        }

        Period p = Period.between(today, nextBDay);
        long p2 = ChronoUnit.DAYS.between(today, nextBDay);

        System.out.println("There are " + p.getMonths() + " months, and "
                           + p.getDays() + " days until your next birthday. ("
                           + p2 + " total)");
    }
}

✅ 可能输出:

java 复制代码
There are 3 months, and 12 days until your next birthday. (104 total)

👉 在这里:

  • Period 告诉你还有「几个月 + 几天」。
  • ChronoUnit.DAYS 告诉你还有「总共多少天」。

3️⃣ ⚠️ 注意事项

  • 这些计算没有考虑 时区差异 。
    比如你在澳大利亚出生,现在住在印度班加罗尔,那么时区不同会导致「生日当天的精确计算」稍有偏差。
  • 如果涉及到 跨时区 的计算,应该结合 ZonedDateTime 使用:

🚀 示例片段:

java 复制代码
ZonedDateTime zdt = ZonedDateTime.of(birthday.atStartOfDay(), ZoneId.of("Australia/Sydney"));
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));

Period p = Period.between(zdt.toLocalDate(), now.toLocalDate());
System.out.println("跨时区年龄: " + p.getYears() + " 年 " + p.getMonths() + " 月 " + p.getDays() + " 天");

🎯小结

  • Period = 日历时间(年、月、日),贴近「人类语义」。
  • ChronoUnit.between = 单一度量(比如总天数),贴近「数值统计」。
  • 结合使用,两者互补,可以满足大多数场景。

👉 打个比方:

  • Period 像你日历本 📅 上写的「64 岁 8 个月 15 天」
  • ChronoUnit 像你体检单 📑 上精确写的「23640 天」。
相关推荐
羲云网络1 小时前
GPT Image 2.6 海报改横竖版:文字安全区、排版分离与交付验收
图像处理·人工智能·python·aigc
考虑考虑1 小时前
JDK27结构化并发
java·后端·java ee
曲径海盗2 小时前
【Python 学习第 65 天】字典基础
python·学习笔记·编程入门
Wellijohn2 小时前
Docker 瘦身、手写 MCP、异步并发:被问得最多的 12 个问题,一次答完
python·docker·mcp
Cosolar4 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
VW_4046 小时前
csdn-2026-11-23-第一站踩坑
爬虫·python
yiangL_6 小时前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
Dmz丶6 小时前
前端转 AI 100 天 Day 52:RAG 项目实战(一)——从零搭建企业知识库
python
geovindu8 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
skywalk81639 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness