JavaImprove--Lesson04--LocalDateTime,ZoneId,Instant,DateTimeFormatter
一.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的更多相关文章
- Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date等
从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转D ...
- Java日期时间API系列21-----Jdk8中java.time包中的新的日期时间API类,xk-time时间转换,计算,格式化,解析的工具
通过工作之余,对Java8中java.time包源码的不断学习,使用和总结,开发了xk-time,初步完成,欢迎试用和提出建议! xk-time xk-time is a datetime conve ...
- POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10575 Accepted: 5489 Descrip ...
- 纯CSS菜单样式,及其Shadow DOM,Json接口 实现
先声明,要看懂这篇博客要求你具备少量基础CSS知识, 当然如果你只是要用的话就随便了,不用了解任何知识 完整项目github链接:https://github.com/git-Code-Shelf/M ...
- Java中,尽量相信自己,使用自己写的方法,不要使用底层提供的方法。都是坑。
Date转LocalDate时,调用toInstant()报UnsupportedOperationException异常. https://www.jianshu.com/p/11d8ed48f7a ...
- 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结
1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018 年月日时分秒 CST代表北 ...
- CDR X7正版优惠,3折来袭,好礼相送,行不行动?
意料之中的是,CorelDRAW系列软件在618期间成绩再次突破历史,成为新高.因为X7版本活动在6月15号的才上, 加之在此之前从没有过X7的活动优惠,势头之猛,可想而知,如此一来,官方预定的限量2 ...
- 这个夏天有你,有CorelDRAW X7,有理想,有设计!
CorelDRAW是加拿大Corel公司出品的一款功能全面的矢量绘图.平面设计软件,兼有图形设计的简易操作性和图像编辑的强大功能.目前,被广泛应用于广告宣传.艺术作品.纺织业等各个行业.和Photos ...
- 三年Android开发,月薪一万二,不敢跳槽,每天都很焦虑
在我们的身边,存在一个普遍现象:很多人从事Android开发工作多年,走过的弯和坎,不计其数,经历的心酸难与外人道也.可是技术确难以提升.止步不前,薪资也只能看着别人水涨船高,自己却没有什么起色. 虽 ...
- SpringCloud(六) - RabbitMQ安装,三种消息发送模式,消息发送确认,消息消费确认(自动,手动)
1.安装erlang语言环境 1.1 创建 erlang安装目录 mkdir erlang 1.2 上传解压压缩包 上传到: /root/ 解压缩# tar -zxvf otp_src_22.0.ta ...
随机推荐
- 如何配置.h头文件include“”相对路径
编译工程时,找的是当前main.c文件下的.h文件,如果当前路径下没有就会报错,当前路径用.\表示,上一级目录用..\表示. 如果你的main.h文件在main.c的上一级目录中Include 文件夹 ...
- linux常见命令(四)
用于查看日期和时间的相关命令 cal date hwclock cal:显示日历信息 命令语音:cal [选项] [[[日]月]年] 选项 选项含义 -j 显示出给定月中的每一天是一年总的第几天(从1 ...
- 我试图扯掉这条 SQL 的底裤。
你好呀,我是歪歪. 这次带大家盘一个我觉得有点意思的东西,也是之前写<一个烂分页,踩了三个坑!>这篇文章时,遇到的一个神奇的现象,但是当时忙着做文章搞定这个主线任务,就没有去深究这个支线任 ...
- 谱图论:Laplacian算子及其谱性质
1 Laplacian 算子 给定无向图\(G=(V, E)\),我们在上一篇博客<谱图论:Laplacian二次型和Markov转移算子>中介绍了其对应的Laplacian二次型: \[ ...
- kubernetes发布周期
前言 页面介绍了版本发布的一些时间点和PR的要求,通过了解k8s的发布周期来规划自己的版本选择. 合并PR的要求 如果你希望将你的代码合并到官方代码仓库中,不同的开发阶段需要有不同的标签和里程碑.也是 ...
- 21.4 Python 使用GeoIP2地图定位
GeoIP2是一种IP地址定位库,它允许开发人员根据IP地址查找有关位置和地理位置的信息.它使用MaxMind公司的IP地址数据库,并提供一个方便的Python API.GeoIP2可以用于许多不同的 ...
- linux操作系统读写文件操作(c语言)
(一)open系统的调用 为了创建一个新的文件描述,需要使用系统调用open #include<fcntl.h> #include<sys/types.h> #include& ...
- 【pwn】ez_pz_hackover_2016 --pwngdb和pwntools的结合,动态调试
首先checksec 没开nx,说明堆栈可执行,初步考虑需要shellcode,然后拖进ida看主函数逻辑 看chall函数 printf("Yippie, lets crash: %p ...
- 解决Few-shot问题的两大方法:元学习与微调
.center { width: auto; display: table; margin-left: auto; margin-right: auto } 基于元学习(Meta-Learning)的 ...
- re2-cpp-is-awesome
没做出来,看题解得 攻防世界逆向高手题之re2-cpp-is-awesome_align 20h-CSDN博客 注意 汇编知识 align 8,align num是让后面的字节都对齐num,也就是这里 ...