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 天」。
相关推荐
2601_962297257 分钟前
C# vs Java vs Python:YOLO工业部署性能对比实战
java·python·c·工业视觉·性能对比
计算机毕设定制辅导-无忧学长9 分钟前
《基于SpringBoot的马术俱乐部管理系统》
java·spring boot·后端
Persistent的粽子!11 分钟前
C++:类与对象(二)
开发语言·c++
Dreams°12316 分钟前
【Java后端+Vue前后端分离:内网正常、公网访问异常|5个高频经典踩坑完整复盘】
java·开发语言·vue.js
CTA终结者30 分钟前
示例、拆解和练习,要连成一条量化补课线
人工智能·python
lolijiaqi1531 分钟前
一线观察:长期体验后发现的医疗器械 CDMO 底层现象
大数据·python
学长毕业设计37 分钟前
基于SpringBoot的民间艺术传承管理系统(源码+文档+讲解视频)
java·spring boot·后端
小蒜学长38 分钟前
基于Springboot+Vue的环保行动志愿者招募系统设计与实现(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端
frjc1 小时前
如何在 IDEA 中打开并运行已有Java项目
java·ide·intellij-idea
.YM.Z1 小时前
C++ STL 容器深度剖析:vector 与 list 的模拟实现及对比
开发语言·c++·vector·list