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包中的类如何使用的更多相关文章

  1. Java遍历包中所有类

    PackageUtil 类 import java.io.File; import java.net.URL; import java.net.URLClassLoader; import java. ...

  2. 关于java同步包中ConcurrentLinkedQueue类的深入分析与理解

    一,官方描写叙述 一个基于连接节点的无界线程安全队列.这个队列的顺序是先进先出.队列头部的元素是留在队列中时间最长的,队列尾部的元素是留在队列中时间最短的.新元素被插入到元素的尾部,队列从队列的头部检 ...

  3. 27 Java动态加载第三方jar包中的类

    我加载的方法是://参数fileName是jar包的路径,processorName 是业务类的包名+类名public static A load(String fileName, String pr ...

  4. 集合框架的类和接口均在java.util包中。 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换。

    集合框架的类和接口均在java.util包中. 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换.

  5. Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类

    因为Jdk7及以前的日期时间类的不方便使用问题和线程安全问题等问题,2005年,Stephen Colebourne创建了Joda-Time库,作为替代的日期和时间API.Stephen向JCP提交了 ...

  6. Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date等

    从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转D ...

  7. Java日期时间API系列19-----Jdk8中java.time包中的新的日期时间API类,ZonedDateTime与ZoneId和LocalDateTime的关系,ZonedDateTime格式化和时区转换等。

    通过Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类中时间范围示意图:可以很清晰的看出ZonedDateTime相当于LocalDateTime+ZoneI ...

  8. Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析

    目录 0.前言 1.TemporalAccessor源码 2.Temporal源码 3.TemporalAdjuster源码 4.ChronoLocalDate源码 5.LocalDate源码 6.总 ...

  9. Java日期时间API系列11-----Jdk8中java.time包中的新的日期时间API类,使用java8日期时间API重写农历LunarDate

    通过Java日期时间API系列7-----Jdk8中java.time包中的新的日期时间API类的优点,java8具有很多优点,现在网上查到的农历转换工具类都是基于jdk7及以前的类写的,下面使用ja ...

  10. Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全

    通过Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter, 可以看出java8的DateTimeFormatter完美解决 ...

随机推荐

  1. 【接口自动化测试】Eolink Apilkit 安装部署,支持 Windows、Mac、Linux 等系统

    Eolink Apikit 有三种客户端,可以依据自己的情况选择.三种客户端的数据是共用的,因此可以随时切换不同的客户端. 我们推荐使用新推出的 Apikit PC 客户端,PC 端拥有线上产品所有的 ...

  2. Record - Dec. 1st, 2020 - Exam. REC

    Prob. 1 Desc. & Link. 行走的形式是比较自由的,因为只要走到了最优答案处就可以不管了,所以不需要考虑游戏的结束. 考虑二分答案. 然后预处理出每个节点到 \(s\)(另一棵 ...

  3. asp.net mvc Core 网页错误提示:An unhandled exception occurred while processing the request.处理请求时发生未处理的异常。

    网页错误提示: An unhandled exception occurred while processing the request. InvalidOperationException: The ...

  4. Go 语言的前生今世与介绍

    Go 语言的前生今世与介绍 目录 Go 语言的前生今世与介绍 一. Go 语言的发展 1.1 Go 语言是如何诞生的? 1.2 Go语言的早期团队和演进历程 1.3 Go语言正式发布并开源 1.4 G ...

  5. Oracle存储函数写法及调用

    1.右键导航创建函数界面 2.返回值FunctionResult可自定义,当函数有输出函数时可不传数据,但需要设置返回(当提示未限定返回长度时,如代码示例设置255长度皆可): -------使用函数 ...

  6. MySQL误删恢复方法1

    MySQL不同于oracle,没有闪回查询这类概念,但网上流传几个闪回的开源工具如 binglog2sql.MyFlash,可以使用binglog日志进行误操作数据的恢复. 笔者以前测试过 bingl ...

  7. Jmeter中使用BeanShell获取接口返回的指定值

    第一步:先引入jar包编写代码的时候,引入了一个jar包,是需要把它添加在测试计划中的  第一种:获取data中的paramName和paramVal值 //获取当前请求响应结果 String res ...

  8. node(1)

    1.新建http.js //node搭建http服务器 let http=require('http'); //使用http建立服务请求 http.createServer(function(requ ...

  9. Dubbo 路由及负载均衡性能优化

    作者:vivo 互联网中间件团队- Wang Xiaochuang 本文主要介绍在vivo内部针对Dubbo路由模块及负载均衡的一些优化手段,主要是异步化+缓存,可减少在RPC调用过程中路由及负载均衡 ...

  10. Kubernetes 迁移节点 Kubelet 数据存储目录

    1.概述 默认Kubernetes节点Kubelet数据目录在/var/lib/kubelet,如果在部署前没有做好规划,其实默认就存储在系统盘/分区下了,这可能会引发一些问题: 磁盘空间限制: 系统 ...