一.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. MySQL系列之优化——1.优化哲学、2. 优化工具的使用、3. 优化思路分解、4. MySQL参数优化测试、5.1 参数优化、6. 参数优化结果、7. 锁的监控及处理、8. 主从优化

    文章目录 1.优化哲学 1.1 为什么优化? 1.2 优化风险 1.3 谁参与优化 1.4 优化方向 1.5 优化的范围及思路 优化效果和成本的评估: 2. 优化工具的使用 2.1 系统层面的 2.1 ...

  2. Go 复合类型之字典类型介绍

    Go 复合类型之字典类型介绍 目录 Go 复合类型之字典类型介绍 一.map类型介绍 1.1 什么是 map 类型? 1.2 map 类型特性 二.map 变量的声明和初始化 2.1 方法一:使用 m ...

  3. Python 数学函数和 math 模块指南

    Python 提供了一组内置的数学函数,包括一个广泛的数学模块,可以让您对数字执行数学任务. 内置数学函数.min() 和 max() 函数可用于在可迭代对象中查找最低或最高值: 示例:查找可迭代对象 ...

  4. Xmind思维导图工具2023最新专业版破解思路

    工具介绍 XMind 是一款最为流行的专业级思维_导图_制作与编辑软件,它现在在全球范围内都已极具名气,可谓是办公.学习.团队交流必备工具之一. 准备工作 1,官方Xmind软件 2,一个心意的编辑器 ...

  5. Hyper-V中的虚拟机(Centos)安装FTP服务

    linux上是否装上了ftp服务命令: rpm -qa | grep vsftpd ,若没有安装(无显示版本号)则进行下一步 安装ftp服务,命令: yum -y install ftp vsftpd ...

  6. 【PySide6】QChart笔记(一)—— 用QDateTimeAxis作为x轴绘制多条折线图

    一.QDateTimeAxis简介 1. 官方描述 https://doc.qt.io/qtforpython-6/PySide6/QtCharts/QDateTimeAxis.html QDateT ...

  7. 最小的k个数 (3.20 leetcode每日打卡)

    输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 示例 1: 输入:arr = [3,2,1], k = 2 输 ...

  8. WPF --- 如何重写WPF原生控件样式

    引言 上一篇中 WPF --- 重写DataGrid样式,因新产品UI需要,重写了一下微软 WPF 原生的 DataGrid 的样式,包含如下内容: 基础设置,一些基本背景色,字体颜色等. 滚动条样式 ...

  9. Cadence SPB 22.1 -- 原理图的电器元件放置03Day

    1.新增原理图:"SCHEMATIC"-->"New Page" 2.元器件放置 ①.添加原理图库 ②.放置元器件 选择对应元件库,再选择需要放置的元件, ...

  10. 一步解决Cannot resolve symbol 'WebServlet'(@WebServlet注解标红)

    右键项目名(javaweb01)->Open Module Settings Modules->Dependencies->点击Export上面的,添加Tomcat即可