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 ...
随机推荐
- PLSQL_developer安装与配置
前言: 记录安装与配置操作 环境: 客户机:windows 服务器:虚拟机中的windows server 2003 /---------------------------------------- ...
- git Failed to connect to 127.0.0.1 port xxxx: Connection refused 的问题。
问题描述在使用 git 拉取.提交代码的时候,会出现 git Failed to connect to 127.0.0.1 port xxxx: Connection refused 的问题. 原因: ...
- android 尺寸适配相关
Android上常见度量单位 px(像素):屏幕上的点,绝对长度,与硬件相关. in(英寸):长度单位. mm(毫米):长度单位. pt(磅):1/72英寸,point. dp(与密度无关的像素):一 ...
- 爽。。。一键导出 MySQL 表结构,告别手动梳理表结构文档了。。。
背景 系统需要交付,客户要求提供交维材料,包括系统的表结构,安排开发人员进行梳理,效率比较慢,遂自己花点时间捣鼓一下,发现有此插件,记录一下方便与同事分享 前提条件 必须有 go语言环境,有的话直接看 ...
- WSL2 中访问 Windows 的代理的最简易方案
前言 学校的网卡不允许运行虚拟机,所以必须将 WSL 的流量变成主机的流量,但从百度查的方案都是设置 Windows 主机的 IP,都忽视了 Windows 的默认功能,即 mdns 或 Window ...
- 4款.NET开源的Redis客户端驱动库
前言 今天给大家推荐4款.NET开源免费的Redis客户端驱动库(以下排名不分先后). Redis是什么? Redis全称是REmote DIctionary Service,即远程字典服务.Redi ...
- 低代码平台探讨-MetaStore元数据缓存
背景及需求 之前提到我们模型驱动的实现选择的是解释型,需要模型的元数据信息,在接到请求后动态处理逻辑. 此外,应用的通用能力中还包括:页面dsl查询,菜单查询等. 而且后期加入触发器,用户自定义api ...
- Intervals 题解
Intervals 题目大意 给定 \(m\) 条形如 \((l_i,r_i,a_i)\) 的规则,你需要求出一个长为 \(n\) 的分数最大的 01 串的分数,其中一个 01 串 \(A\) 的分数 ...
- React框架的基本运行原理与组件定义方式
React框架的基本运行原理 React的本质是内部维护了一套虚拟DOM树,这个虚拟DOM树就是一棵js对象树,它和真实DOM树是一致的,一一对应的. 当某一个组件的state发生修改时,就会生成一个 ...
- Jenkins软件平台安装部署
1.Jenkins软件平台概念剖解: 基于主流的Hudson/Jenkins平台工具实现全自动网站部署.网站测试.网站回滚会大大的减轻网站部署的成本,Jenkins的前身为Hudson,Hudson主 ...