Java日期和时间(二)

新增的日期和时间

为什么要学习新增的日期和时间

1、代替Calendar

        LocalDate:年、月、日

        LocalTime:时、分、秒

        LocalDateTime:年、月、日、时、分、秒

        ZoneId:时区

        ZoneldDatetime:带时区的时间

2、代替Date 

        Instant:时间戳/时间线

 3、代替SimpleDateForamt

        DateTimeFormatter:用于时间的格式化和解析

4、其他补充 

        Period:时间间隔(年、月、日)

        Duration:时间间隔(时、分、秒,纳秒)

LocalDate、LocalTime、LocalDateTime

LocalDate:代表本地日期(年、月、日、星期)

LocalTime:代表本地时间(时、分、秒、纳秒)

LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)

它们获取对象的方案

方法名示例
public static Xxxx now():获取系统当前时间对应的该对象

LocalDate ld = LocaDate.now();

LocalTime ld = LocalTime.now();

LocalDateTime ld = LocalDateTime.now();

public static Xxxx of(…):获取指定时间的对象

LocalDate localDate1 = LocalDate.of(2099,11,11)

LocalTime localTime1 = LocalTime.of(9,8,59)

LocalDateTime localDateTime1 = LocalDateTime.of(2025,11,16,14,30,01)

LocalDate 

LocalDate的常用API(都是处理年、月、日、星期相关的)
import java.time.LocalDate;

public class Test {
    public static void main(String[] args){
        // 获取本地日期对象(不可变对象)
        LocalDate ld = LocalDate.now(); // 年 月 日
        System.out.println(ld);

        // 获取日期对象中的信息
        int year = ld.getYear(); // 年
        int month = ld.getMonthValue(); // 月(1-12)
        int day = ld.getDayOfMonth(); // 日
        int dayOfYear = ld.getDayOfYear(); // 一年中的第几天
        int dayOfWeek = ld.getDayOfWeek().getValue(); // 星期几

        // 直接修改某个信息:withYear、withMonth、withDayOfMonth、withDayOfYear
        LocalDate ld2 = ld.withYear(2099);
        LocalDate ld3 = ld.withMonth(12);
        System.out.println(ld2);
        System.out.println(ld3);
        System.out.println(ld);

        // 把某个信息加多少:plusYears、plusMonths、plusDays、plusWeeks
        LocalDate ld4 = ld.plusYears(2);
        LocalDate ld5 = ld.plusMonths(2);

        // 把某个信息减多少:minusYears、minusMonths、minusDays、minusWeeks
        LocalDate ld6 = ld.minusYears(2);
        LocalDate ld7 = ld.minusMonths(2);

        // 获取指定日期的LocalDate对象
        LocalDate ld8 = LocalDate.of(2099,12,12);
        LocalDate ld9 = LocalDate.of(2099,12,12);

        // 判断2个日期对象是否相等,再前还是再后:equals isBefore isAfter
        System.out.println(ld8.equals(ld9));
        System.out.println(ld8.isAfter(ld));
        System.out.println(ld8.isBefore(ld));
    }
}

LocalTime

LocalTime的常用API(都是处理时、分、秒、纳秒相关的)
import java.time.LocalTime;

public class Test {
    public static void main(String[] args){
        // 获取本地时间对象
        LocalTime lt = LocalTime.now(); // 时 分 秒
        System.out.println(lt);

        // 获取时间中的信息
        int hour = lt.getHour(); // 时
        int minute = lt.getMinute(); // 分
        int second = lt.getSecond(); // 秒
        int nano = lt.getNano(); // 纳秒

        // 修改时间
        LocalTime lt2 = lt.withHour(10);
        LocalTime lt3 = lt.withMinute(10);
        LocalTime lt4 = lt.withSecond(10);
        LocalTime lt5 = lt.withNano(10);

        // 加多少:plusHours、plusMinutes、plusSeconds、plusNanos
        LocalTime lt6 = lt.plusHours(10);
        LocalTime lt7 = lt.plusMinutes(10);

        // 减多少:minusHours、minusMinutes、minusSeconds、minusNanos
        LocalTime lt8 = lt.minusHours(10);
        LocalTime lt9 = lt.minusMinutes(10);

        // 获取指定时间的LocalTime对象
        LocalTime lt10 = LocalTime.of(12,12,12);
        LocalTime lt11 = LocalTime.of(12,12,12);

        // 判断2个日期对象是否相等,再前还是再后:equals isBefore isAfter
        System.out.println(lt10.equals(lt11));
        System.out.println(lt10.isAfter(lt));
        System.out.println(lt10.isBefore(lt));
    }
}

LocalDateTime

LocalTime的常用API(都是处理年、月、日、时、分、秒、纳秒相关的)
import java.time.LocalDateTime;

public class Test {
    public static void main(String[] args){
        // 获取本地日期、时间对象
        LocalDateTime ldt = LocalDateTime.now(); // 年 月 日 时 分 秒
        System.out.println(ldt);

        // 获取日期、时间中的信息
        int year = ldt.getYear(); // 年
        int month = ldt.getMonthValue(); // 月(1-12)
        int day = ldt.getDayOfMonth(); // 日
        int dayOfYear = ldt.getDayOfYear(); // 一年中的第几天
        int dayOfWeek = ldt.getDayOfWeek().getValue(); // 星期几
        int hour = ldt.getHour(); // 时
        int minute = ldt.getMinute(); // 分
        int second = ldt.getSecond(); // 秒
        int nano = ldt.getNano(); // 纳秒

        // 修改时间
        LocalDateTime ldt2 = ldt.withYear(2099);
        LocalDateTime ldt3 = ldt.withMinute(59);

        // 加多少
        LocalDateTime ldt4 = ldt.plusYears(2);
        LocalDateTime ldt5 = ldt.plusMinutes(3);

        // 减多少
        LocalDateTime ldt6 = ldt.minusYears(2);
        LocalDateTime ldt7 = ldt.minusMinutes(3);

        // 获取指定日期、时间的LocalDateTime对象
        LocalDateTime ldt8 = LocalDateTime.of(2099,12,12,12,12,12);
        LocalDateTime ldt9 = LocalDateTime.of(2099,12,12,12,12,12);

        // 判断2个日期、时间对象是否相等,再前还是再后:equals isBefore isAfter
        System.out.println(ldt8.equals(ldt9));
        System.out.println(ldt8.isAfter(ldt));
        System.out.println(ldt8.isBefore(ldt));
    }
}

Zoneld、ZonedDateTime

Zoneld:代表时区id

Zoneld时区的常见方法

ZonedDateTime 

ZonedDateTime带时区时间的常见方法

import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Test {
    public static void main(String[] args){
        // Zoneld的常见方法
        // 获取系统默认的时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId.getId());
        System.out.println(zoneId);

        // 获取Java支持的全部时区id
        System.out.println(ZoneId.getAvailableZoneIds());

        // 把某个时区id封装成Zoneid对象
        ZoneId zoneId1 = ZoneId.of("America/New_York");

        // ZonedDateTime:带时区的时间
        // 获取系统某个时区的ZondDateTime对象
        ZonedDateTime now = ZonedDateTime.now(zoneId1);
        System.out.println(now);

        // 世界标准时间
        ZonedDateTime now1 = ZonedDateTime.now(Clock.systemUTC());
        System.out.println(now1);

        // 获取系统默认时区的ZondDateTime对象
        ZonedDateTime now2 = ZonedDateTime.now();
        System.out.println(now2);
    }
}

 

Instant  时间线上的某个时刻/时间戳

  • 通过获取Instant的对象可以拿到此刻的时间,该时间由两部分组成:从1970-01-01  00:00:00开始走到此刻的总秒数 + 不够一秒的纳秒数
import java.time.Instant;

public class Test {
    public static void main(String[] args){
        // 创建Instant对象,获取此刻时间信息
        Instant now = Instant.now();

        // 获取总秒数
        long second = now.getEpochSecond();
        System.out.println(second);

        // 不够一秒的纳秒数
        int nano = now.getNano();
        System.out.println(nano);

        // 增加、减少时间
        Instant instant1 = now.plusNanos(111);
        Instant instant2 = now.minusMillis(2);

        System.out.println(now);

        // Instant对象的作用:做代码的性能分析,或者记录用户的操作时间点
        Instant now1 = Instant.now();
        // 代码行……
        Instant now2 = Instant.now();
    }
}
  • 作用:可以用来记录代码的执行时间,或者记录用户的操作某个时间时间点。
  • 传统的Date类,只能精确到毫秒,并且是可变对象
  • 新增的Instant类,可以精确到纳秒,并且是不可变对象,推荐用Instant代替Date。

DateTimeFormatter

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        // 创建一个日期时间格式化器对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

        // 对时间进行格式化
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        String rs = formatter.format(now);
        System.out.println(rs);

        // 格式化时间的另一种方案
        String rs2 = now.format(formatter);
        System.out.println(rs2);

        // 解析时间
        String dateStr = "2029年12月12日 12:12:12";
        LocalDateTime ldt = LocalDateTime.parse(dateStr,formatter);
        System.out.println(ldt);
    }
}

 

Period、Duration

Period(一段时期)

  • 可以用于计算两个LocalDate对象相差的年数、月数、天数。
import java.time.LocalDate;
import java.time.Period;

public class Test {
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2029,8,10);
        LocalDate end = LocalDate.of(2029,12,15);

        // 创建Period对象,封装两个日期对象
        Period period = Period.between(start,end);

        // 通过Period对象获取两个日期对象相差的信息
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }
}

 

Duration(持续时间)

  • 可以用于计算两个时间对象相差的天数、小数数、分数、秒数、纳秒数;支持LocalTime、LocalDateTime、Instant等时间。
import java.time.Duration;
import java.time.LocalDateTime;

public class Test {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2029,11,11,11,10,10);
        LocalDateTime end = LocalDateTime.of(2029,11,11,11,11,11);

        // 创建Duration对象,封装两个时间对象
        Duration duration = Duration.between(start,end);

        // 通过Duration对象获取两个时间对象相差的信息
        System.out.println(duration.toDays());
        System.out.println(duration.toHours());
        System.out.println(duration.toMinutes());
        System.out.println(duration.toMillis());
        System.out.println(duration.toNanos());
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/284111.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

使用flutter开发一个简单的轮播图带指示器的组件

使用PageView开发一个带指示器的轮播图组件,当轮播图切换的时候,指示器也会跟着切换,切换到当前轮播图所在的索引时,指示器的背景色会变成蓝色,否则是灰色。使用了一个curIndex变量来记录当前激活的轮播图索引。并使用…

HarmonyOS资源分类与访问

资源分类与访问 应用开发过程中,经常需要用到颜色、字体、间距、图片等资源,在不同的设备或配置中,这些资源的值可能不同。 应用资源:借助资源文件能力,开发者在应用中自定义资源,自行管理这些资源在不同…

【INTEL(ALTERA)】如何使用quartus设计助理Design Assistant提高结果质量,很好的资料一定要分享!!!

大家在用quartus的时候一定遇到过超级多的警告 warning,甚至异常 error,还有无从下手的timing 。 多扇出,布线拥堵,时序违例是不是让你头疼不已?那你一定要看看这篇文章分享的文档和资料。 优化设计的源代码通常是提高…

CMake入门教程【基础篇】什么是CMakeLists.txt

文章目录 什么是CMakeLists.txtCMakeLists.txt的核心作用CMakeLists.txt的基本结构总结 什么是CMakeLists.txt CMakeLists.txt是一个由CMake(一个跨平台的自动化构建系统)使用的配置文件。这个文件用于定义软件构建的过程,包括编译源代码、链…

wait 和 notify 这个为什么要在synchronized 代码块中

文章目录 wait 和 notify 这个为什么要在synchronized 代码块中? wait 和 notify 这个为什么要在synchronized 代码块中? wait 和 notify 用来实现多线程之间的协调,wait 表示让线程进入到阻塞状态,notify 表示让阻塞的线程唤醒。…

Vue3+Echarts(柱状图):点击不同的按钮可以切换不同年份的数据

一、需求 在Vue3项目中,绘制一个柱状图: 柱状图会展示某一年里四个季度的销售额提供2个按钮选项,点击不同的按钮可以切换到不同年份的销售额,这里的年份指2022年以及2023年目标效果如下: 默认展示的是2023年的数据&a…

spring 之 事务

1、JdbcTemplate Spring 框架对 JDBC 进行封装&#xff0c;使用 JdbcTemplate 方便实现对数据库操作 1.1 准备工作 ①搭建子模块 搭建子模块&#xff1a;spring-jdbc-tx ②加入依赖 <dependencies><!--spring jdbc Spring 持久化层支持jar包--><dependency&…

病情聊天机器人,利用Neo4j图数据库和Elasticsearch全文搜索引擎相结合

项目设计目的&#xff1a; 本项目旨在开发一个病情聊天机器人&#xff0c;利用Neo4j图数据库和Elasticsearch全文搜索引擎相结合&#xff0c;实现对病情相关数据的存储、查询和自动回答。通过与用户的交互&#xff0c;机器人可以根据用户提供的症状描述&#xff0c;给出初步的可…

论虚继承的作用

虚继承 实验介绍 在上一小节中学习了多继承与多重继承,实际在开发的时候可能会遇到一种情况,既用到了多继承又用到了多重继承,这种继承方式通常又称为菱形继承。但这样一来就会产生新的问题,过多消耗空间。希望通过本小节学习能知道菱形继承以及产生的问题和解决方式。 …

【网络面试(3)】浏览器委托协议栈完成消息的收发

前面的博客中&#xff0c;提到过很多次&#xff0c;浏览器作为应用程序&#xff0c;本身是不具备向网络中发送网络请求的能力&#xff0c;要委托操作系统的内核协议栈来完成。协议栈再调用网卡驱动&#xff0c;通过网卡将请求消息发送出去&#xff0c;本篇博客就来探讨一下这个…

给零基础朋友的编程课09 上集 - 代码

给零基础朋友的编程课09 上 - 矩形、曲线、文字、案例5讲解 上_哔哩哔哩_bilibili 上半Code: / // 彩色案例 艺术仿制品4 // /// 色表 // // 238,150,43 橙 // 229,207,192 暖灰 // 204,50,47 暗红// 项目设定 size(825, 984); // 设置画布(窗口)尺寸 background(…

计算机网络——基础知识汇总(八)

前言&#xff1a; 前面我们已经将计算机网络的基础知识和基础框架有了一个简单的学习与了解&#xff0c;也对它可能考的一些计算机网络计算大题有了一个详细的解答与记录&#xff0c;现在我们将计算机网络中的一些基础知识点进行一个总结与记录&#xff0c;这些基础知识大多会出…

C#编程-编写和执行C#程序

编写和执行C#程序 可以使用Windows记事本应用程序来编写C#程序。在记事本应用程序中创建C#程序后,您需要编译并执行该程序以获得所需的输出。编译器将程序的源代码转换为机器代码,这样计算机就能理解程序中的指令了。 注释 除了记事本,您还可以使用任何其他文本编辑器来编写…

托管在亚马逊云科技的向量数据库MyScale如何借助AWS基础设施构建稳定高效的云数据库

MyScale是一款完全托管于亚马逊云科技&#xff0c;支持SQL的高效向量数据库。MyScale的优势在于&#xff0c;它在提供与专用向量数据库相匹敌甚至优于的性能的同时&#xff0c;还支持完整的SQL语法。以下内容&#xff0c;将阐述MyScale是如何借助亚马逊云科技的基础设施&#x…

ubuntu 20.04 自由切换 python 的版本

问题描述 当前 ubuntu 20.04 默认安装了多个 python 的版本&#xff0c;执行 python 时&#xff0c;默认版本是 Python 2.7.18 zhangszzhangsz:~$ python Python 2.7.18 (default, Jul 1 2022, 12:27:04) [GCC 9.4.0] on linux2 Type "help", "copyright&quo…

AI时代系列丛书(由北京大学出版社出版)

前言 在AI时代&#xff0c;程序员面临着新的机遇和挑战。为了适应这个快速发展的时代&#xff0c;掌握新技能并采取相应的应对策略是至关重要的。 对于办公人员或程序员来说&#xff0c;利用AI可以提高工作效率。例如&#xff0c;使用AI助手可以帮助自动化日常的重复性工作&a…

【flink番外篇】9、Flink Table API 支持的操作示例(8)- 时态表的join(scala版本)

Flink 系列文章 一、Flink 专栏 Flink 专栏系统介绍某一知识点&#xff0c;并辅以具体的示例进行说明。 1、Flink 部署系列 本部分介绍Flink的部署、配置相关基础内容。 2、Flink基础系列 本部分介绍Flink 的基础部分&#xff0c;比如术语、架构、编程模型、编程指南、基本的…

Spring Cloud Function SpEL注入漏洞(CVE-2022-22963)分析

一、概述 2022年3月24日&#xff0c;Pivotal修补了Spring Cloud Function中一个关键的服务器端代码注入漏洞&#xff08;Spring表达式语言注入&#xff09;&#xff0c;该漏洞有可能导致系统被攻击。Spring是一种流行的开源Java框架&#xff0c;该漏洞与另一个相关的远程代码执…

[区间动态规划] 棋盘分割

题目描述 ​ 将一个&#xff18;*&#xff18;的棋盘进行如下分割&#xff1a;将原棋盘割下一块矩形棋盘并使剩下部分也是矩形&#xff0c;再将剩下的部分继续如此分割&#xff0c;这样割了(n−1)次后&#xff0c;连同最后剩下的矩形棋盘共有 n 块矩形棋盘。(每次切割都只能沿…

【SpringBoot】SwaggerKnif4j接口文档集成

[TOC] 序&#xff1a;接口文档 ​ 在开发过程中&#xff0c;接口文档是非常重要的一环&#xff0c;在 Spring Boot 中&#xff0c;我们可以通过集成第三方来实现接口文档的自动生成。 ​ 通过注解来描述接口&#xff0c;然后根据这些注解自动生成接口文档&#xff0c;它不仅…
最新文章