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

时间日期工具类

时间日期工具类

  • import java.time.*;
    import java.time.format.DateTimeFormatter;
    import java.time.temporal.ChronoUnit;public class DateTimeUtils {private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";private static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";private static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");// 获取当前日期public static LocalDate getCurrentDate() {return LocalDate.now();}// 获取当前时间public static LocalTime getCurrentTime() {return LocalTime.now();}// 获取当前日期时间public static LocalDateTime getCurrentDateTime() {return LocalDateTime.now();}// 获取当前时间戳public static long getCurrentTimestamp() {return Instant.now().toEpochMilli();}/***  生成当前时间字符串,默认格式yyyy-MM-dd HH:mm:ss* @return java.lang.String**/public static String currentDateStr() {LocalDateTime now = LocalDateTime.now();return now.format(formatter);}/*** 指定日期格式生成当前时间字符串* @param formatter* @return java.lang.String**/public static String currentDateStr(String formatter) {LocalDateTime now = LocalDateTime.now();DateTimeFormatter pattern = DateTimeFormatter.ofPattern(formatter);return now.format(pattern);}/****时间转字符串,默认格式化:yyyy-MM-dd HH:mm:ss* @param dateTime* @return java.lang.String**/public static String dateToStr(LocalDateTime dateTime) {return dateTime.format(formatter);}/****时间转字符串* @param dateTime* @param formatter 格式化* @return java.lang.String**/public static String dateToStr(LocalDateTime dateTime, DateTimeFormatter formatter) {return dateTime.format(formatter);}/****判断bigTime是否大于smallTime* @param smallTime* @param bigTime* @return boolean**/public static boolean lessThanTime(String smallTime, String bigTime) {LocalDateTime smallDateTime = LocalDateTime.parse(smallTime, formatter);LocalDateTime bigDateTime = LocalDateTime.parse(bigTime, formatter);return smallDateTime.isBefore(bigDateTime);}/****获取num天前的日期* @param num* @return java.lang.String**/public static String getPastDate(int num) {LocalDate currentDate = LocalDate.now();LocalDate pastDate = currentDate.minusDays(num);DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");return pastDate.format(formatter);}/****获取近n天的日期字符串集合* @param n* @return java.util.List<java.lang.String>**/public static List<String> getRecentNumDays(int n) {List<String> dates = new ArrayList<>();LocalDate today = LocalDate.now();for (int i = 0; i < n; i++) {LocalDate date = today.minusDays(i);String dateStr = date.format(DateTimeFormatter.ofPattern("MM-dd"));dates.add(dateStr);}return dates;}// 将日期字符串解析成LocalDate对象public static LocalDate parseDate(String date) {return LocalDate.parse(date);}// 将时间字符串解析成LocalTime对象public static LocalTime parseTime(String time) {return LocalTime.parse(time);}// 将日期时间字符串解析成LocalDateTime对象public static LocalDateTime parseDateTime(String dateTime) {return LocalDateTime.parse(dateTime);}// 将日期时间字符串按照指定格式解析成LocalDateTime对象public static LocalDateTime parseDateTime(String dateTime, String format) {DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);return LocalDateTime.parse(dateTime, formatter);}// 将LocalDate对象格式化成日期字符串public static String formatDate(LocalDate date) {return date.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));}// 将LocalTime对象格式化成时间字符串public static String formatTime(LocalTime time) {return time.format(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT));}// 将LocalDateTime对象格式化成日期时间字符串public static String formatDateTime(LocalDateTime dateTime) {return dateTime.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));}// 将LocalDateTime对象按照指定格式格式化成日期时间字符串public static String formatDateTime(LocalDateTime dateTime, String format) {return dateTime.format(DateTimeFormatter.ofPattern(format));}// 获取指定日期的星期几(1-7,分别代表周一至周日)public static int getDayOfWeek(LocalDate date) {return date.getDayOfWeek().getValue();}// 获取指定日期的年份public static int getYear(LocalDate date) {return date.getYear();}// 获取指定日期的月份public static int getMonth(LocalDate date) {return date.getMonthValue();}// 获取指定日期的天数public static int getDayOfMonth(LocalDate date) {return date.getDayOfMonth();}// 获取指定日期是当年的第几天public static int getDayOfYear(LocalDate date) {return date.getDayOfYear();}// 获取指定日期是否为闰年public static boolean isLeapYear(LocalDate date) {return date.isLeapYear();}// 获取指定日期之前或之后的几天public static LocalDate plusDays(LocalDate date, long days) {return date.plusDays(days);}// 获取指定日期之前或之后的几周public static LocalDate plusWeeks(LocalDate date, long weeks) {return date.plusWeeks(weeks);}// 获取指定日期之前或之后的几个月public static LocalDate plusMonths(LocalDate date, long months) {return date.plusMonths(months);}// 获取指定日期之前或之后的几年public static LocalDate plusYears(LocalDate date, long years) {return date.plusYears(years);}// 获取指定日期之前或之后的几小时public static LocalDateTime plusHours(LocalDateTime dateTime, long hours) {return dateTime.plusHours(hours);}// 获取指定日期之前或之后的几分钟public static LocalDateTime plusMinutes(LocalDateTime dateTime, long minutes) {return dateTime.plusMinutes(minutes);}// 获取指定日期之前或之后的几秒钟public static LocalDateTime plusSeconds(LocalDateTime dateTime, long seconds) {return dateTime.plusSeconds(seconds);}// 获取指定日期之前或之后的几毫秒public static LocalDateTime plusMilliseconds(LocalDateTime dateTime, long milliseconds) {return dateTime.plus(milliseconds, ChronoUnit.MILLIS);}// 获取指定日期之前或之后的几纳秒public static LocalDateTime plusNanoseconds(LocalDateTime dateTime, long nanoseconds) {return dateTime.plus(nanoseconds, ChronoUnit.NANOS);}// 比较两个日期的先后顺序(返回值小于0表示date1在date2之前,等于0表示两个日期相等,大于0表示date1在date2之后)public static int compareDates(LocalDate date1, LocalDate date2) {return date1.compareTo(date2);}// 判断两个日期是否相等public static boolean areDatesEqual(LocalDate date1, LocalDate date2) {return date1.isEqual(date2);}// 计算两个日期之间的天数差public static long getDaysBetween(LocalDate date1, LocalDate date2) {return ChronoUnit.DAYS.between(date1, date2);}// 计算两个日期之间的周数差public static long getWeeksBetween(LocalDate date1, LocalDate date2) {return ChronoUnit.WEEKS.between(date1, date2);}// 计算两个日期之间的月数差public static long getMonthsBetween(LocalDate date1, LocalDate date2) {return ChronoUnit.MONTHS.between(date1, date2);}// 计算两个日期之间的年数差public static long getYearsBetween(LocalDate date1, LocalDate date2) {return ChronoUnit.YEARS.between(date1, date2);}// 判断指定日期是否在当前日期之前public static boolean isBeforeCurrentDate(LocalDate date) {return date.isBefore(getCurrentDate());}// 判断指定日期是否在当前日期之后public static boolean isAfterCurrentDate(LocalDate date) {return date.isAfter(getCurrentDate());}// 判断指定时间是否在当前时间之前public static boolean isBeforeCurrentTime(LocalTime time) {return time.isBefore(getCurrentTime());}// 判断指定时间是否在当前时间之后public static boolean isAfterCurrentTime(LocalTime time) {return time.isAfter(getCurrentTime());}// 判断指定日期时间是否在当前日期时间之前public static boolean isBeforeCurrentDateTime(LocalDateTime dateTime) {return dateTime.isBefore(getCurrentDateTime());}// 判断指定日期时间是否在当前日期时间之后public static boolean isAfterCurrentDateTime(LocalDateTime dateTime) {return dateTime.isAfter(getCurrentDateTime());}// 判断两个日期时间是否相等public static boolean areDateTimesEqual(LocalDateTime dateTime1, LocalDateTime dateTime2) {return dateTime1.isEqual(dateTime2);}// 计算两个日期时间之间的小时数差public static long getHoursBetween(LocalDateTime dateTime1, LocalDateTime dateTime2) {return ChronoUnit.HOURS.between(dateTime1, dateTime2);}// 计算两个日期时间之间的分钟数差public static long getMinutesBetween(LocalDateTime dateTime1, LocalDateTime dateTime2) {return ChronoUnit.MINUTES.between(dateTime1, dateTime2);}// 计算两个日期时间之间的秒数差public static long getSecondsBetween(LocalDateTime dateTime1, LocalDateTime dateTime2) {return ChronoUnit.SECONDS.between(dateTime1, dateTime2);}// 计算两个日期时间之间的毫秒数差public static long getMillisecondsBetween(LocalDateTime dateTime1, LocalDateTime dateTime2) {return ChronoUnit.MILLIS.between(dateTime1, dateTime2);}// 计算两个日期时间之间的纳秒数差public static long getNanosecondsBetween(LocalDateTime dateTime1, LocalDateTime dateTime2) {return ChronoUnit.NANOS.between(dateTime1, dateTime2);}}
    

相关文章:

时间日期工具类

时间日期工具类 import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit;public class DateTimeUtils {private static final String DEFAULT_DATE_FORMAT "yyyy-MM-dd";private static final String DEFAULT_TIME_…...

linux中vim常用命令大全

前言 Linux有大量的配置文件&#xff0c;所以 Linux的文本处理工具也是比较多的&#xff0c;其中编辑一些配置文件时&#xff0c;常用的工具就是 vim。在Linux中&#xff0c;Vim编辑器是一个非常强大的文本编辑工具&#xff0c;它提供了多种模式和命令来满足不同的编辑需求。以…...

计算机的错误计算(八十九)

摘要 探讨反双曲余切函数 acoth(x) 在 附近的计算精度问题。 Acoth(x) 函数的定义为&#xff1a; 其中 x 的绝对值大于 1 . 例1. 计算 acoth(1.000000000002) . 不妨在 Excel 的单元格中计算&#xff0c;则有&#xff1a; 若在Python中用定义直接计算&#xff0c;则有几乎…...

深入理解java并发编程之aqs框架

跟synchronized 相比较&#xff0c;可重入锁ReentrankLock其实原理有什么不同&#xff1f; 所得基本原理是为了达到一个目的&#xff1b;就是让所有线程都能看到某种标记。synchronized通过在对象头中设置标记实现了这一目的&#xff0c;是一种JVM原生的锁实现方式。而Reentran…...

ubuntu配置tftp、nfs

tftp配置 tftp是简单文件传输协议&#xff0c;基于udp实现传输。这里的简单文件&#xff0c;指的是不复杂、开销不大的文件。 先在ubuntu中安装tftp&#xff0c;输入命令&#xff1a;sudo apt-get install tftp-hpa tftpd-hpa。 接着配置tftp。 输入命令&#xff1a;sudo v…...

Sklearn的datasets模块与自带数据集介绍

datasets 模块 用 dir() 函数查看 datasets 模块的所有属性和函数 import sklearn.datasets as datasets# 列出 sklearn.datasets 模块中的所有属性和函数 print(dir(datasets)) datasets 模块下的数据集有三种类型&#xff1a; &#xff08;1&#xff09;load系列的经典数…...

css 个人喜欢的样式 速查笔记

起因&#xff0c; 目的: 记录自己喜欢的&#xff0c; 觉得比较好看的 css. 下次用的时候&#xff0c;直接复制&#xff0c;很方便。 1. 个人 html 模板&#xff0c; 导入常用的 link 设置英语字体: Noto导入默认的 css使用网络 icon 图标导入 Bootstrap css 框架 html <…...

C/C++ let __DATE__ format to “YYYY-MM-DD“

C/C let DATE format to “YYYY-MM-DD” code&#xff1a; #include <iostream> #include <string>class compileDate {// 静态函数&#xff0c;用来格式化并返回编译日期 static std::string formatCompileDate() {// 编译时的日期&#xff0c;格式为 "MMM…...

git如何灵活切换本地账号对应远程github的两个账号

git如何灵活切换本地账号对应远程github的两个账号 问题&#xff1a; 有时候我们会同时维护两个github的账号里面的仓库内容&#xff0c;这时候本地git需要频繁的切换ssh&#xff0c;以方便灵活的与两个账号的仓库可以通信。这篇日记将阐述我是怎么解决这个问题的。1. 第一个账…...

Python中实现函数的递归调用

在Python中&#xff0c;函数的递归调用是一种非常强大且常用的编程技巧&#xff0c;它允许函数在其执行过程中调用自身。递归调用在解决许多问题时都显得尤为方便&#xff0c;比如遍历树形结构、计算阶乘、实现快速排序等。然而&#xff0c;递归也需要谨慎使用&#xff0c;因为…...

Multisim使用手册

目录 原件库&#xff1a; 基础元件库&#xff1a; 最右侧的分析仪们&#xff1a; 示波器&#xff1a; 交流分析&#xff1a; 操作&#xff1a; dB 一、幅频特性曲线 二、相频特性曲线 下载资源&#xff08;有汉化&#xff09;&#xff1a;Multisim 14.0电路设计与仿真…...

线程的六种状态

优质博文&#xff1a;IT-BLOG-CN 线程的状态在Thread.State这个枚举类型中定义&#xff1a;共有6种状态&#xff0c;可以调用线程Thread种的getState()方法获取当前线程状态。 public enum State { /** * 新建状态(New)&#xff1a; * 当用new操作符创建一个线程时&#…...

全球热门剪辑软件大搜罗

如果你要为你的视频进行配音那肯定离不开音频剪辑软件&#xff0c;现在有不少音频剪辑软件免费版本就可以实现我们并不复杂的音频剪辑操作。这次我就给你分享几款能提高剪辑效率的音频剪辑工具。 1.福晰音频剪辑 链接直达>>https://www.foxitsoftware.cn/audio-clip/ …...

swagger-bootstrap-ui页面空白,也没报错

回想起来&#xff0c;代码层面没有进行什么大的调整&#xff0c;增加了配置文件&#xff0c;application.yml中的 spring:profiles:active: sms # dev --> smsname: sms-server swagger配置未调整导致空白 修改profile 问题解决...

15.2 JDBC数据库编程2

15.2.1 数据库访问步骤 使用JDBC API连接和访问数据库&#xff0c;一般分为以下5个步骤: (1) 加载驱动程序 (2) 建立连接对象 (3) 创建语句对象 (4) 获得SQL语句的执行结果 (5) 关闭建立的对象&#xff0c;释放资源 下面将详细描述这些步骤 15.2.2 加载驱动程序 要使…...

Spark数据介绍

从趋势上看&#xff0c;DataFrame 和 Dataset 更加流行。 示例场景 数据仓库和 BI 工具集成&#xff1a; 如果你需要处理存储在数据仓库中的结构化数据&#xff0c;并且希望与 BI 工具集成&#xff0c;那么 DataFrame 和 Dataset 是首选。 机器学习流水线&#xff1a; 在构建机…...

【0基础】制作HTML网页小游戏——贪吃蛇(附详细解析)

我在昨天的文章&#xff08;贪吃蛇HTML源码&#xff09;里面分享了网页版贪吃蛇小游戏的源码&#xff0c;今天就来给大家详细讲解一下每部分代码是如何运作的&#xff0c;以及以后要如何美化贪吃蛇的UI界面&#xff0c;在哪里修改等。 目录 一、代码运作 1、HTML结构: 2、C…...

Vscode python无法转到函数定义

今天上午换了电脑&#xff0c;使用Vscode发现找不到对应的函数定义了。 使用了网上的全部教程。一点用没有。重启电脑&#xff0c;重启Vscode也没有作用。最后通过重装vscode&#xff0c;解决问题。&#xff08;也不知道Vscode什么毛病&#xff09; 重点语句&#xff1a; 去官网…...

Python中的上下文管理器(with语句)及其作用

Python中的上下文管理器&#xff08;Context Manager&#xff09;是一种通过with语句来管理资源&#xff08;如文件、网络连接、线程锁等&#xff09;的机制。with语句旨在简化常见的资源管理任务&#xff0c;如资源的获取、使用后的清理等。使用上下文管理器可以确保资源在使用…...

CTK框架(八):服务追踪

目录 1.简介 2.实现方式 3.具体实现 3.1.新建插件PluginA​​ 3.2.新建插件PluginB 4.服务追踪的优势 5.应用场景 6.总结 1.简介 CTK服务追踪是一种机制&#xff0c;用于在CTK插件框架中追踪和管理插件提供的服务。当一个插件注册了一个服务到服务注册中心后&#xff0…...

stm32G473的flash模式是单bank还是双bank?

今天突然有人stm32G473的flash模式是单bank还是双bank&#xff1f;由于时间太久&#xff0c;我真忘记了。搜搜发现&#xff0c;还真有人和我一样。见下面的链接&#xff1a;https://shequ.stmicroelectronics.cn/forum.php?modviewthread&tid644563 根据STM32G4系列参考手…...

脑机新手指南(八):OpenBCI_GUI:从环境搭建到数据可视化(下)

一、数据处理与分析实战 &#xff08;一&#xff09;实时滤波与参数调整 基础滤波操作 60Hz 工频滤波&#xff1a;勾选界面右侧 “60Hz” 复选框&#xff0c;可有效抑制电网干扰&#xff08;适用于北美地区&#xff0c;欧洲用户可调整为 50Hz&#xff09;。 平滑处理&…...

从零实现富文本编辑器#5-编辑器选区模型的状态结构表达

先前我们总结了浏览器选区模型的交互策略&#xff0c;并且实现了基本的选区操作&#xff0c;还调研了自绘选区的实现。那么相对的&#xff0c;我们还需要设计编辑器的选区表达&#xff0c;也可以称为模型选区。编辑器中应用变更时的操作范围&#xff0c;就是以模型选区为基准来…...

ffmpeg(四):滤镜命令

FFmpeg 的滤镜命令是用于音视频处理中的强大工具&#xff0c;可以完成剪裁、缩放、加水印、调色、合成、旋转、模糊、叠加字幕等复杂的操作。其核心语法格式一般如下&#xff1a; ffmpeg -i input.mp4 -vf "滤镜参数" output.mp4或者带音频滤镜&#xff1a; ffmpeg…...

C++ 基础特性深度解析

目录 引言 一、命名空间&#xff08;namespace&#xff09; C 中的命名空间​ 与 C 语言的对比​ 二、缺省参数​ C 中的缺省参数​ 与 C 语言的对比​ 三、引用&#xff08;reference&#xff09;​ C 中的引用​ 与 C 语言的对比​ 四、inline&#xff08;内联函数…...

以光量子为例,详解量子获取方式

光量子技术获取量子比特可在室温下进行。该方式有望通过与名为硅光子学&#xff08;silicon photonics&#xff09;的光波导&#xff08;optical waveguide&#xff09;芯片制造技术和光纤等光通信技术相结合来实现量子计算机。量子力学中&#xff0c;光既是波又是粒子。光子本…...

基于Java+MySQL实现(GUI)客户管理系统

客户资料管理系统的设计与实现 第一章 需求分析 1.1 需求总体介绍 本项目为了方便维护客户信息为了方便维护客户信息&#xff0c;对客户进行统一管理&#xff0c;可以把所有客户信息录入系统&#xff0c;进行维护和统计功能。可通过文件的方式保存相关录入数据&#xff0c;对…...

【从零开始学习JVM | 第四篇】类加载器和双亲委派机制(高频面试题)

前言&#xff1a; 双亲委派机制对于面试这块来说非常重要&#xff0c;在实际开发中也是经常遇见需要打破双亲委派的需求&#xff0c;今天我们一起来探索一下什么是双亲委派机制&#xff0c;在此之前我们先介绍一下类的加载器。 目录 ​编辑 前言&#xff1a; 类加载器 1. …...

Rust 开发环境搭建

环境搭建 1、开发工具RustRover 或者vs code 2、Cygwin64 安装 https://cygwin.com/install.html 在工具终端执行&#xff1a; rustup toolchain install stable-x86_64-pc-windows-gnu rustup default stable-x86_64-pc-windows-gnu ​ 2、Hello World fn main() { println…...

MyBatis中关于缓存的理解

MyBatis缓存 MyBatis系统当中默认定义两级缓存&#xff1a;一级缓存、二级缓存 默认情况下&#xff0c;只有一级缓存开启&#xff08;sqlSession级别的缓存&#xff09;二级缓存需要手动开启配置&#xff0c;需要局域namespace级别的缓存 一级缓存&#xff08;本地缓存&#…...