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. numpy 中的nan和常用的统计方法

  2. 【译】ASP.NET Core在 .NET Core 3.1 Preview 1中的更新

    .NET Core 3.1 Preview 1现在可用.此版本主要侧重于错误修复,但同时也包含一些新功能. 这是此版本的ASP.NET Core的新增功能: 对Razor components的部分类 ...

  3. 探索抽象同步队列 AQS

    by emanjusaka from https://www.emanjusaka.top/archives/8 彼岸花开可奈何 本文欢迎分享与聚合,全文转载请留下原文地址. 前言 AbstractQ ...

  4. 《HelloGitHub》第 90 期

    兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. https://github.com/521xueweiha ...

  5. Ubuntu更新软件的命令

    更新软件源 apt-get update 更新升级所有软件 apt-get upgrade 更新某个软件 apt-get upgrade 名 列出可更新的软件 apt list --upgradabl ...

  6. isHex

    public class Test { public static boolean isHex(String str) { boolean isHexFlg = true; int i = 0; ch ...

  7. MySQL5.7版本单节点大数据量迁移到PXC8.0版本集群全记录-3

    接上文,单节点升级到80版本之后,构建新版本的80集群就水到渠成.相对简单了,详情可参见之前的集群构建博文. 本文在修改配置集群的新参数时,修改了pxc_strict_mode为默认的ENFORCIN ...

  8. 小景的Dba之路--Oracle用exp导出dmp文件很慢

    小景最近在系统压测相关的工作,其中涉及了Oracle数据库相关的知识,之前考的OCP证书也在此地起了作用.今天的问题是:Oracle用exp导出dmp文件很慢,究竟是什么原因,具体的解决方案都有哪些呢 ...

  9. 「Eolink Apikit 教程」API 异常监控-创建 API 监控

    API 监控能够确保 API 的稳定性.如果一个 API 出现故障或崩溃,它可能会导致整个应用程序无法正常工作.这对用户和业务来说可能是灾难性的.通过监控 API,开发团队可以及时发现问题并采取措施来 ...

  10. oceanbase 数据库SQL优化 (把你的脑袋当成CBO)

    OB一哥们找我优化条SQL,反馈在OceanBase存储过程执行时间很慢,需要626秒才能出结果,安排. INSERT INTO insurance_stat_sx (id, stat_date, c ...