当前位置: 首页 > news >正文

Java 获取和修改期日与时间的各种操作方法

LocalDateTime获取当地日期和时间

import java.time.LocalDateTime;
/*LocalDateTime.now() 获取当前时间*/
public class LocalDateTimeDemo {public static void main(String[] args) {LocalDateTime time1 = LocalDateTime.now();System.out.println(time1);//2024-06-01T13:20:41.336609500System.out.println(time1.getDayOfYear());System.out.println(time1.getDayOfMonth());System.out.println(time1.getHour());System.out.println(time1.getMinute());System.out.println(time1.getSecond());System.out.println(time1.getNano());//纳秒System.out.println(time1.getYear());System.out.println(time1.getMonthValue());System.out.println(time1.getDayOfWeek());//SATURDAY}
}

DateTimeFormatter对日期和时间自定义想要的格式

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;public class DateTimeFormatterDemo {public static void main(String[] args) {/*static DateTimeFormatter ofPattern(格式) 获取格式对象String format(时间对象) 按照指定方式格式化*/// 获取格式对象ZonedDateTime time = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));System.out.println(time);//2024-05-31T18:15:21.535714300+02:00[Europe/Berlin]// 自定义格式DateTimeFormatter form= DateTimeFormatter.ofPattern("yyyy年MM月dd日hh时mm分ss秒 星期e");System.out.println(form.format(time));//2024年05月31日06时20分54秒 星期5}
}

两个日期之间各种单位的差值

ChronoUnit获取两个日期和时间各种换算单位之间的总时差

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;public class ToolsChronoUnitDemo {public static void main(String[] args) {// 当前年月日LocalDateTime today = LocalDateTime.now();System.out.println(today);//2024-06-01T14:06:52.387788300// 出生年月日LocalDateTime BornDate = LocalDateTime.of(2004, 6, 14,0,0,0,0);System.out.println(BornDate);//2004-06-14T00:00System.out.println("相差的总年数:" + ChronoUnit.YEARS.between(BornDate,today));System.out.println("相差的总月数:" + ChronoUnit.MONTHS.between(BornDate,today));System.out.println("相差的总周数:" + ChronoUnit.WEEKS.between(BornDate,today));System.out.println("相差的总天数:" + ChronoUnit.DAYS.between(BornDate,today));System.out.println("相差的总时数:" + ChronoUnit.HOURS.between(BornDate,today));System.out.println("相差的总分数:" + ChronoUnit.MINUTES.between(BornDate,today));System.out.println("相差的总秒数:" + ChronoUnit.SECONDS.between(BornDate,today));System.out.println("相差的总毫秒数:" + ChronoUnit.MILLIS.between(BornDate,today));System.out.println("相差的总微秒数:" + ChronoUnit.MICROS.between(BornDate,today));System.out.println("相差的总纳秒数:" + ChronoUnit.NANOS.between(BornDate,today));System.out.println("相差的总半天数:" + ChronoUnit.HALF_DAYS.between(BornDate,today));System.out.println("相差的总十年数:" + ChronoUnit.DECADES.between(BornDate,today));System.out.println("相差的总世纪(百年)数:" + ChronoUnit.CENTURIES.between(BornDate,today));System.out.println("相差的总千年数:" + ChronoUnit.MILLENNIA.between(BornDate,today));System.out.println("相差的总纪元数:" + ChronoUnit.ERAS.between(BornDate,today));}
}

Period获取两个日期一年内的月份和天数之间的间隔

import java.time.LocalDate;
import java.time.Period;
public class ToolsDatePeriodDemo {public static void main(String[] args) {// 当前年月日LocalDate today = LocalDate.now();System.out.println(today);// 出生年月日LocalDate BornDate = LocalDate.of(2004, 6, 14);Period period = Period.between(BornDate, today);// 出生时间到今天的间隔时间System.out.println(period);//P19Y11M18DSystem.out.println(period.getYears());// 相差的年数System.out.println(period.getMonths());// 相差的月数System.out.println(period.getDays());// 相差的天数System.out.println(period.toTotalMonths());// 相差的总月数}
}

Duration获取两个日期和时间之间相差的总天数和各种单位的总时间数

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;public class ToolsDurationDemo {public static void main(String[] args) {// 本地日期时间对象。LocalDateTime today = LocalDateTime.now();System.out.println(today);// 出生的日期时间对象LocalDateTime BornDate = LocalDateTime.of(2004, 6, 14, 0, 0, 0);System.out.println(BornDate);Duration duration = Duration.between(BornDate, today);// 出生时间到今天的间隔时间System.out.println(duration);//PT175021H57M48.8411209SSystem.out.println(duration.toDays());// 相差的总天数System.out.println(duration.toHours());// 相差的总小时数System.out.println(duration.toMinutes());// 相差的总分钟数System.out.println(duration.toSeconds());// 相差的总秒数System.out.println(duration.toMillis());// 相差的总毫秒数System.out.println(duration.toNanos());// 相差总纳秒数}
}

获取时间和时区,对时间的值进行各种操作

Instant对时间进行增加,减少和差异判断

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class InstentDemo {public static void main(String[] args) {/*static Instant now()                    获取当前时间的Instant对象(标准时间)static Instant ofXxxx(long epochMilli)  根据(秒/毫秒/纳秒)获取Instant对象ZonedDateTime atZone(ZoneIdzone)        指定时区boolean isxxx(Instant otherInstant)     判断时间差异的方法Instant minusXxx(long millisToSubtract) 减少时间的方法,需要放到一个新对象中Instant plusXxx(long millisToSubtract)  增加时间的方法,需要放到一个新对象中*///  1.获取当前时间的对象Instant now = Instant.now();System.out.println(now);//标准时间,比北京时间少8个小时//  2.根据(秒/毫秒/纳秒)获取Instant对象Instant now2 = Instant.ofEpochSecond(67L);System.out.println(now2);//1970-01-01T00:01:07ZInstant now4 = Instant.ofEpochMilli(6000L);// 6000毫秒System.out.println(now4);//1970-01-01T00:00:06ZInstant now3 = Instant.ofEpochSecond(67L,1000000000L);System.out.println(now3);//1970-01-01T00:01:08Z  67秒加 (10亿纳秒=1秒)//  3.指定时区的时间ZonedDateTime localTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));System.out.println(localTime);// 上海时区//  4.判断时间差异boolean result1 = now2.isBefore(now3);//now2的时间在now3的前面System.out.println(result1);//trueboolean result2 = now2.isAfter(now3);//now2的时间在now3的后面System.out.println(result2);//false//  5.减少时间Instant result3 = now2.minusSeconds(7);// 减少了7秒System.out.println(result3);//1970-01-01T00:01:00Z//  6.增加时间Instant result4 = now2.plusSeconds(53);//增加了53秒System.out.println(result4);//1970-01-01T00:02:00Z}
}

ZonedDateTime对时间的值进行修改和增加,减少

import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZoneDateTimeDemo {public static void main(String[] args) {/*static ZonedDateTime now()         获取当前时间(带当地的时区)static ZonedDateTime ofXxxx(。。。) 获取自己输入的时间的对象ZonedDateTime withXxx(时间)         修改时间的各种方法ZonedDateTime minusXxx(时间)        减少时间的各种方法ZonedDateTime plusXxx(时间)         增加时间的各种方法*/// 1.获取当前时间(带当地的时区)ZonedDateTime time1 = ZonedDateTime.now();System.out.println(time1);//2024-05-31T23:52:57.247911600+08:00[Asia/Shanghai]// 2.获取指定时间的对象(年月日时分秒纳秒的方式指定,带时区)ZonedDateTime time2 = ZonedDateTime.of(2024,5,31,23,57,11,11, ZoneId.of("Asia/Shanghai"));System.out.println(time2);//2024-05-31T23:57:11.000000011+08:00[Asia/Shanghai]// 3.修改时间的各种方法 withXxxxZonedDateTime time3 = time2.withHour(6);System.out.println(time3);//2024-05-31T06:57:11.000000011+08:00[Asia/Shanghai]ZonedDateTime time4 = time2.withYear(2025);System.out.println(time4);//2025-05-31T23:57:11.000000011+08:00[Asia/Shanghai]// 4. 减少时间的各种方法 minusXxxxZonedDateTime time5 = time2.minusDays(6);System.out.println(time5);//2024-05-25T23:57:11.000000011+08:00[Asia/Shanghai]ZonedDateTime time6 = time2.minusHours(6);System.out.println(time6);//2024-05-31T17:57:11.000000011+08:00[Asia/Shanghai]// 5.增加时间的各种方法 plusXxxxZonedDateTime time7 = time2.plusDays(6);System.out.println(time7);//2024-06-06T23:57:11.000000011+08:00[Asia/Shanghai]ZonedDateTime time8 = time2.plusMonths(6);System.out.println(time8);//2024-06-06T23:57:11.000000011+08:00[Asia/Shanghai]}
}

ZoneId获取Java支持的所有时区的地区名称

import java.util.Set;
import java.time.ZoneId;
public class ZoneIdDemo {public static void main(String[] args) {/*static set<String>getAvailableZoneIds() 获取Java中支持的所有时区static ZoneId systemDefault()           获取系统默认时区static ZoneId of(String zoneId)         获取一个指定时区*/// 1.获取所有的时区名称Set<String> zoneIds = ZoneId.getAvailableZoneIds();System.out.println(zoneIds.size());//603System.out.println(zoneIds);// 2.获取系统默认时间ZoneId id = ZoneId.systemDefault();System.out.println(id);//Asia/Shanghai// 3.获取指定的时区ZoneId id2 = ZoneId.of("Europe/Berlin");System.out.println(id2);//Europe/Berlin}
}

相关文章:

Java 获取和修改期日与时间的各种操作方法

LocalDateTime获取当地日期和时间 import java.time.LocalDateTime; /*LocalDateTime.now() 获取当前时间*/ public class LocalDateTimeDemo {public static void main(String[] args) {LocalDateTime time1 LocalDateTime.now();System.out.println(time1);//2024-06-01T13…...

【ubuntu20】--- 定时同步文件

在编程的艺术世界里&#xff0c;代码和灵感需要寻找到最佳的交融点&#xff0c;才能打造出令人为之惊叹的作品。而在这座秋知叶i博客的殿堂里&#xff0c;我们将共同追寻这种完美结合&#xff0c;为未来的世界留下属于我们的独特印记。 【Linux命令】--- 多核压缩命令大全&…...

网吧|基于SprinBoot+vue的网吧管理系统(源码+数据库+文档)

网吧管理系统 目录 基于SprinBootvue的网吧管理系统 一、前言 二、系统设计 三、系统功能设计 1 管理员功能模块 2 网管功能模块 3 会员功能模块 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 博主介绍&#…...

[C/C++] -- Libcurl开发

libcurl 是一个功能强大的 C 语言库&#xff0c;用于实现各种网络传输协议的客户端功能。它是 Curl 工具的核心&#xff0c;并提供了一个简单、灵活、高效的 API&#xff0c;允许开发人员在他们的应用程序中轻松地执行网络操作。 以下是 libcurl 的一些主要特点和功能&#xf…...

Streamsets-JDBC模式使用更新时间字段数据同步

StreamSets的开源地址&#xff1a;https://github.com/streamsets/datacollector-oss Streamsets官网地址&#xff1a;https://streamsets.com/ Streamsets文档地址&#xff1a;https://docs.streamsets.com/portal/datacollector/3.16.x/help/index.html 我又来写Streamsets了…...

Nodejs-- 网络编程

网络编程 构建tcp服务 TCP tcp全名为传输控制协议。再osi模型中属于传输层协议。 tcp是面向连接的协议&#xff0c;在传输之前需要形成三次握手形成会话 只有会话形成了&#xff0c;服务端和客户端才能想发送数据&#xff0c;在创建会话的过程中&#xff0c;服务端和客户…...

React@16.x(14)context 举例 - Form 表单

目录 1&#xff0c;目标2&#xff0c;实现2.1&#xff0c;index.js2.2&#xff0c;context.js2.2&#xff0c;Form.Input2.3&#xff0c;Form.Button 3&#xff0c;使用 1&#xff0c;目标 上篇文章说到&#xff0c;context 上下文一般用于第3方组件库&#xff0c;因为使用场景…...

十几款基于ChatGPT的免费神器,每个都是王炸!

十几款基于ChatGPT的免费神器&#xff0c;每个都是王炸&#xff01; 1、ChatGPT ChatGPT非常强大&#xff0c;但注册需要魔法和国外的手机号&#xff0c;大部分人都没法使用。还好有一些基于API开发的体验版&#xff0c;我收集了一些可以直接使用的站点分享给大家&#xff0c…...

devicemotion 或者 deviceorientation在window.addEventListener 事件中不生效,没有输出内容

问题&#xff1a;devicemotion 或者 deviceorientation 在window.addEventListener 事件中不生效&#xff0c;没有输出内容 原因&#xff1a; 1、必须在Https协议下才可使用 2、必须用户手动点击click事件中调用 &#xff0c;进行权限申请 源码&#xff1a; <!DOCTYPE h…...

java单元测试如何断言异常

​ 在junit单元测试中&#xff0c;我们可以使用 org.junit.Assert.assertThrows 包下的 assertThrows() 方法 这个方法返回了一个泛型的异常 public static <T extends Throwable> assertThrows(Class<T> expectedType, Executable executable)​ 假设我们有以下…...

C语言| n的阶乘相加

逻辑性较强&#xff0c;建议记住。 分析思路&#xff1a; 假如n4&#xff1a;m m * i; sum sum m; 1&#xff09;当i1时&#xff0c;m1, sum1。 2&#xff09;当i2时&#xff0c;m12, sum112。 3&#xff09;当i3时&#xff0c;m123, sum112123。 4&#xff09;当i4时&…...

cwiseMax、cwiseMin函数

一、cwiseMax含义 cwiseMax是Eigen库中的一个函数&#xff0c;用于求两个矩阵或向量的逐元素最大值。它的作用类似于std::max函数&#xff0c;但是可以同时处理多个元素&#xff0c;且支持矩阵和向量。 举例&#xff1a; 例如&#xff0c;对于两个向量a和b&#xff0c;cwiseMax…...

【thinkphp问题栏】tp5.1重写URL,取消路径上的index.php

在Apache运行thinkphp5.1时&#xff0c;发现系统默认生成的.htaccess不生效。 首先先查看怎么修改伪静态 1、修改Apache的配置文件 在Apache的安装目录下&#xff0c;打开config/httpd.conf。 搜索rewrite.so&#xff0c;将前面的#删掉&#xff0c;表示开启URL重写功能 2、…...

缓冲字符流

BufferedReader/BufferedWriter增加了缓存机制&#xff0c;大大提高了读写文本文件的效率。 字符输入缓冲流 BufferedReader是针对字符输入流的缓冲流对象&#xff0c;提供了更方便的按行读取的方法&#xff1a;readLine();在使用字符流读取文本文件时&#xff0c;我们可以使…...

Django中使用Celery和APScheduler实现定时任务

在之前的文章我们已经学习了Celery和APScheduler的基本使用&#xff0c;下面让我们来了解一下如何在Django中使用Celery和APScheduler Celery 1.前提工作 python 3.7 pip install celery pip install eventlet #5.0版本以下 pip install importlib-metadata4.8.3&#xff08…...

Kivy.uix.textinput

一个小小的输入框&#xff0c;纵上下数页文档已不能全不概括&#xff0c;当去源码慢慢寻找&#xff0c;才知道其中作用&#xff0c;才能运用灵活。 Text Input — Kivy 2.3.0 documentation # -*- encoding: utf-8 -*-Text Input .. versionadded:: 1.0.4.. image:: images/te…...

基于IoTDB 平台的学习和研究

Apache IoTDB&#xff08;物联网数据库&#xff09;是一个针对物联网领域的高性能原生数据库&#xff0c;适用于数据管理和分析&#xff0c;并可在边缘计算和云端部署。由于它轻量级的架构、高性能和丰富的功能集&#xff0c;以及与Apache Hadoop、Spark和Flink的深度集成&…...

nessus plugins目录为空的问题

想要避免这种问题&#xff0c;可以将nessus服务设置为手动&#xff0c;并且先停止nessus服务。 批处理脚本&#xff1a; 下面的/~/Nessus/plugin_feed_info.inc替换成你配置好的 plugin_feed_info.inc 所在的路径 service nessusd stop; cp /~/Nessus/plugin_feed_info.inc …...

FDW(Foreign Data Wrapper)

在上一篇博客里&#xff0c;最末尾提到了 FDW。pg 实现了数百个 fdw 插件&#xff0c;用于访问外部数据。 FDW 到底是什么呢&#xff1f; 标准 FDW&#xff08;Foreign Data Wrapper&#xff09;遵循了 SQL/MED 标准&#xff0c;标准全称&#xff1a;ISO/IEC 9075-9 Managem…...

Flutter开发指南

Flutter开发指南&#xff08;Android 开发角度&#xff09; 与Android 的对比 1.Android 的View 与Flutter 的对应关系&#xff1a; a.在android 中&#xff0c;view 是屏幕显示的基础&#xff0c;比如 button&#xff0c;文本&#xff0c;列表&#xff0c;输入框都是 view。…...

synchronized 学习

学习源&#xff1a; https://www.bilibili.com/video/BV1aJ411V763?spm_id_from333.788.videopod.episodes&vd_source32e1c41a9370911ab06d12fbc36c4ebc 1.应用场景 不超卖&#xff0c;也要考虑性能问题&#xff08;场景&#xff09; 2.常见面试问题&#xff1a; sync出…...

SCAU期末笔记 - 数据分析与数据挖掘题库解析

这门怎么题库答案不全啊日 来简单学一下子来 一、选择题&#xff08;可多选&#xff09; 将原始数据进行集成、变换、维度规约、数值规约是在以下哪个步骤的任务?(C) A. 频繁模式挖掘 B.分类和预测 C.数据预处理 D.数据流挖掘 A. 频繁模式挖掘&#xff1a;专注于发现数据中…...

从深圳崛起的“机器之眼”:赴港乐动机器人的万亿赛道赶考路

进入2025年以来&#xff0c;尽管围绕人形机器人、具身智能等机器人赛道的质疑声不断&#xff0c;但全球市场热度依然高涨&#xff0c;入局者持续增加。 以国内市场为例&#xff0c;天眼查专业版数据显示&#xff0c;截至5月底&#xff0c;我国现存在业、存续状态的机器人相关企…...

Golang dig框架与GraphQL的完美结合

将 Go 的 Dig 依赖注入框架与 GraphQL 结合使用&#xff0c;可以显著提升应用程序的可维护性、可测试性以及灵活性。 Dig 是一个强大的依赖注入容器&#xff0c;能够帮助开发者更好地管理复杂的依赖关系&#xff0c;而 GraphQL 则是一种用于 API 的查询语言&#xff0c;能够提…...

Keil 中设置 STM32 Flash 和 RAM 地址详解

文章目录 Keil 中设置 STM32 Flash 和 RAM 地址详解一、Flash 和 RAM 配置界面(Target 选项卡)1. IROM1(用于配置 Flash)2. IRAM1(用于配置 RAM)二、链接器设置界面(Linker 选项卡)1. 勾选“Use Memory Layout from Target Dialog”2. 查看链接器参数(如果没有勾选上面…...

【单片机期末】单片机系统设计

主要内容&#xff1a;系统状态机&#xff0c;系统时基&#xff0c;系统需求分析&#xff0c;系统构建&#xff0c;系统状态流图 一、题目要求 二、绘制系统状态流图 题目&#xff1a;根据上述描述绘制系统状态流图&#xff0c;注明状态转移条件及方向。 三、利用定时器产生时…...

数据库分批入库

今天在工作中&#xff0c;遇到一个问题&#xff0c;就是分批查询的时候&#xff0c;由于批次过大导致出现了一些问题&#xff0c;一下是问题描述和解决方案&#xff1a; 示例&#xff1a; // 假设已有数据列表 dataList 和 PreparedStatement pstmt int batchSize 1000; // …...

全志A40i android7.1 调试信息打印串口由uart0改为uart3

一&#xff0c;概述 1. 目的 将调试信息打印串口由uart0改为uart3。 2. 版本信息 Uboot版本&#xff1a;2014.07&#xff1b; Kernel版本&#xff1a;Linux-3.10&#xff1b; 二&#xff0c;Uboot 1. sys_config.fex改动 使能uart3(TX:PH00 RX:PH01)&#xff0c;并让boo…...

微软PowerBI考试 PL300-在 Power BI 中清理、转换和加载数据

微软PowerBI考试 PL300-在 Power BI 中清理、转换和加载数据 Power Query 具有大量专门帮助您清理和准备数据以供分析的功能。 您将了解如何简化复杂模型、更改数据类型、重命名对象和透视数据。 您还将了解如何分析列&#xff0c;以便知晓哪些列包含有价值的数据&#xff0c;…...

Reasoning over Uncertain Text by Generative Large Language Models

https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829 1. 概述 文本中的不确定性在许多语境中传达,从日常对话到特定领域的文档(例如医学文档)(Heritage 2013;Landmark、Gulbrandsen 和 Svenevei…...