当前位置: 首页 > backend >正文

Java时间API终极指南

Java 时间 API 概述

Java 提供了多个时间相关的 API,主要包括 java.util.Datejava.util.Calendarjava.time 包(Java 8 引入)。其中,java.time 是最现代且功能最全面的时间 API。以下分为新旧 API 分别介绍。


java.time 包(Java 8+ 推荐使用)

java.time 包分为多个类,包括 LocalDateLocalTimeLocalDateTimeZonedDateTime 等。

LocalDate(仅日期)
// 无参构造:获取当前日期
LocalDate today = LocalDate.now();
System.out.println("Today: " + today); // 输出:2023-10-05// 有参构造:指定日期
LocalDate specificDate = LocalDate.of(2023, 10, 1);
System.out.println("Specific Date: " + specificDate); // 输出:2023-10-01// 成员方法示例
LocalDate tomorrow = today.plusDays(1); // 增加 1 天
System.out.println("Tomorrow: " + tomorrow);int year = today.getYear(); // 获取年份
System.out.println("Year: " + year);

LocalTime(仅时间)
// 无参构造:获取当前时间
LocalTime currentTime = LocalTime.now();
System.out.println("Current Time: " + currentTime); // 输出:14:30:15.123// 有参构造:指定时间
LocalTime specificTime = LocalTime.of(12, 30, 45);
System.out.println("Specific Time: " + specificTime); // 输出:12:30:45// 成员方法示例
LocalTime laterTime = currentTime.plusHours(2); // 增加 2 小时
System.out.println("Later Time: " + laterTime);int hour = specificTime.getHour(); // 获取小时
System.out.println("Hour: " + hour);

LocalDateTime(日期 + 时间)
// 无参构造:获取当前日期和时间
LocalDateTime now = LocalDateTime.now();
System.out.println("Now: " + now); // 输出:2023-10-05T14:30:15.123// 有参构造:指定日期时间
LocalDateTime specificDateTime = LocalDateTime.of(2023, 10, 1, 12, 30);
System.out.println("Specific DateTime: " + specificDateTime); // 输出:2023-10-01T12:30// 成员方法示例
LocalDateTime nextMonth = now.plusMonths(1); // 增加 1 个月
System.out.println("Next Month: " + nextMonth);int dayOfMonth = specificDateTime.getDayOfMonth(); // 获取日
System.out.println("Day: " + dayOfMonth);

ZonedDateTime(带时区的日期时间)
// 无参构造:获取当前时区的日期时间
ZonedDateTime zonedNow = ZonedDateTime.now();
System.out.println("Zoned Now: " + zonedNow); // 输出:2023-10-05T14:30:15.123+08:00[Asia/Shanghai]// 有参构造:指定时区
ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime newYorkTime = ZonedDateTime.now(zoneId);
System.out.println("New York Time: " + newYorkTime); // 输出:2023-10-05T02:30:15.123-04:00[America/New_York]// 成员方法示例
ZoneId currentZone = zonedNow.getZone(); // 获取时区
System.out.println("Current Zone: " + currentZone);


java.util.Date(旧 API,不推荐)

// 无参构造:当前时间
Date currentDate = new Date();
System.out.println("Current Date: " + currentDate); // 输出:Thu Oct 05 14:30:15 CST 2023// 有参构造:指定时间(毫秒时间戳)
Date specificDate = new Date(1696491000000L);
System.out.println("Specific Date: " + specificDate); // 输出:Wed Oct 04 00:00:00 CST 2023// 成员方法示例
long timeInMillis = currentDate.getTime(); // 获取毫秒时间戳
System.out.println("Time in Millis: " + timeInMillis);


java.util.Calendar(旧 API,不推荐)

// 无参构造:当前时间
Calendar calendar = Calendar.getInstance();
System.out.println("Calendar Time: " + calendar.getTime()); // 输出:Thu Oct 05 14:30:15 CST 2023// 有参构造:指定日期(需先获取实例再设置)
Calendar specificCalendar = Calendar.getInstance();
specificCalendar.set(2023, Calendar.OCTOBER, 1); // 注意月份从 0 开始
System.out.println("Specific Calendar: " + specificCalendar.getTime()); // 输出:Sun Oct 01 14:30:15 CST 2023// 成员方法示例
int month = calendar.get(Calendar.MONTH); // 获取月份(0-11)
System.out.println("Month: " + month);calendar.add(Calendar.DAY_OF_MONTH, 5); // 增加 5 天
System.out.println("After 5 Days: " + calendar.getTime());


总结

  • java.time(推荐):功能丰富,线程安全,适用于现代 Java 应用。
  • java.util.Date / Calendar(旧 API):存在设计缺陷,建议仅用于兼容旧代码。

使用时优先选择 java.time 包,代码更简洁、可读性更强。

http://www.xdnf.cn/news/11930.html

相关文章:

  • C++11 中 auto 和 decltype 的深入解析
  • DeepSeek本地部署及WebUI可视化教程
  • 豆瓣图书评论数据分析与可视化
  • CentOS在vmware局域网内搭建DHCP服务器【踩坑记录】
  • 2025年- H66-Lc174--215.数组中的第k个最大元素(小根堆,堆顶元素是当前堆元素里面最小的)--Java版
  • 【计算机网络】HTTPS
  • OD 算法题 B卷【排队游戏】
  • Odoo 审批模块深度解析
  • 学习logging模块
  • nt!CcInitializeCacheMap函数分析初始化Vacbs结构
  • nmcli connection常用命令及设置wifi为AP模式
  • 【Redis实战:缓存与消息队列的应用】
  • Ethernet IP转Modbus网关在热泵机组中的协议转换技术实现
  • [C++入门]简化的艺术---对模版的初步探索
  • 敏捷项目管理:重塑价值交付的动态协作范式
  • 什么是内网映射?如何将内网ip映射到外网访问?
  • OPenCV CUDA模块图像处理-----对图像执行 均值漂移滤波(Mean Shift Filtering)函数meanShiftFiltering()
  • 一起学Spring AI:核心概念
  • 极速唤醒:高通平台 Android15 默认跳过锁屏,秒启主界面!
  • 每天总结一个html标签——Audio音频标签
  • ideal2022.3.1版本编译项目报java: OutOfMemoryError: insufficient memory
  • iOS上传应用包错误问题 “Invalid bundle. The “UIInterfaceOrientationPortrait”“
  • 打卡第36天:模型可视化以及推理
  • 机器学习监督学习sklearn实战三:八种算法对印第安人糖尿病预测数据进行分类和比较
  • 什么是终端安全管理系统(终端安全管理软件2024科普)
  • 12306高并发计算架构揭秘:Apache Geode 客户端接入与实践
  • OpenCV C++ 心形雨动画
  • Web3时代的数据保护挑战与应对策略
  • Elasticsearch的插件(Plugin)系统介绍
  • Java中Git基础操作详解(clone、commit、push、branch)