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完美解决 ...
随机推荐
- Solution -「洛谷 P2044」「NOI 2012」随机数生成器
Description Link. 给你一个递推式,让你求某一项的值模上 \(g\). Solution 这道题正解是矩阵.我这里给出一种分治的做法. 题目中说 $\ \ \ \ \ \ \ $ $\ ...
- 《流畅的Python》 读书笔记 230926
写在最前面的话 缘由 关于Python的资料市面上非常多,好的其实并不太多. 个人认为,基础的,下面的都还算可以 B站小甲鱼 黑马的视频 刘江的博客 廖雪峰的Python课程 进阶的更少,<流畅 ...
- MySQL系列之——MySQL体系结构、基础管理(用户、权限管理、连接管理、多种启动方式介绍、初始化配置、多实例的应用)
文章目录 一 体系结构 1.1 C/S(客户端/服务端)模型介绍 1.2 实例介绍 1.3 mysqld程序运行原理 1.3.1 mysqld程序结构 1.3.2 一条SQL语句的执行过程 1.3.2 ...
- 单元测验4:人格知识大比武2mooc
单元测验4:人格知识大比武2 返回 本次得分为:10.00/10.00, 本次测试的提交时间为:2020-09-06, 如果你认为本次测试成绩不理想,你可以选择 再做一次 . 1 单选(2分) 关于M ...
- math库常用函数+产生随机数总结
math库常用函数+产生随机数总结 1.对x开平方 double sqrt(x)://返回值为double类型,输入的x类型随意,只要是数的类型 2.求常数e的x次方 double exp(x);// ...
- C#怎样链接mysql数据库
C#一般链接sqlserver数据库,当然也会链接oracle.C#和MYSQL搭配貌似不多见哦 下面说说方法. 1.下载链接库文件,MySql.Data.dll MySql.Data.rar 2.工 ...
- IPv4:根据CIDR显示地址范围
最近遇到一个很有意思的点,于是就记录下来. CIDR一般是由IP地址和子网掩码组成,即 IP地址/子网掩码 格式. 子网掩码表示前面地址中的前多少位,为网络位,后面部分代表主机部分.例如:192.16 ...
- YCSB对MongoDB数据库性能测试
一.安装部署 1.1前置条件 Install Java and Maven Go to http://www.oracle.com/technetwork/java/javase/downloads/ ...
- OpenJDK里的AsmTools简介
前言 转自:http://hengyunabc.github.io/openjdk-asmtools/ https://wiki.openjdk.java.net/display/CodeTools/ ...
- 高性能队列——Disruptor(转)
https://tech.meituan.com/disruptor.html 背景 Disruptor是英国外汇交易公司LMAX开发的一个高性能队列,研发的初衷是解决内存队列的延迟问题(在性能测试中 ...