一.LocalDateTime

LocalDateTime是JDK8的新时间特性,它解决了Date类和Calender类的很多不足,如使用不方便,线程不安全,以及获取时间戳只能拿到毫秒而不能拿到纳秒等问题

使用LocalDateTime可以获得的时间有:年-月-日,时:分:秒,纳秒

而LocalDate和LocalTime可以组合成LocalDateime,反之,LocalDateTime也可以分解成为它们两个

LocalDate封装的是 :年-月-日

LocalTime封装的是:时:分:秒,纳秒

LocalDateTime封装了二者,所以也是开发中使用的最多的,其内部的方法使用也是差不多的,可以类举,所以我们主要看LocalDateTime

LocalDateTime 是 Java 8 中引入的一个类,属于 java.time 包的一部分,是用来表示没有时区的日期和时间的。LocalDateTime 是不可变的,也就是说一旦创建了 LocalDateTime 对象,就不能修改它的值。

LocalDateTime 提供了许多有用的方法来操作日期和时间,例如获取日期和时间的各个部分(年、月、日、小时、分钟等),计算两个日期之间的差异,以及将日期和时间格式化为字符串等。

获得LocalDateTime

//获得LocalDateTime对象
LocalDateTime NowTime = LocalDateTime.now();
System.out.println(NowTime); //2024-01-07T16:58:40.024

获得相关日期的方法:

//获得年
int year = nowTime.getYear();
System.out.println(year);//2024
//获得月
Month month = nowTime.getMonth();
System.out.println(month.getValue());//1
//获得日
int dayOfYear = nowTime.getDayOfYear();
System.out.println(dayOfYear);//7
//获得星期
int value = nowTime.getDayOfWeek().getValue();
System.out.println(value);//7
//获得小时
int hour = nowTime.getHour();
System.out.println(hour);//17

with----():修改日期的,并且返回一个新的对象

//修改年
LocalDateTime newYear = nowTime.withYear(2028);
System.out.println(newYear.getYear()); //2028
//修改月
LocalDateTime newMonth = nowTime.withMonth(7);
System.out.println(newMonth.getMonth().getValue());//7
//修改分钟
LocalDateTime newMinute = nowTime.withMinute(50);
System.out.println(newMinute.getMinute());//50

plus----():增加日期,对某个日期进行新增,并且返回一个新的对象

//增加8个月
LocalDateTime newMonth = nowTime.plusMonths(8);
System.out.println(newMonth.getMonth().getValue());// 9 = 1 + 8
//增加3小时
LocalDateTime newHour = nowTime.plusHours(3);
System.out.println(newHour.getHour()); //20 = 17 + 3

minus----():减少日期,对某个日期进行减少,并且返回一个新的对象

//日期减少5天
LocalDateTime newDay = nowTime.minusDays(5);
System.out.println(newDay.getDayOfYear()); // 2 = 7-5
//日期减少5s
LocalDateTime newSecond = nowTime.minusSeconds(5);
System.out.println(newSecond.getSecond());// 17 = 22-5

equlas,isBefore,isAfter:时间对比方法

//获得LocalDateTime对象
LocalDateTime nowTime = LocalDateTime.now();
//新增1s
LocalDateTime newSecond = nowTime.plusSeconds(1);
//equals
boolean a = nowTime.equals(newSecond);
System.out.println(a); //false nowTime 比 newSecond 少 1s
//isAfter
boolean b = nowTime.isAfter(newSecond);
System.out.println(b);//false nowTime 比 newSecond 少 1s,故 nowTime在newSecond之后
//isBefore
boolean c = nowTime.isBefore(newSecond);
System.out.println(c); //ture nowTime 比 newSecond 少 1s, 故 nowTime在newSecond之后

二.ZoneID时区ID

时区用于Java开发中取任何地区的时间信息,当然指的是标准地区时间,也就是,中国可以取到北京,上海等地区时间,不是每个城市都可以取到,一定是标志性城市

在Java中,ZoneId 是 java.time 包中的一个类,它用于表示一个特定的时区。时区是一个地理区域,其时间标准由一个特定的时区偏移量定义。这个时区偏移量是相对于协调世界时(UTC)的偏移量。

使用 ZoneId,你可以更容易地处理与特定时区相关的日期和时间,并执行时区特定的计算和转换

获取当前时区:

//获取系统默认的时区(当前系统)
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId.getId());//Asia/Shanghai
System.out.println(zoneId); //Asia/Shanghai

获取时区集合:

Set<String> zoneIds = ZoneId.getAvailableZoneIds()
System.out.println(zoneIds);//内容太多,便展示,包括所有的时区

将时区封装成ZoneId:

//把时区Id封装成ZoneId
ZoneId Chongqing = ZoneId.of("Asia/Chongqing");//亚洲重庆

获得ZoneId的时间:

//获得重庆的时间
ZonedDateTime ChongqingTime = ZonedDateTime.now(Chongqing);
System.out.println(ChongqingTime);

获得世界标准时间----本次子午线时间:

System.out.println(ChongqingTime);//2024-01-07T09:54:36.623Z
//获得世界标准时间--本初子午线
ZonedDateTime utc = ZonedDateTime.now(Clock.systemUTC());
System.out.println(utc); //2024-01-07T09:54:36.623Z

获得系统当前时区时间:

//获取系统的默认时间
ZonedDateTime nowTime = ZonedDateTime.now();
System.out.println(nowTime);

时区时间也是支持LocaDateTime类相关的方法的:

//在时区时间的基础上新增8年
ZonedDateTime addEightYear = nowTime.plusYears(8);
System.out.println(addEightYear);
//在时区时间的基础上减少10天
ZonedDateTime minusTenDay = nowTime.minusDays(10);
System.out.println(minusTenDay);

三.Instant

时刻,时间戳,它代表的是某一时刻到1970年1月1日0:0:0的毫秒数,不足1毫秒的用纳秒代替

1毫秒 = 1000微秒

1微秒=1000纳秒

在Java中,Instant 是 java.time 包中的一个类,它用于表示一个特定的时间点,通常与UTC时间关联。与 Date 类相比,Instant 提供了更精确的时间表示,因为它不包含时区或夏令时调整的信息。

Instant 类包含表示时间的基本属性,如年、月、日、小时、分钟和秒,但这些属性通常只用于计算时间差,而不是用于表示具体的时间点。

获取Instant对象:

//获取Instant对象
Instant now = Instant.now();

获取毫秒和纳秒:

 //获取毫秒
long epochSecond = now.getEpochSecond();
System.out.println(epochSecond);//1704622124
//获取纳秒
int nano = now.getNano();
System.out.println(nano);//825000000

equlas,isBefore,isAfter:时间对比方法

//开始时间戳
Instant start = Instant.now();
Thread.sleep(5);//休息5毫秒
//结束时间戳
Instant end = Instant.now();
//equals
boolean equals = start.equals(end);
System.out.println(equals); //false
//isAfter
boolean after = start.isAfter(end);
System.out.println(after); //false
//isBefore
boolean before = start.isBefore(end);
System.out.println(before);//true

四.DateTimeFormatter

时间格式化器

是JDK8新出的时间格式类,用来代替SimpleDateFormat类的,是线程安全的类

DateTimeFormatter 是 Java 8 及其以后版本中 java.time 包的一部分,用于格式化和解析日期和时间。这个类提供了一种灵活的方式来表示日期和时间的格式

创建日期格式化器:

// 创建日期格式化器对象
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

格式化时间:

//获取当前系统时间
LocalDateTime now = LocalDateTime.now();
//格式化时间
String time = pattern.format(now);
System.out.println(time);//2024年01月07日 20:27:31

利用LocacDateTime对象的 format方法格式化时间:

//获取当前系统时间
LocalDateTime now = LocalDateTime.now();
//格式化时间
String time = now.format(pattern);
System.out.println(time);//2024年01月07日 20:27:31

解析时间:

// 创建日期格式化器对象
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
//解析时间
String dat = "2024年12月12日 12:12:10";
//字符串格式转时间格式
LocalDateTime time = LocalDateTime.parse(dat, pattern);
System.out.println(time);

注意:解析时间的时候一定要注意占位,如果格式化器上的是 mm :代表两位数的分钟,那么,字符串中就必须是两位数如果不满两位数就用0补齐,07,08等

不然就解析不成功

period

一段时期

可以计算两个LocalDate对象相差的年,月,日

在Java中,Period 类是 java.time 包的一部分,用于表示两个日期之间的时间跨度。这个类主要用于处理日期间隔,并且不包含时间信息。

//两个时间对象
LocalDate y1 = LocalDate.of(2002, 11, 4);
LocalDate y2 = LocalDate.of(2022, 12, 25);
//创建period对象,将两个时间对象放入
Period period = Period.between(y1, y2);
System.out.println(period.getYears());//年 20
System.out.println(period.getMonths());//月 1
System.out.println(period.getDays());//日 21

Duration

可以用于计算两个时间对象相差的天数,小时数,分数,秒数,纳秒数

支持laoclTime,LocalDateTime,Instant等

Java的 Duration 类是 java.time 包的一部分,用于表示一个时间段的长度,以秒和纳秒为单位。这个类主要用于处理时间间隔,并且可以用于执行各种时间相关的操作

//两个时间对象
LocalDateTime y1 = LocalDateTime.of(2002, 03, 05, 8, 2, 12);
LocalDateTime y2 = LocalDateTime.of(2002, 03, 05, 10, 14, 20);
//创建period对象,将两个时间对象放入
Duration duration = Duration.between(y1, y2);
System.out.println(duration.toDays()); //天 0
System.out.println(duration.toHours()); //小时 2
System.out.println(duration.toMinutes()); //分钟 132
System.out.println(duration.getSeconds());//秒 7298
System.out.println(duration.toNanos());//纳秒 7928000000000

JavaImprove--Lesson04--LocalDateTime,ZoneId,Instant,DateTimeFormatter的更多相关文章

  1. Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date等

    从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转D ...

  2. Java日期时间API系列21-----Jdk8中java.time包中的新的日期时间API类,xk-time时间转换,计算,格式化,解析的工具

    通过工作之余,对Java8中java.time包源码的不断学习,使用和总结,开发了xk-time,初步完成,欢迎试用和提出建议! xk-time xk-time is a datetime conve ...

  3. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  4. 纯CSS菜单样式,及其Shadow DOM,Json接口 实现

    先声明,要看懂这篇博客要求你具备少量基础CSS知识, 当然如果你只是要用的话就随便了,不用了解任何知识 完整项目github链接:https://github.com/git-Code-Shelf/M ...

  5. Java中,尽量相信自己,使用自己写的方法,不要使用底层提供的方法。都是坑。

    Date转LocalDate时,调用toInstant()报UnsupportedOperationException异常. https://www.jianshu.com/p/11d8ed48f7a ...

  6. 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结

    1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018             年月日时分秒    CST代表北 ...

  7. CDR X7正版优惠,3折来袭,好礼相送,行不行动?

    意料之中的是,CorelDRAW系列软件在618期间成绩再次突破历史,成为新高.因为X7版本活动在6月15号的才上, 加之在此之前从没有过X7的活动优惠,势头之猛,可想而知,如此一来,官方预定的限量2 ...

  8. 这个夏天有你,有CorelDRAW X7,有理想,有设计!

    CorelDRAW是加拿大Corel公司出品的一款功能全面的矢量绘图.平面设计软件,兼有图形设计的简易操作性和图像编辑的强大功能.目前,被广泛应用于广告宣传.艺术作品.纺织业等各个行业.和Photos ...

  9. 三年Android开发,月薪一万二,不敢跳槽,每天都很焦虑

    在我们的身边,存在一个普遍现象:很多人从事Android开发工作多年,走过的弯和坎,不计其数,经历的心酸难与外人道也.可是技术确难以提升.止步不前,薪资也只能看着别人水涨船高,自己却没有什么起色. 虽 ...

  10. SpringCloud(六) - RabbitMQ安装,三种消息发送模式,消息发送确认,消息消费确认(自动,手动)

    1.安装erlang语言环境 1.1 创建 erlang安装目录 mkdir erlang 1.2 上传解压压缩包 上传到: /root/ 解压缩# tar -zxvf otp_src_22.0.ta ...

随机推荐

  1. C#归并排序算法

    前言 归并排序是一种常见的排序算法,它采用分治法的思想,在排序过程中不断将待排序序列分割成更小的子序列,直到每个子序列中只剩下一个元素,然后将这些子序列两两合并并排序,最终得到一个有序的序列. 归并排 ...

  2. ansible-配置文件优化-性能调优

    ansible-配置文件详解:ansible默认配置文件为/etc/ansible/ansible.cfg,配置文件中可以对ansible进行各项参数的调整,包括并发线程.用户.模块路径.配置优化等, ...

  3. python 自动创建Hype-V虚拟机脚本

    安装模块 pip install pywinrm 脚本如下 #!/usr/bin/env python3 # coding=utf-8 # author:LJX # describe:一键创建hype ...

  4. Docker学习资料集(从入门到实践)

    前言 昨天分享了一篇介绍Docker可视化管理工具的文章,然后在公众号后台收到了挺多同学的私信问:学习Docker有好的资料值得推荐的吗?想要学习Docker但是无从下手.其实之前我有断断续续的分享过 ...

  5. NewsCenter

    打开界面有一个搜索框 抓包查看是post形式提交的数据包 这时候试试sql注入,万能密码直接全都显示,那就说明存在sql注入漏洞 这里试试用sqlmap自动注入试试(POST类型的sql注入第一次尝试 ...

  6. npm 发布包时 图片打包在新的项目引入不显示 路径错误解决方案

    使用的是vue-cli 4.0以上脚手架 vue2.6 封装好组件后 npm publish ,在其他项目引入该组件库 图片显示失败 打开F12时看到 组件库里图片是/img/图片名,可是该项目没有此 ...

  7. Excel 使用 VLOOKUP 函数匹配特定列

    前言 工作有一项内容,是根据新的表格的某一列的内容一对一匹配,生成一列新的表格.这就用到了 Excel 的 VLOOKUP 函数. 函数使用 函数体: =VLOOKUP(lookup_value,ta ...

  8. C/C++ 运用Npcap发送UDP数据包

    Npcap 是一个功能强大的开源网络抓包库,它是 WinPcap 的一个分支,并提供了一些增强和改进.特别适用于在 Windows 环境下进行网络流量捕获和分析.除了支持通常的网络抓包功能外,Npca ...

  9. windows server 2019 2012 server 2022 无线网卡驱动安装报错,无线网卡驱动不能安装, inf服务安装段落无效

    windows server 2019 无线网络 服务安装段落无效 windows server 2019 无线网卡驱动安装报错,无线网卡驱动不能安装, inf服务安装段落无效 indows serv ...

  10. 聊一聊 .NET高级调试 中必知的符号表

    一:背景 1. 讲故事 在高级调试的旅行中,发现有不少人对符号表不是很清楚,其实简而言之符号表中记录着一些程序的生物特征,比如哪个地址是函数(签名信息),哪个地址是全局变量,静态变量,行号是多少,数据 ...