Java 时间类(中):JDK8 全新时间 API 详细教程

Java 时间类(中):JDK8 全新时间 API 详细教程
在这里插入图片描述
🏠个人主页:黎雁
🎬作者简介:C/C++/JAVA后端开发学习者
❄️个人专栏:C语言数据结构(C语言)EasyXJAVA游戏规划程序人生
✨ 从来绝巘须孤往,万里同尘即玉京
在这里插入图片描述


文章目录

在这里插入图片描述

Java 时间类(中):JDK8 全新时间 API 详细教程 🕘

线程安全、设计优雅、日常开发必用,建议和上篇 JDK7 时间类对照学习~

📝 文章摘要

  • 阅读时长:10 分钟
  • 适合人群
    1. 已经学完 JDK7 时间类,想升级 JDK8 写法的同学
    2. Java 初/中级开发者 → 重点看:LocalDateTime、DateTimeFormatter、时间计算
    3. 面试/工作党 → 重点看:不可变对象、线程安全、时间间隔工具
  • 本文内容:系统讲解 JDK8 全新时间类,包括时区、时间戳、日期时间、格式化、时间间隔,覆盖日常开发高频用法。

🧠 上篇知识回顾

  1. JDK7 时间类核心:Date(表示时间) + SimpleDateFormat(格式化解析) + Calendar(日历操作)
  2. 痛点:线程不安全、设计混乱(月份从 0 开始)、API 不够直观
  3. JDK8 新时间类优势:
    • 不可变对象,天然线程安全
    • 方法命名语义化,见名知意
    • 年月日常规从 1 开始,符合直觉
    • 按功能拆分(日期/时间/日期+时间),职责清晰

一、JDK8 时间类整体架构 🏛

JDK8 基于 ISO 8601 标准重构了时间 API,把时间功能拆解得清晰易懂:

类别核心类作用
时区ZoneId表示时区(如 Asia/Shanghai)
时间戳Instant表示 UTC 标准时间戳
带时区时间ZonedDateTime带时区的完整时间
纯日期LocalDate仅包含年月日
纯时间LocalTime仅包含时分秒纳秒
日期+时间LocalDateTime包含年月日时分秒
格式化解析DateTimeFormatter替代 SimpleDateFormat
时间间隔Period/Duration/ChronoUnit计算两个时间的间隔

二、ZoneId 时区类 🌍

1. 核心作用

表示全球各个时区,解决不同地区时间偏移的问题,是处理跨时区时间的基础。

2. 常用方法

方法名说明
ZoneId.systemDefault()获取系统默认时区
ZoneId.of(String zoneId)根据时区名获取指定时区
ZoneId.getAvailableZoneIds()获取 Java 支持的所有时区

3. 代码示例

importjava.time.ZoneId;importjava.util.Set;publicclassZoneIdDemo{publicstaticvoidmain(String[] args){// 1. 获取系统默认时区(国内通常是 Asia/Shanghai)ZoneId defaultZone =ZoneId.systemDefault();System.out.println("系统默认时区:"+ defaultZone);// 2. 获取指定时区ZoneId shanghaiZone =ZoneId.of("Asia/Shanghai");ZoneId tokyoZone =ZoneId.of("Asia/Tokyo");// 3. 查看所有支持的时区(约600个)Set<String> allZones =ZoneId.getAvailableZoneIds();System.out.println("Java支持的时区总数:"+ allZones.size());}}

三、Instant 时间戳类 ⚡

1. 核心作用

表示时间线上的一个瞬时点,对应 JDK7 的 Date 类,底层存储的是从 1970-01-01 00:00:00 UTC 开始的毫秒/纳秒数。

2. 常用方法

方法名说明
Instant.now()获取当前 UTC 时间戳
Instant.ofEpochMilli(long)根据毫秒值构建 Instant
Instant.ofEpochSecond(long)根据秒值构建 Instant
atZone(ZoneId zone)为时间戳绑定指定时区
isBefore(Instant other)判断当前时间是否在参数时间之前
isAfter(Instant other)判断当前时间是否在参数时间之后
plusSeconds(long) / minusSeconds(long)增加/减少指定秒数

3. 代码示例

importjava.time.Instant;importjava.time.ZoneId;importjava.time.ZonedDateTime;publicclassInstantDemo{publicstaticvoidmain(String[] args){// 1. 获取当前 UTC 时间戳Instant nowInstant =Instant.now();System.out.println("当前UTC时间戳:"+ nowInstant);// 2. 根据毫秒值构建(0毫秒对应1970-01-01 00:00:00 UTC)Instant zeroInstant =Instant.ofEpochMilli(0L);System.out.println("0毫秒对应的时间戳:"+ zeroInstant);// 3. 为时间戳绑定时区(转换为带时区的时间)ZonedDateTime shanghaiTime = nowInstant.atZone(ZoneId.of("Asia/Shanghai"));System.out.println("上海时区的当前时间:"+ shanghaiTime);// 4. 时间比较Instant oneSecond =Instant.ofEpochSecond(1L);boolean isBefore = zeroInstant.isBefore(oneSecond);// trueboolean isAfter = zeroInstant.isAfter(oneSecond);// falseSystem.out.println("0秒是否在1秒之前:"+ isBefore);// 5. 时间加减Instant addInstant = zeroInstant.plusSeconds(3600);// 加1小时System.out.println("加1小时后的时间:"+ addInstant);}}

四、ZonedDateTime 带时区时间 🕒

1. 核心特点

  • 包含完整的“日期+时间+时区”信息,是 Instant + ZoneId 的组合体
  • 不可变对象:修改时间会返回新的对象,原对象保持不变
  • 方法语义化,操作更直观

2. 常用方法

方法类型核心方法说明
获取对象ZonedDateTime.now()获取当前带时区时间
ZonedDateTime.of(年,月,日,时,分,秒,纳秒,时区)指定时间创建对象
ZonedDateTime.ofInstant(Instant, ZoneId)通过时间戳+时区创建
修改时间withYear(int) / withMonth(int)修改年/月(返回新对象)
增减时间plusYears(long) / minusMonths(long)增加/减少年/月(返回新对象)

3. 代码示例

importjava.time.Instant;importjava.time.ZoneId;importjava.time.ZonedDateTime;publicclassZonedDateTimeDemo{publicstaticvoidmain(String[] args){// 1. 获取当前带时区时间ZonedDateTime nowZoned =ZonedDateTime.now();System.out.println("当前带时区时间:"+ nowZoned);// 2. 指定时间创建ZonedDateTime customTime =ZonedDateTime.of(2026,10,1,// 年月日11,11,11,0,// 时分秒纳秒ZoneId.of("Asia/Shanghai")// 时区);System.out.println("指定的上海时间:"+ customTime);// 3. 通过Instant+时区创建Instant instant =Instant.ofEpochMilli(0L);ZonedDateTime zeroZoned =ZonedDateTime.ofInstant(instant,ZoneId.of("Asia/Shanghai"));System.out.println("0毫秒对应的上海时间:"+ zeroZoned);// 4. 修改时间(返回新对象,原对象不变)ZonedDateTime modifyYear = customTime.withYear(2025);ZonedDateTime minusMonth = modifyYear.minusMonths(1);System.out.println("修改并减1个月后的时间:"+ minusMonth);}}

五、DateTimeFormatter 格式化解析 ✨

1. 核心优势

替代 JDK7 的 SimpleDateFormat,解决了线程不安全的问题,是 JDK8 推荐的时间格式化工具。

2. 常用方法

方法名说明
DateTimeFormatter.ofPattern(String)根据指定格式创建格式化器
format(TemporalAccessor temporal)将时间对象格式化为字符串

3. 格式规则(与 SimpleDateFormat 一致)

常用格式模板:

  • yyyy-MM-dd:仅日期(如 2026-02-20)
  • HH:mm:ss:仅时间(如 15:30:45)
  • yyyy-MM-dd HH:mm:ss:日期+时间(如 2026-02-20 15:30:45)

4. 代码示例

importjava.time.ZonedDateTime;importjava.time.format.DateTimeFormatter;publicclassDateTimeFormatterDemo{publicstaticvoidmain(String[] args){// 1. 获取带时区的当前时间ZonedDateTime now =ZonedDateTime.now();// 2. 创建格式化器(指定格式)DateTimeFormatter formatter =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");// 3. 格式化时间(时间对象 → 字符串)String formatTime = formatter.format(now);System.out.println("格式化后的时间:"+ formatTime);}}

六、LocalDate / LocalTime / LocalDateTime 🌟

这三个类是 JDK8 时间 API 中日常开发使用频率最高的,按功能拆分,满足不同场景需求。

1. 功能分工

类名包含信息适用场景
LocalDate年、月、日生日、签到日期、订单日期
LocalTime时、分、秒、纳秒上下班打卡时间、活动开始时间
LocalDateTime年、月、日、时、分、秒日志记录、接口请求时间、数据创建时间

2. 通用核心方法

方法类型核心方法说明
获取对象XXX.now()获取当前时间/日期
XXX.of(...)指定时间/日期创建对象
获取字段getYear() / getMonthValue()获取年/月(月份1-12)
getDayOfMonth() / getHour()获取日/时
修改时间withYear(int) / withDayOfMonth(int)修改年/日
增减时间plusDays(long) / minusHours(long)增加/减少天/小时
时间比较isBefore(XXX) / isAfter(XXX)判断时间先后

3. LocalDate 代码示例(最常用)

importjava.time.LocalDate;importjava.time.DayOfWeek;publicclassLocalDateDemo{publicstaticvoidmain(String[] args){// 1. 获取当前日期LocalDate today =LocalDate.now();System.out.println("今天的日期:"+ today);// 2. 指定日期创建LocalDate customDate =LocalDate.of(2026,1,1);System.out.println("指定日期:"+ customDate);// 3. 获取日期字段int year = customDate.getYear();// 2026int month = customDate.getMonthValue();// 1(1-12,无需+1)int day = customDate.getDayOfMonth();// 1DayOfWeek week = customDate.getDayOfWeek();// 星期几System.out.println(year +"年"+ month +"月"+ day +"日 星期"+ week.getValue());// 4. 时间比较boolean isBefore = today.isBefore(customDate);System.out.println("今天是否在2026-01-01之前:"+ isBefore);// 5. 时间修改与增减LocalDate modifyDate = customDate.withYear(2025);// 修改年LocalDate addDate = modifyDate.plusMonths(3);// 加3个月System.out.println("修改并加3个月后的日期:"+ addDate);}}

4. 类之间的相互转换

LocalDateTime 可以拆分为 LocalDate 和 LocalTime:

importjava.time.LocalDateTime;publicclassLocalConvertDemo{publicstaticvoidmain(String[] args){LocalDateTime now =LocalDateTime.now();// LocalDateTime → LocalDateLocalDate date = now.toLocalDate();// LocalDateTime → LocalTimeLocalTime time = now.toLocalTime();System.out.println("拆分出的日期:"+ date);System.out.println("拆分出的时间:"+ time);}}

七、时间间隔计算:Period / Duration / ChronoUnit ⏳

日常开发中经常需要计算两个时间的间隔,JDK8 提供了三个工具类,满足不同粒度的需求。

1. Period —— 按“年、月、日”计算日期间隔

importjava.time.LocalDate;importjava.time.Period;publicclassPeriodDemo{publicstaticvoidmain(String[] args){// 起始日期LocalDate start =LocalDate.of(2020,1,1);// 结束日期LocalDate end =LocalDate.now();// 计算间隔Period period =Period.between(start, end);System.out.println("年差:"+ period.getYears());System.out.println("月差:"+ period.getMonths());System.out.println("日差:"+ period.getDays());System.out.println("总月数:"+ period.toTotalMonths());}}

2. Duration —— 按“时、分、秒、毫秒”计算时间间隔

importjava.time.LocalDateTime;importjava.time.Duration;publicclassDurationDemo{publicstaticvoidmain(String[] args){// 起始时间LocalDateTime start =LocalDateTime.of(2020,1,1,0,0);// 结束时间LocalDateTime end =LocalDateTime.now();// 计算间隔Duration duration =Duration.between(start, end);System.out.println("天差:"+ duration.toDays());System.out.println("小时差:"+ duration.toHours());System.out.println("分钟差:"+ duration.toMinutes());System.out.println("毫秒差:"+ duration.toMillis());}}

3. ChronoUnit —— 支持任意时间单位的间隔计算(最灵活)

importjava.time.LocalDateTime;importjava.time.temporal.ChronoUnit;publicclassChronoUnitDemo{publicstaticvoidmain(String[] args){// 起始时间LocalDateTime start =LocalDateTime.of(2007,8,13,14,0);// 结束时间LocalDateTime end =LocalDateTime.now();// 计算不同单位的间隔long years =ChronoUnit.YEARS.between(start, end);long months =ChronoUnit.MONTHS.between(start, end);long days =ChronoUnit.DAYS.between(start, end);long hours =ChronoUnit.HOURS.between(start, end);System.out.println("年差:"+ years);System.out.println("月差:"+ months);System.out.println("天差:"+ days);System.out.println("小时差:"+ hours);}}

📌 本篇知识回顾

  1. JDK8 时间类均为不可变对象,线程安全,修改/增减时间会返回新对象;
  2. 高频核心类:LocalDate(年月日)、LocalTime(时分秒)、LocalDateTime(年月日时分秒);
  3. 格式化工具:DateTimeFormatter 替代 SimpleDateFormat,解决线程安全问题;
  4. 时间间隔计算:Period(年月日)、Duration(时分秒)、ChronoUnit(任意单位)。

✍️ 写在最后

本篇我们系统学习了 JDK8 全新的时间 API,相比 JDK7 的传统时间类,它更优雅、更安全、更易用,是日常开发的首选。

下一篇我们将学习 Java 包装类相关知识:包括基本数据类型对应的包装类、自动装箱拆箱、常量池、字符串转基本类型等,这是 Java 基础的重要组成部分,也是面试高频考点!

如果你觉得本文对你有帮助,欢迎点赞 👍、收藏 💾、评论 💬,后续持续更新 Java 基础系列内容~

Read more

零基础学 OpenCV + Python 图像处理:手把手带你做人脸识别(附代码+典型案例)

零基础学 OpenCV + Python 图像处理:手把手带你做人脸识别(附代码+典型案例)

零基础学 OpenCV + Python 图像处理:手把手带你做人脸识别(附代码+典型案例) 关键词:opencv-python、opencv图像处理、opencv人脸识别代码python、python安装opencv库 亮点提示:本文面向零基础读者,手把手教你从环境搭建到实战应用,一步步深入,让你快速掌握 OpenCV+Python 图像处理与人脸识别技术。文中附带完整示例代码与典型案例,可直接复制、运行与深度改造,助你轻松入门并提升项目收藏率! 摘要 零基础学 OpenCV + Python 图像处理,手把手带你从 Python 安装 OpenCV 库、opencv-python 基础操作到 opencv图像处理、opencv人脸识别代码python 实战案例(静态图、人脸检测、摄像头实时识别)全流程讲解,附完整代码与典型案例,帮助初学者快速上手人脸识别项目。 目录 1. 为什么选择 OpenCV + Python?

By Ne0inhk
Java 常见Exception全面解析:出现场景、错误排查与代码修正实战

Java 常见Exception全面解析:出现场景、错误排查与代码修正实战

文章目录 * 课程导言 * 适用对象 * 学习目标 * 课程安排 * 教学方式 * 第一部分:Java异常体系回顾(约10分钟) * 1.1 异常是什么? * 1.2 Java异常体系结构 * 1.3 异常信息解读 * 第二课时(上):运行时异常深度剖析(约30分钟) * 2.1 NullPointerException(空指针异常) * 现象描述 * 出现场景 * 堆栈分析示例 * 排查方法流程图 * 代码修正与预防 * 2.2 ArrayIndexOutOfBoundsException(数组下标越界异常) * 现象描述 * 出现场景 * 堆栈分析示例 * 排查方法 * 代码修正与预防 * 2.3 ClassCastException(类型转换异常) * 现象描述 * 出现场景 * 堆栈分析示例 * 排查方法 * 代码修正与预防 * 2.

By Ne0inhk
Python-flask旅游景点酒店推荐系统的设计与开发-Pycharm django

Python-flask旅游景点酒店推荐系统的设计与开发-Pycharm django

目录 * 技术文章大纲:Python-Flask旅游景点酒店推荐系统开发(基于PyCharm与Django技术栈) * 系统架构设计 * 开发环境配置 * 核心功能实现 * 前后端交互 * 测试与部署 * 扩展方向 * 开发技术路线 * 源码lw获取/同行可拿货,招校园代理 :文章底部获取博主联系方式! 技术文章大纲:Python-Flask旅游景点酒店推荐系统开发(基于PyCharm与Django技术栈) 系统架构设计 采用Flask作为后端框架,结合Django的ORM模块进行数据管理,前端使用Bootstrap或Vue.js实现交互界面。系统分为用户模块、推荐算法模块、数据管理模块三大核心部分。 数据库设计使用SQLite或MySQL,包含用户表、景点表、酒店表、评分表等,通过Django的models.py定义数据结构。 开发环境配置 PyCharm中创建Flask项目,安装依赖库:flask, flask-sqlalchemy, django, pandas。通过virtualenv创建虚拟环境隔

By Ne0inhk
python无需验证码免登录12306抢票 --selenium(2)

python无需验证码免登录12306抢票 --selenium(2)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 @[TOC](python无需验证码免登录12306抢票 --selenium(2)) 前言 提示:这里可以添加本文要记录的大概内容: 就在刚刚我抢的票:2025年1月8日 上午9.00多 抢到了哈哈哈哈— 其实还是有用的我是在 8:59:56运行程序的 上一篇帖子,我们已经了解了如何用python自动登录12306实现自动抢票,现在我们来一个进阶的版本,实现cookie免登录,这样可绕过验证码实现自动抢票。同时包括环境如何配置,也在这个帖子里面。 上一篇帖子的链接:https://blog.ZEEKLOG.net/xaing1314/article/details/144868504 配置selenium自动化的帖子链接:https://blog.ZEEKLOG.net/xaing1314/article/details/144869489?spm=1001.2014.3001.5502

By Ne0inhk