时间日期工具类
时间日期工具类
-
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有大量的配置文件,所以 Linux的文本处理工具也是比较多的,其中编辑一些配置文件时,常用的工具就是 vim。在Linux中,Vim编辑器是一个非常强大的文本编辑工具,它提供了多种模式和命令来满足不同的编辑需求。以…...
计算机的错误计算(八十九)
摘要 探讨反双曲余切函数 acoth(x) 在 附近的计算精度问题。 Acoth(x) 函数的定义为: 其中 x 的绝对值大于 1 . 例1. 计算 acoth(1.000000000002) . 不妨在 Excel 的单元格中计算,则有: 若在Python中用定义直接计算,则有几乎…...
深入理解java并发编程之aqs框架
跟synchronized 相比较,可重入锁ReentrankLock其实原理有什么不同? 所得基本原理是为了达到一个目的;就是让所有线程都能看到某种标记。synchronized通过在对象头中设置标记实现了这一目的,是一种JVM原生的锁实现方式。而Reentran…...
ubuntu配置tftp、nfs
tftp配置 tftp是简单文件传输协议,基于udp实现传输。这里的简单文件,指的是不复杂、开销不大的文件。 先在ubuntu中安装tftp,输入命令:sudo apt-get install tftp-hpa tftpd-hpa。 接着配置tftp。 输入命令:sudo v…...
Sklearn的datasets模块与自带数据集介绍
datasets 模块 用 dir() 函数查看 datasets 模块的所有属性和函数 import sklearn.datasets as datasets# 列出 sklearn.datasets 模块中的所有属性和函数 print(dir(datasets)) datasets 模块下的数据集有三种类型: (1)load系列的经典数…...
css 个人喜欢的样式 速查笔记
起因, 目的: 记录自己喜欢的, 觉得比较好看的 css. 下次用的时候,直接复制,很方便。 1. 个人 html 模板, 导入常用的 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: #include <iostream> #include <string>class compileDate {// 静态函数,用来格式化并返回编译日期 static std::string formatCompileDate() {// 编译时的日期,格式为 "MMM…...
git如何灵活切换本地账号对应远程github的两个账号
git如何灵活切换本地账号对应远程github的两个账号 问题: 有时候我们会同时维护两个github的账号里面的仓库内容,这时候本地git需要频繁的切换ssh,以方便灵活的与两个账号的仓库可以通信。这篇日记将阐述我是怎么解决这个问题的。1. 第一个账…...
Python中实现函数的递归调用
在Python中,函数的递归调用是一种非常强大且常用的编程技巧,它允许函数在其执行过程中调用自身。递归调用在解决许多问题时都显得尤为方便,比如遍历树形结构、计算阶乘、实现快速排序等。然而,递归也需要谨慎使用,因为…...
Multisim使用手册
目录 原件库: 基础元件库: 最右侧的分析仪们: 示波器: 交流分析: 操作: dB 一、幅频特性曲线 二、相频特性曲线 下载资源(有汉化):Multisim 14.0电路设计与仿真…...
线程的六种状态
优质博文:IT-BLOG-CN 线程的状态在Thread.State这个枚举类型中定义:共有6种状态,可以调用线程Thread种的getState()方法获取当前线程状态。 public enum State { /** * 新建状态(New): * 当用new操作符创建一个线程时&#…...
全球热门剪辑软件大搜罗
如果你要为你的视频进行配音那肯定离不开音频剪辑软件,现在有不少音频剪辑软件免费版本就可以实现我们并不复杂的音频剪辑操作。这次我就给你分享几款能提高剪辑效率的音频剪辑工具。 1.福晰音频剪辑 链接直达>>https://www.foxitsoftware.cn/audio-clip/ …...
swagger-bootstrap-ui页面空白,也没报错
回想起来,代码层面没有进行什么大的调整,增加了配置文件,application.yml中的 spring:profiles:active: sms # dev --> smsname: sms-server swagger配置未调整导致空白 修改profile 问题解决...
15.2 JDBC数据库编程2
15.2.1 数据库访问步骤 使用JDBC API连接和访问数据库,一般分为以下5个步骤: (1) 加载驱动程序 (2) 建立连接对象 (3) 创建语句对象 (4) 获得SQL语句的执行结果 (5) 关闭建立的对象,释放资源 下面将详细描述这些步骤 15.2.2 加载驱动程序 要使…...
Spark数据介绍
从趋势上看,DataFrame 和 Dataset 更加流行。 示例场景 数据仓库和 BI 工具集成: 如果你需要处理存储在数据仓库中的结构化数据,并且希望与 BI 工具集成,那么 DataFrame 和 Dataset 是首选。 机器学习流水线: 在构建机…...
【0基础】制作HTML网页小游戏——贪吃蛇(附详细解析)
我在昨天的文章(贪吃蛇HTML源码)里面分享了网页版贪吃蛇小游戏的源码,今天就来给大家详细讲解一下每部分代码是如何运作的,以及以后要如何美化贪吃蛇的UI界面,在哪里修改等。 目录 一、代码运作 1、HTML结构: 2、C…...
Vscode python无法转到函数定义
今天上午换了电脑,使用Vscode发现找不到对应的函数定义了。 使用了网上的全部教程。一点用没有。重启电脑,重启Vscode也没有作用。最后通过重装vscode,解决问题。(也不知道Vscode什么毛病) 重点语句: 去官网…...
Python中的上下文管理器(with语句)及其作用
Python中的上下文管理器(Context Manager)是一种通过with语句来管理资源(如文件、网络连接、线程锁等)的机制。with语句旨在简化常见的资源管理任务,如资源的获取、使用后的清理等。使用上下文管理器可以确保资源在使用…...
CTK框架(八):服务追踪
目录 1.简介 2.实现方式 3.具体实现 3.1.新建插件PluginA 3.2.新建插件PluginB 4.服务追踪的优势 5.应用场景 6.总结 1.简介 CTK服务追踪是一种机制,用于在CTK插件框架中追踪和管理插件提供的服务。当一个插件注册了一个服务到服务注册中心后࿰…...
UE5 学习系列(二)用户操作界面及介绍
这篇博客是 UE5 学习系列博客的第二篇,在第一篇的基础上展开这篇内容。博客参考的 B 站视频资料和第一篇的链接如下: 【Note】:如果你已经完成安装等操作,可以只执行第一篇博客中 2. 新建一个空白游戏项目 章节操作,重…...
使用docker在3台服务器上搭建基于redis 6.x的一主两从三台均是哨兵模式
一、环境及版本说明 如果服务器已经安装了docker,则忽略此步骤,如果没有安装,则可以按照一下方式安装: 1. 在线安装(有互联网环境): 请看我这篇文章 传送阵>> 点我查看 2. 离线安装(内网环境):请看我这篇文章 传送阵>> 点我查看 说明:假设每台服务器已…...
MPNet:旋转机械轻量化故障诊断模型详解python代码复现
目录 一、问题背景与挑战 二、MPNet核心架构 2.1 多分支特征融合模块(MBFM) 2.2 残差注意力金字塔模块(RAPM) 2.2.1 空间金字塔注意力(SPA) 2.2.2 金字塔残差块(PRBlock) 2.3 分类器设计 三、关键技术突破 3.1 多尺度特征融合 3.2 轻量化设计策略 3.3 抗噪声…...
【第二十一章 SDIO接口(SDIO)】
第二十一章 SDIO接口 目录 第二十一章 SDIO接口(SDIO) 1 SDIO 主要功能 2 SDIO 总线拓扑 3 SDIO 功能描述 3.1 SDIO 适配器 3.2 SDIOAHB 接口 4 卡功能描述 4.1 卡识别模式 4.2 卡复位 4.3 操作电压范围确认 4.4 卡识别过程 4.5 写数据块 4.6 读数据块 4.7 数据流…...
C++ 基础特性深度解析
目录 引言 一、命名空间(namespace) C 中的命名空间 与 C 语言的对比 二、缺省参数 C 中的缺省参数 与 C 语言的对比 三、引用(reference) C 中的引用 与 C 语言的对比 四、inline(内联函数…...
【OSG学习笔记】Day 16: 骨骼动画与蒙皮(osgAnimation)
骨骼动画基础 骨骼动画是 3D 计算机图形中常用的技术,它通过以下两个主要组件实现角色动画。 骨骼系统 (Skeleton):由层级结构的骨头组成,类似于人体骨骼蒙皮 (Mesh Skinning):将模型网格顶点绑定到骨骼上,使骨骼移动…...
tree 树组件大数据卡顿问题优化
问题背景 项目中有用到树组件用来做文件目录,但是由于这个树组件的节点越来越多,导致页面在滚动这个树组件的时候浏览器就很容易卡死。这种问题基本上都是因为dom节点太多,导致的浏览器卡顿,这里很明显就需要用到虚拟列表的技术&…...
2025季度云服务器排行榜
在全球云服务器市场,各厂商的排名和地位并非一成不变,而是由其独特的优势、战略布局和市场适应性共同决定的。以下是根据2025年市场趋势,对主要云服务器厂商在排行榜中占据重要位置的原因和优势进行深度分析: 一、全球“三巨头”…...
【Go语言基础【13】】函数、闭包、方法
文章目录 零、概述一、函数基础1、函数基础概念2、参数传递机制3、返回值特性3.1. 多返回值3.2. 命名返回值3.3. 错误处理 二、函数类型与高阶函数1. 函数类型定义2. 高阶函数(函数作为参数、返回值) 三、匿名函数与闭包1. 匿名函数(Lambda函…...
springboot整合VUE之在线教育管理系统简介
可以学习到的技能 学会常用技术栈的使用 独立开发项目 学会前端的开发流程 学会后端的开发流程 学会数据库的设计 学会前后端接口调用方式 学会多模块之间的关联 学会数据的处理 适用人群 在校学生,小白用户,想学习知识的 有点基础,想要通过项…...
