Java日期工具类LocalDateTime
Java日期工具类LocalDateTime
- 嘚吧嘚
- LocalDateTime - API
- 创建时间
- 获取年月日时分秒
- 增加时间
- 减少时间
- 替换时间
- 日期比较
嘚吧嘚
压轴的来了,个人感觉LocalDateTime是使用频率最高的工具类,所以本篇像文章详细研究说明一下🧐。
如果看了Java日期工具类LocalDate和Java时间工具类LocalTime👍,那么本篇文章就算是一个整合、进阶吧😎。
LocalDateTime - API
创建时间
函数声明 | 描述 |
---|---|
static LocalDateTime now() | 获取默认时区的当前日期时间 |
static LocalDateTime now(ZoneId zone) | 获取指定时区的当前日期时间 |
static LocalDateTime now(Clock clock) | 从指定时钟获取当前日期时间 |
static LocalDateTime of(LocalDate date, LocalTime time) | 根据日期和时间对象获取LocalDateTime对象 |
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) | 根据指定的年、月、日、时、分、秒获取LocalDateTime实例 |
LocalDateTime now()
获取指定时区、时钟的日期时间就不多做说明了,和LocalDate一样。
// 获取当前时间
LocalDateTime now = LocalDateTime.now();
System.out.println("now : " + now);
// 格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String nowStr = now.format(formatter);
System.out.println("nowStr : " + nowStr);
LocalDateTime of()
获取年月日时分秒
函数声明 | 描述 |
---|---|
int getYear() | 获取年份 |
Month getMonth() | 获取月份,返回值为月份的枚举 |
int getMonthValue() | 获取月份,返回值为int类型月份 |
DayOfWeek getDayOfWeek() | 获取日期是星期几 |
int getDayOfMonth() | 获取日期在该月是第几天 |
int getDayOfYear() | 获取日期在该年是第几天 |
int getHour() | 获取小时 |
int getMinute() | 获取分钟 |
int getSecond() | 获取秒钟 |
int getNano() | 获取纳秒 |
LocalDateTime now = LocalDateTime.now();
// 获取年
System.out.println("getYear : " + now.getYear());
// 获取月份
System.out.println("getMonth : " + now.getMonth());
System.out.println("getMonthValue : " + now.getMonthValue());
// 获取日
System.out.println("getDayOfWeek : " + now.getDayOfWeek());
System.out.println("getDayOfMonth : " + now.getDayOfMonth());
System.out.println("getDayOfYear : " + now.getDayOfYear());
// 获取小时
System.out.println("getHour : " + now.getHour());
// 获取分钟
System.out.println("getMinute : " + now.getMinute());
// 获取秒
System.out.println("getSecond : " + now.getSecond());
// 获取纳秒
System.out.println("getNano : " + now.getNano());
增加时间
虽然是增加时间,传参可为正数,也可为负数。传参为正数时增加,传参为负数时减少。
函数声明 | 描述 |
---|---|
LocalDateTime plusYears(long years) | 增加年 |
LocalDateTime plusMonths(long months) | 增加月份 |
LocalDateTime plusWeeks(long weeks) | 增加周 |
LocalDateTime plusDays(long days) | 增加日 |
LocalDateTime plusHours(long hours) | 增加小时 |
LocalDateTime plusMinutes(long minutes) | 增加分钟 |
LocalDateTime plusSeconds(long seconds) | 增加秒 |
LocalDateTime plusNanos(long nanos) | 增加纳秒 |
增加年、月、周、日
LocalDateTime now = LocalDateTime.now();
System.out.println("now:" + now);
// 修改年份
System.out.println("plusYears : " + now.plusYears(1));
System.out.println("plusYears : " + now.plusYears(-1));
// 修改月份
System.out.println("plusMonths : " + now.plusMonths(1));
System.out.println("plusMonths : " + now.plusMonths(-2));
// 修改周
System.out.println("getDayOfWeek : " + now.plusWeeks(1));
System.out.println("getDayOfWeek : " + now.plusWeeks(-2));
// 修改日
System.out.println("plusDays : " + now.plusDays(3));
System.out.println("plusDays : " + now.plusDays(-3));
增加时、分、秒、纳秒
LocalDateTime now = LocalDateTime.now();System.out.println("now:" + now);// 修改小时System.out.println("plusHours : " + now.plusHours(2));System.out.println("plusHours : " + now.plusHours(-5));// 修改分钟System.out.println("plusMinutes : " + now.plusMinutes(20));System.out.println("plusMinutes : " + now.plusMinutes(-15));// 修改秒System.out.println("plusSeconds : " + now.plusSeconds(11));System.out.println("plusSeconds : " + now.plusSeconds(-31));// 修改纳秒System.out.println("plusNanos : " + now.plusNanos(53));System.out.println("plusNanos : " + now.plusNanos(-63));
减少时间
虽然是减少时间,传参可为正数,也可为负数。传参为正数时减少,传参为负数时增加。
函数声明 | 描述 |
---|---|
LocalDateTime minusYears(long years) | 减少年 |
LocalDateTime minusMonths(long months) | 减少月份 |
LocalDateTime minusWeeks(long weeks) | 减少周 |
LocalDateTime minusDays(long days) | 减少日 |
LocalDateTime minusHours(long hours) | 减少小时 |
LocalDateTime minusMinutes(long minutes) | 减少分钟 |
LocalDateTime minusSeconds(long seconds) | 减少秒 |
LocalDateTime minusNanos(long nanos) | 减少纳秒 |
减少其实也是调用的增加的方法
以减少年为例
LocalDateTime now = LocalDateTime.now();
System.out.println("now:" + now);
// 减少年
System.out.println("minusYears : " + now.minusYears(2));
System.out.println("minusYears : " + now.minusYears(-5));
替换时间
函数声明 | 描述 |
---|---|
LocalDateTime withYear(int year) | 替换年(-999999999-999999999) |
LocalDateTime withMonth(int month) | 替换月份(1-12) |
LocalDateTime withDayOfMonth(int dayOfMonth) | 替换为本月中的第几天(1-31) |
LocalDateTime withDayOfYear(int dayOfYear) | 替换为本年中的第几天(1-366) |
LocalDateTime withHour(int hour) | 替换小时(0-23) |
LocalDateTime withMinute(int minute) | 替换分钟(0-59) |
LocalDateTime withSecond(int second) | 替换秒(0-59) |
LocalDateTime withNano(int nanoOfSecond) | 替换纳秒(0-999999999) |
LocalDateTime now = LocalDateTime.now();
System.out.println("now:" + now);
// 替换年
System.out.println("withYear : " + now.withYear(1996));
// 替换月
System.out.println("withMonth : " + now.withMonth(5));
// 替换日
System.out.println("withDayOfMonth : " + now.withDayOfMonth(5));
System.out.println("withDayOfYear : " + now.withDayOfYear(5));
// 替换时
System.out.println("withHour : " + now.withHour(5));
// 替换分
System.out.println("withMinute : " + now.withMinute(5));
// 替换秒
System.out.println("withSecond : " + now.withSecond(5));
// 替换纳秒
System.out.println("withNano : " + now.withNano(5));
日期比较
函数声明 | 描述 |
---|---|
boolean isEqual(ChronoLocalDateTime<?> other) | 判断日期时间是否相等 |
boolean isAfter(ChronoLocalDateTime<?> other) | 检查是否在指定日期时间之前 |
boolean isBefore(ChronoLocalDateTime<?> other) | 检查是否在指定日期时间之后 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time1 = LocalDateTime.of(1999, 12, 5, 10, 12, 12);
LocalDateTime time2 = LocalDateTime.of(1999, 12, 6, 10, 12, 12);
String timeStr1 = time1.format(formatter);
String timeStr2 = time2.format(formatter);
System.out.println("time1 : " + timeStr1);
System.out.println("time2 : " + timeStr2);
System.out.println(timeStr1 + " = " + timeStr2 + " : " + time1.isEqual(time2));
System.out.println(timeStr1 + " > " + timeStr2 + " : " + time1.isAfter(time2));
System.out.println(timeStr1 + " < " + timeStr2 + " : " + time1.isBefore(time2));
Java8新特性日期工具类的梳理到此结束,欢迎大家补充说明😉。
相关文章:

Java日期工具类LocalDateTime
Java日期工具类LocalDateTime 嘚吧嘚LocalDateTime - API创建时间获取年月日时分秒增加时间减少时间替换时间日期比较 嘚吧嘚 压轴的来了,个人感觉LocalDateTime是使用频率最高的工具类,所以本篇像文章详细研究说明一下🧐。 如果看了Java日期…...
从C到C++1
一.思想过渡 前言:明确地说,学了C语言就相当于学了 C 的一半,从C语言转向 C 时,不需要再从头开始,接着C语言往下学就可以,所以我强烈建议先学C语言再学 C。 1.面向过程与面向对象 从“学院派”的角度来…...

[Angular] 笔记 18:Angular Router
Angular Router 视频 chatgpt: Angular 具有内置的大量工具、功能和库,功能强大且经过良好设计,如组件化架构、依赖注入、模块化系统、路由和HTTP客户端等。这些功能可以直接用于项目中,无需额外的设置或第三方库。这简化了开发流…...

微服务全链路灰度方案介绍
目录 一、单体架构下的服务发布 1.1 蓝绿发布 二、微服务架构下的服务发布 三、微服务场景下服务发布的问题 四、全链路灰度解决方案 4.1 物理环境隔离 4.2 逻辑环境隔离 4.3 全链路灰度方案实现技术 4.3.1 标签路由 4.3.2 节点打标 4.3.3 流量染色 4.3.4 分布式链路…...

低代码开发OA系统 低代码平台如何搭建OA办公系统
随着企业业务的复杂化和信息化的推进,如何快速、高效地构建一个适应企业发展需求的OA系统成为许多企业关注的焦点。本文将介绍低代码开发在构建OA系统方面的优势,并以白码低代码平台为例,探讨其在实际应用中的价值和功能。 什么是低代码开发?…...
构建Python的Windows整合包教程
构建Python的Windows整合包教程 原文链接:https://blog.gcc.ac.cn/post/2023/buildpythonwindowsintegrationpackagetutorial/ 构建Python的Windows整合包教程 - 我的博客原文链接 前言 之前的开源项目本地素材搜索有很多人想要Windows整合包,因为Wi…...

《整机柜服务器通用规范》由OCTC正式发布!浪潮信息牵头编制
近日,中国电子工业标准化技术协会开放计算标准工作委员会(OCTC)正式批准发布了《整机柜服务器通用规范》,该标准由浪潮信息牵头,中国工商银行、中国质量认证中心、英特尔、中国计量科学研究院等十余家单位联合编制&…...
Linux:修改和删除已有变量
变量修改 变量的修改有以下几种方式: 变量设置方式说明${变量名#匹配字串}从头向后开始匹配,删除符合匹配字串的最短数据${变量名##匹配字串}从头向后开始匹配,删除符合匹配字串的最长数据${变量名%匹配字串}从尾向前开始匹配,删除符合匹配…...

【23.12.29期--Spring篇】Spring的 IOC 介绍
介绍一下Spring的IOC ✔️引言✔️ lOC的优点✔️Spring的IOC✔️ 拓展知识仓✔️IOC是如何实现的? ✔️引言 所谓的IOC (inversion of control) ,就是控制反转的意思。何为控制反转? 在传统的程序设计中,应用程序代码通常控制着对象的创建和…...

【Python排序算法系列】—— 选择排序
🌈个人主页: Aileen_0v0 🔥热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 💫个人格言:"没有罗马,那就自己创造罗马~" 目录 选择排序 过程演示: 选择排序实现代码: 分析选择排序:…...

会议室占用时间段 - 华为OD统一考试
OD统一考试 题解: Java / Python / C++ 题目描述 现有若干个会议,所有会议共享一个会议室,用数组表示各个会议的开始时间和结束时间, 格式为: [[会议1开始时间,会议1结束时间],[会议2开始时间,会议2结束时间]] 请计算会议室占用时间段。 输入描述 [[会议1开始时间,…...

计算机网络复习5
传输层——端到端 文章目录 传输层——端到端功能传输层的寻址与端口UDPTCPTCP连接管理TCP可靠传输TCP流量控制TCP拥塞控制网络拥塞的处理 功能 从通信和信息处理的角度看,传输层向它上面的应用层提供通信服务,它属于面向通信部分的最高层,同…...

React Hooks 面试题 | 05.精选React Hooks面试题
🤍 前端开发工程师(主业)、技术博主(副业)、已过CET6 🍨 阿珊和她的猫_CSDN个人主页 🕠 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 🍚 蓝桥云课签约作者、已在蓝桥云…...

2024收入最高的编程语言
我的新书《Android App开发入门与实战》已于2020年8月由人民邮电出版社出版,欢迎购买。点击进入详情 1.Python Python 是最流行、用途最广泛的语言之一。它通常用于网络开发、数据科学、机器学习等。 以下是 Python 编程语言的一些主要用途: Web 开发&…...

Android笔记(二十三):Paging3分页加载库结合Compose的实现分层数据源访问
在Android笔记(二十二):Paging3分页加载库结合Compose的实现网络单一数据源访问一文中,实现了单一数据源的访问。在实际运行中,往往希望不是单纯地访问网络数据,更希望将访问的网络数据保存到移动终端的SQL…...

Python实现马赛克图片处理
文章目录 读取图片代码1、导入使用包2、读取图片 操作图片1、上下翻转2、左右翻转3、颜色颠倒4、降低图片精度5、打马赛克 说明: 在python中,图片可以看成一个三维的矩阵,第一维控制着垂直方向,第二维控制着水平方向,第…...

你能描述下你对vue生命周期的理解?在created和mounted这两个生命周期中请求数据有什么区别呢?
一、生命周期是什么 生命周期(Life Cycle)的概念应用很广泛,特别是在政治、经济、环境、技术、社会等诸多领域经常出现,其基本涵义可以通俗地理解为“从摇篮到坟墓”(Cradle-to-Grave)的整个过程在Vue中实…...

【经典算法】有趣的算法之---蚁群算法梳理
every blog every motto: You can do more than you think. 0. 前言 蚁群算法记录 1. 简介 蚁群算法(Ant Clony Optimization, ACO)是一种群智能算法,它是由一群无智能或有轻微智能的个体(Agent)通过相互协作而表现出智能行为,从而为求解复杂问题提供了一个新的可能性…...

第八届视觉、图像与信号处理国际会议(ICVISP 2024) | Ei, Scopus双检索
会议简介 Brief Introduction 2024年第八届视觉、图像与信号处理国际会议(ICVISP 2024) 会议时间:2024年12月27日-29日 召开地点:中国西双版纳 大会官网:ICVISP 2024-2024 8th International Conference on Vision, Image and Signal Process…...

《HelloGitHub》第 93 期
兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣、入门级的开源项目。 这里有实战项目、入门教程、黑科技、开源书籍、大厂开源项目等,涵盖多种编程语言 Python、Java、Go、C/C、Swift...让你在短时间内…...

C++初阶-list的底层
目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...

智慧工地云平台源码,基于微服务架构+Java+Spring Cloud +UniApp +MySql
智慧工地管理云平台系统,智慧工地全套源码,java版智慧工地源码,支持PC端、大屏端、移动端。 智慧工地聚焦建筑行业的市场需求,提供“平台网络终端”的整体解决方案,提供劳务管理、视频管理、智能监测、绿色施工、安全管…...
大语言模型如何处理长文本?常用文本分割技术详解
为什么需要文本分割? 引言:为什么需要文本分割?一、基础文本分割方法1. 按段落分割(Paragraph Splitting)2. 按句子分割(Sentence Splitting)二、高级文本分割策略3. 重叠分割(Sliding Window)4. 递归分割(Recursive Splitting)三、生产级工具推荐5. 使用LangChain的…...

【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例
文章目录 ★ position 的五种类型及基本用法 ★ 一、position 属性概述 二、position 的五种类型详解(初学者版) 1. static(默认值) 2. relative(相对定位) 3. absolute(绝对定位) 4. fixed(固定定位) 5. sticky(粘性定位) 三、定位元素的层级关系(z-i…...

微信小程序 - 手机震动
一、界面 <button type"primary" bindtap"shortVibrate">短震动</button> <button type"primary" bindtap"longVibrate">长震动</button> 二、js逻辑代码 注:文档 https://developers.weixin.qq…...
08. C#入门系列【类的基本概念】:开启编程世界的奇妙冒险
C#入门系列【类的基本概念】:开启编程世界的奇妙冒险 嘿,各位编程小白探险家!欢迎来到 C# 的奇幻大陆!今天咱们要深入探索这片大陆上至关重要的 “建筑”—— 类!别害怕,跟着我,保准让你轻松搞…...
人工智能--安全大模型训练计划:基于Fine-tuning + LLM Agent
安全大模型训练计划:基于Fine-tuning LLM Agent 1. 构建高质量安全数据集 目标:为安全大模型创建高质量、去偏、符合伦理的训练数据集,涵盖安全相关任务(如有害内容检测、隐私保护、道德推理等)。 1.1 数据收集 描…...

ubuntu系统文件误删(/lib/x86_64-linux-gnu/libc.so.6)修复方案 [成功解决]
报错信息:libc.so.6: cannot open shared object file: No such file or directory: #ls, ln, sudo...命令都不能用 error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory重启后报错信息&…...
es6+和css3新增的特性有哪些
一:ECMAScript 新特性(ES6) ES6 (2015) - 革命性更新 1,记住的方法,从一个方法里面用到了哪些技术 1,let /const块级作用域声明2,**默认参数**:函数参数可以设置默认值。3&#x…...

Matlab实现任意伪彩色图像可视化显示
Matlab实现任意伪彩色图像可视化显示 1、灰度原始图像2、RGB彩色原始图像 在科研研究中,如何展示好看的实验结果图像非常重要!!! 1、灰度原始图像 灰度图像每个像素点只有一个数值,代表该点的亮度(或…...