java.time包中的类如何使用
java.time包是在java8中引入的日期和时间处理API,提供了一组全新的类,用于更灵活、更强大的处理日期和时间。
常用用法
1、localDate
表示日期,不包含时间和时区信息
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date: " + currentDate);
// 创建指定日期
LocalDate specificDate = LocalDate.of(2022, 1, 1);
System.out.println("Specific Date: " + specificDate);
// 执行日期操作
LocalDate futureDate = currentDate.plusMonths(3);
System.out.println("Future Date: " + futureDate);
}
}
2、loaclTime
表示时间,不包含日期和时区信息
import java.time.LocalTime;
public class LocalTimeExample {
public static void main(String[] args) {
// 获取当前时间
LocalTime currentTime = LocalTime.now();
System.out.println("Current Time: " + currentTime);
// 创建指定时间
LocalTime specificTime = LocalTime.of(12, 30, 45);
System.out.println("Specific Time: " + specificTime);
// 执行时间操作
LocalTime futureTime = currentTime.plusHours(2);
System.out.println("Future Time: " + futureTime);
}
}
3、LocalDateTime
表示日期和时间,不包含时区信息
import java.time.LocalDateTime;
public class LocalDateTimeExample {
public static void main(String[] args) {
// 获取当前日期和时间
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current Date and Time: " + currentDateTime);
// 创建指定日期和时间
LocalDateTime specificDateTime = LocalDateTime.of(2022, 1, 1, 12, 30);
System.out.println("Specific Date and Time: " + specificDateTime);
// 执行日期和时间操作
LocalDateTime futureDateTime = currentDateTime.plusDays(7).minusHours(3);
System.out.println("Future Date and Time: " + futureDateTime);
}
}
4、ZoneDateTime
表示带有时区的日期和时间
import java.time.ZonedDateTime;
import java.time.ZoneId; public class ZonedDateTimeExample {
public static void main(String[] args) {
// 获取带有时区信息的当前日期和时间
ZonedDateTime currentZonedDateTime = ZonedDateTime.now();
System.out.println("Current Date and Time with Zone: " + currentZonedDateTime); // 创建指定时区的日期和时间
ZonedDateTime specificZonedDateTime = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("America/New_York"));
System.out.println("Specific Date and Time with Zone: " + specificZonedDateTime);
}
}
java.time包中的类如何使用的更多相关文章
- Java遍历包中所有类
PackageUtil 类 import java.io.File; import java.net.URL; import java.net.URLClassLoader; import java. ...
- 关于java同步包中ConcurrentLinkedQueue类的深入分析与理解
一,官方描写叙述 一个基于连接节点的无界线程安全队列.这个队列的顺序是先进先出.队列头部的元素是留在队列中时间最长的,队列尾部的元素是留在队列中时间最短的.新元素被插入到元素的尾部,队列从队列的头部检 ...
- 27 Java动态加载第三方jar包中的类
我加载的方法是://参数fileName是jar包的路径,processorName 是业务类的包名+类名public static A load(String fileName, String pr ...
- 集合框架的类和接口均在java.util包中。 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换。
集合框架的类和接口均在java.util包中. 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换.
- Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类
因为Jdk7及以前的日期时间类的不方便使用问题和线程安全问题等问题,2005年,Stephen Colebourne创建了Joda-Time库,作为替代的日期和时间API.Stephen向JCP提交了 ...
- Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date等
从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转D ...
- Java日期时间API系列19-----Jdk8中java.time包中的新的日期时间API类,ZonedDateTime与ZoneId和LocalDateTime的关系,ZonedDateTime格式化和时区转换等。
通过Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类中时间范围示意图:可以很清晰的看出ZonedDateTime相当于LocalDateTime+ZoneI ...
- Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析
目录 0.前言 1.TemporalAccessor源码 2.Temporal源码 3.TemporalAdjuster源码 4.ChronoLocalDate源码 5.LocalDate源码 6.总 ...
- Java日期时间API系列11-----Jdk8中java.time包中的新的日期时间API类,使用java8日期时间API重写农历LunarDate
通过Java日期时间API系列7-----Jdk8中java.time包中的新的日期时间API类的优点,java8具有很多优点,现在网上查到的农历转换工具类都是基于jdk7及以前的类写的,下面使用ja ...
- Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全
通过Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter, 可以看出java8的DateTimeFormatter完美解决 ...
随机推荐
- Visual Studio 2022 设置代码补全
Visual Studio 2022 设置代码补全 VS默认使用 Tab 键进行代码补全. 若要使用回车补全需要重新设置,具体路径如下: 工具----选项----文本编辑器----C/C++--- ...
- 20个最佳实践提升Terraform工作流程|Part 2
在上一部分,我们一同探讨了构建 Terraform 项目的一些策略,以及使用 Terraform 管理 IaC 的部分最佳实践.今天,我们将继续深入研究将 Terraform 代码提升到新水平的具体要 ...
- DB2复制表结构及数据
在DB2数据库中,复制已经存在的表的结构及其数据.我们采用两步走方式:第一步先复制表结构,第二部拷贝数据. 第一步:复制表结构 方法一: Create table test_Rate as (sele ...
- DICOM PS3.7 2021a - Message Exchange
PS3.7 DICOM PS3.7 2021a - Message Exchange DICOM Standards Committee Copyright 2021 NEMA A DICOM pub ...
- Centos7安装Promethus
安装Prometheus(普罗米修斯)监控: 实验环境准备: 服务器 IP地址 Prometheus服务器 192.168.1.22 被监控服务器 192.168.1.20 grafana服务器 19 ...
- 通过mybatis-plus的自定义拦截器实现控制 mybatis-plus的全局逻辑删除字段的控制 (修改其最终执行的sql中的where条件)
需求:过滤部分请求不实现mybatis-plus的逻辑删除 看到网上关于mybatis-plus的自定义拦截器的文章有的少 想了想自己写了一篇 欢迎参考 指正 通过springboot的拦截器 在请求 ...
- k8s-服务网格实战-配置 Mesh(灰度发布)
在上一篇 k8s-服务网格实战-入门Istio中分享了如何安装部署 Istio,同时可以利用 Istio 实现 gRPC 的负载均衡. 今天我们更进一步,深入了解使用 Istio 的功能. 从 Ist ...
- 02Java学习_注意事项和学习方法
02_Java 开发注意事项细节和学习方法 目录 02_Java 开发注意事项细节和学习方法 注意事项 学习方法 注意事项 .java 是 Java 文件的拓展名.源文件的基本组成部分是类--clas ...
- Python学习 —— 内置数据类型
写在前面 接上文<Python学习 -- 初步认知>,有需要请自取:Python学习 -- 初步认知 在这篇文章中,我们一起深入了解Python中常用的内置数据类型.Python是一种功能 ...
- 高效使用 PyMongo 进行 MongoDB 查询和插入操作
插入到集合中: 要将记录(在MongoDB中称为文档)插入到集合中,使用insert_one()方法.insert_one()方法的第一个参数是一个包含文档中每个字段的名称和值的字典. import ...