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

AI-调查研究-01-正念冥想有用吗?对健康的影响及科学指南

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

第19节 Node.js Express 框架

Express 是一个为Node.js设计的web开发框架&#xff0c;它基于nodejs平台。 Express 简介 Express是一个简洁而灵活的node.js Web应用框架, 提供了一系列强大特性帮助你创建各种Web应用&#xff0c;和丰富的HTTP工具。 使用Express可以快速地搭建一个完整功能的网站。 Expre…...

使用VSCode开发Django指南

使用VSCode开发Django指南 一、概述 Django 是一个高级 Python 框架&#xff0c;专为快速、安全和可扩展的 Web 开发而设计。Django 包含对 URL 路由、页面模板和数据处理的丰富支持。 本文将创建一个简单的 Django 应用&#xff0c;其中包含三个使用通用基本模板的页面。在此…...

Flask RESTful 示例

目录 1. 环境准备2. 安装依赖3. 修改main.py4. 运行应用5. API使用示例获取所有任务获取单个任务创建新任务更新任务删除任务 中文乱码问题&#xff1a; 下面创建一个简单的Flask RESTful API示例。首先&#xff0c;我们需要创建环境&#xff0c;安装必要的依赖&#xff0c;然后…...

Java 语言特性(面试系列1)

一、面向对象编程 1. 封装&#xff08;Encapsulation&#xff09; 定义&#xff1a;将数据&#xff08;属性&#xff09;和操作数据的方法绑定在一起&#xff0c;通过访问控制符&#xff08;private、protected、public&#xff09;隐藏内部实现细节。示例&#xff1a; public …...

Docker 运行 Kafka 带 SASL 认证教程

Docker 运行 Kafka 带 SASL 认证教程 Docker 运行 Kafka 带 SASL 认证教程一、说明二、环境准备三、编写 Docker Compose 和 jaas文件docker-compose.yml代码说明&#xff1a;server_jaas.conf 四、启动服务五、验证服务六、连接kafka服务七、总结 Docker 运行 Kafka 带 SASL 认…...

java调用dll出现unsatisfiedLinkError以及JNA和JNI的区别

UnsatisfiedLinkError 在对接硬件设备中&#xff0c;我们会遇到使用 java 调用 dll文件 的情况&#xff0c;此时大概率出现UnsatisfiedLinkError链接错误&#xff0c;原因可能有如下几种 类名错误包名错误方法名参数错误使用 JNI 协议调用&#xff0c;结果 dll 未实现 JNI 协…...

Leetcode 3577. Count the Number of Computer Unlocking Permutations

Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接&#xff1a;3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯&#xff0c;要想要能够将所有的电脑解锁&#x…...

(二)原型模式

原型的功能是将一个已经存在的对象作为源目标,其余对象都是通过这个源目标创建。发挥复制的作用就是原型模式的核心思想。 一、源型模式的定义 原型模式是指第二次创建对象可以通过复制已经存在的原型对象来实现,忽略对象创建过程中的其它细节。 📌 核心特点: 避免重复初…...

C++ 基础特性深度解析

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