当前位置: 首页 > 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。…...

浏览器访问 AWS ECS 上部署的 Docker 容器(监听 80 端口)

✅ 一、ECS 服务配置 Dockerfile 确保监听 80 端口 EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]或 EXPOSE 80 CMD ["python3", "-m", "http.server", "80"]任务定义&#xff08;Task Definition&…...

golang循环变量捕获问题​​

在 Go 语言中&#xff0c;当在循环中启动协程&#xff08;goroutine&#xff09;时&#xff0c;如果在协程闭包中直接引用循环变量&#xff0c;可能会遇到一个常见的陷阱 - ​​循环变量捕获问题​​。让我详细解释一下&#xff1a; 问题背景 看这个代码片段&#xff1a; fo…...

centos 7 部署awstats 网站访问检测

一、基础环境准备&#xff08;两种安装方式都要做&#xff09; bash # 安装必要依赖 yum install -y httpd perl mod_perl perl-Time-HiRes perl-DateTime systemctl enable httpd # 设置 Apache 开机自启 systemctl start httpd # 启动 Apache二、安装 AWStats&#xff0…...

如何在最短时间内提升打ctf(web)的水平?

刚刚刷完2遍 bugku 的 web 题&#xff0c;前来答题。 每个人对刷题理解是不同&#xff0c;有的人是看了writeup就等于刷了&#xff0c;有的人是收藏了writeup就等于刷了&#xff0c;有的人是跟着writeup做了一遍就等于刷了&#xff0c;还有的人是独立思考做了一遍就等于刷了。…...

python执行测试用例,allure报乱码且未成功生成报告

allure执行测试用例时显示乱码&#xff1a;‘allure’ &#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;ڲ&#xfffd;&#xfffd;&#xfffd;&#xfffd;ⲿ&#xfffd;&#xfffd;&#xfffd;Ҳ&#xfffd;&#xfffd;&#xfffd;ǿ&#xfffd;&am…...

【数据分析】R版IntelliGenes用于生物标志物发现的可解释机器学习

禁止商业或二改转载&#xff0c;仅供自学使用&#xff0c;侵权必究&#xff0c;如需截取部分内容请后台联系作者! 文章目录 介绍流程步骤1. 输入数据2. 特征选择3. 模型训练4. I-Genes 评分计算5. 输出结果 IntelliGenesR 安装包1. 特征选择2. 模型训练和评估3. I-Genes 评分计…...

RSS 2025|从说明书学习复杂机器人操作任务:NUS邵林团队提出全新机器人装配技能学习框架Manual2Skill

视觉语言模型&#xff08;Vision-Language Models, VLMs&#xff09;&#xff0c;为真实环境中的机器人操作任务提供了极具潜力的解决方案。 尽管 VLMs 取得了显著进展&#xff0c;机器人仍难以胜任复杂的长时程任务&#xff08;如家具装配&#xff09;&#xff0c;主要受限于人…...

并发编程 - go版

1.并发编程基础概念 进程和线程 A. 进程是程序在操作系统中的一次执行过程&#xff0c;系统进行资源分配和调度的一个独立单位。B. 线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。C.一个进程可以创建和撤销多个线程;同一个进程中…...

零知开源——STM32F103RBT6驱动 ICM20948 九轴传感器及 vofa + 上位机可视化教程

STM32F1 本教程使用零知标准板&#xff08;STM32F103RBT6&#xff09;通过I2C驱动ICM20948九轴传感器&#xff0c;实现姿态解算&#xff0c;并通过串口将数据实时发送至VOFA上位机进行3D可视化。代码基于开源库修改优化&#xff0c;适合嵌入式及物联网开发者。在基础驱动上新增…...

Java求职者面试指南:Spring、Spring Boot、Spring MVC与MyBatis技术解析

Java求职者面试指南&#xff1a;Spring、Spring Boot、Spring MVC与MyBatis技术解析 一、第一轮基础概念问题 1. Spring框架的核心容器是什么&#xff1f;它的作用是什么&#xff1f; Spring框架的核心容器是IoC&#xff08;控制反转&#xff09;容器。它的主要作用是管理对…...