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新特性日期工具类的梳理到此结束,欢迎大家补充说明😉。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/307353.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【网络安全 | XCTF】2017_Dating_in_Singapore

正文 题目描述&#xff1a; 01081522291516170310172431-050607132027262728-0102030209162330-02091623020310090910172423-02010814222930-0605041118252627-0203040310172431-0102030108152229151617-04050604111825181920-0108152229303124171003-261912052028211407-0405…

Apache多后缀解析漏洞

漏洞描述&#xff1a; apahe解析文件时候有一特性&#xff0c;Apache默认一个文件可以有多个以点分割的后缀&#xff0c;apache会从最右边开始识别其后缀名&#xff0c;如遇无法识别的后缀名则依次往左进行识别 如果运维人员给.php后缀的文件添加了处理程序 AddHandler applic…

喜讯!九章云极DataCanvas公司顺利通过ITSS运维二级认证

近日&#xff0c;九章云极Datacanvas公司顺利通过中国电子工业标准化技术协会信息技术服务分会专家现场答辩评审&#xff0c;成功取得《信息技术服务标准&#xff08;ITSS&#xff09;符合性二级证书》。本次顺利通过认证&#xff0c;是对九章云极Datacanvas在信息运维服务整体…

软件测试/测试开发丨Windows Appium环境搭建

windows 版本 Appium 环境搭建 安装 nodejs 下载.msi文件 https://nodejs.org/en/download/ 注意&#xff1a; 1、下载12.*版本双击安装即可。 2、无须配置环境变量,直接重启一个 cmd 输入下面的命令&#xff0c;能够查看这两个版本号即安装成功。 安装 appium desktop 直…

openwrt的overlay扩容,再也不用担心磁盘不足了!

overlay扩容 1.准备好磁盘&#xff0c;先进行分区&#xff0c;也可以部分去&#xff0c;然后格式&#xff08;可以使用windows的diskgenius格式化&#xff0c;需要知注意的是格式化为ext4格式&#xff09;也可以通过ssh登录后台&#xff0c;命令行使用mkfs.ext4 /dev/sda1的方…

软件测试/测试开发丨Python常用数据结构-列表list

列表的定义 列表是有序的可变元素的集合&#xff0c;使用中括号[ ]包围&#xff0c;元素之间用逗号分隔&#xff1b;列表是动态的&#xff0c;可以随时扩展和收缩&#xff1b;列表是异构的&#xff0c;可以同时存放不同类型的对象&#xff1b;列表允许出现重复的元素。 列表的…

【小程序八股文】系列之篇章一 | 小程序基础及与其他产品区别

【小程序八股文】系列之篇章一 | 小程序基础及与其他产品区别 前言概览一、 微信小程序基础/背景小程序的理解微信小程序的优点及缺点简述一下微信小程序的相关文件类型简述一下小程序的开发流程&#xff1f;简述一下微信小程序的框架&#xff1f; 二、微信小程序与其他的区别&…

再传捷报!百望云荣登投资家网“2023年度企业服务领域创新企业TOP20”

近日&#xff0c;投资家网旗下投资家研究院重磅发布“投资家网2023中国价值企业榜”。经过层层严格评选&#xff0c;百望云荣登“2023年度企业服务领域创新企业TOP20”&#xff0c;再次说明了业界权威机构认可百望云的创新能力和市场价值。 本次评选&#xff0c;投资家网旗下投…

[鹏城杯 2022]简单包含

[鹏城杯 2022]简单包含 wp 题目代码如下&#xff1a; <?php highlight_file(__FILE__); include($_POST["flag"]); //flag in /var/www/html/flag.php; 直接 POST 传参&#xff1a; flag/var/www/html/flag.php 会触发 waf 。 尝试用伪协议读取&#xff1a; …

SV接口的驱动和采样_2023.12.27】

cb 使用cloking block进行信号的同步 在cloking block&#xff0c;所有信号的采样和驱动&#xff0c;都是和时钟同步的 clocking cb &#xff08;posedge clk&#xff09;; input grant; output request; endclocking接口同步 用和wait来同步测试平台中的信号 bus.cb; 接口…

【Angular 】Angular 模板中基于角色的访问控制

您是否在Angular模板中实现角色库访问控制&#xff1f;一种方法是通过*ngIf&#xff0c;但我不会选择该路径&#xff0c;因为它将在Angular模板中包含自定义函数&#xff0c;并且很难维护。正确的方法是使用Angular结构指令&#x1f680;. 什么是RBAC&#xff1f; 基于角色的…

年底离职潮来了!来聊聊程序员的离职跳槽

每当元旦春节将至的时候&#xff0c;办公室的气氛也诡异起来&#xff0c;空气弥漫着离职的味道。因为积累许久的负面情绪长期无法获得释放&#xff0c;打工人对工作的容忍度越发稀薄了起来&#xff0c;有的打工人看似正襟危坐地坐在工位上&#xff0c;实则愤然辞职的念头在心里…