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完美解决 ...
随机推荐
- 为什么 Python 代码在函数中运行得更快?
哈喽大家好,我是咸鱼 当谈到编程效率和性能优化时,Python 常常被调侃为"慢如蜗牛" 有趣的是,Python 代码在函数中运行往往比在全局范围内运行要快得多 小伙伴们可能会有这 ...
- windows上U盘格式化失败提示系统找不到指定文件
某天同事拿来几个U盘,问需不需要,我随便看了眼还挺新的,于是插上电脑看看能否正常使用,果然无法识别,因为没有使用需求了也就放着没管了. 突然有一天要去客户现场搞私有化交付了,自己带物料,这下就派上用场 ...
- 各种SQL连接符Join
一.连接符分类,内连接,外连接 1.内连接:Inner Join简写Join. 2.外连接:Left Outer Join 简写Left Join:Right Outer Join 简写Right J ...
- options has an unknown property ‘contentBase‘
options has an unknown property 'contentBase' 踩坑新版webpack-dev-serve 新版的contentBase取消了替代属性是static
- ABC322 A-F 题解
前言 为什么 ABC 天天出原题. 为什么 D 题这么答辩. A 直接找. 赛时代码 B 模拟. 赛时代码 C 对于每一个节日从后往前扫到上一个节日. 赛时代码 D 搜索,不需要任何剪枝,直接爆搜. ...
- 定时重启Nginx、MySql等服务
利用 Linux Crontab,每天定时重启 Nginx.MySQL等服务. 命令行格式说明 f1 f2 f3 f4 f5 program 其中 f1 是表示分钟,f2 表示小时,f3 表示一个月份 ...
- P7073 [CSP-J2020] 表达式
Problem 考察算法:后缀表达式建树,优化. 题目简述 读入一个后缀表达式,由 \(\&,\mid,!\) 三种运算和操作数构成. 有 \(q\) 次询问,每次输入一个下标 \(i\) , ...
- Safepoints: Meaning, Side Effects and Overheads(译文)
Safepoints: Meaning, Side Effects and Overheads (安全点:含义.副作用和开销) 去年,我一直在进行有关profiling以及JVM运行时/执行的一些讨论 ...
- 题解 UVA10299
前言 数学符号约定: \(a\).\(b\).\(m\).\(n\).\(x\).\(y\):任意一个正整数. \(p\):任意一个质数. \(d\):一个数的任意一个因子. \(\varphi(n) ...
- 【Windows 开发环境配置】NVIDIA 篇
CUDA 从CUDA Toolkit Archive下载相应版本的离线安装包,这里以11.7为例. 打开安装包,在安装选项选择自定义模式,点击下一步. 在自定义安装选项中,仅选择CUDA组件(其中Ns ...