首页 > 其他分享 >时间日期工具类

时间日期工具类

时间:2024-09-14 18:29:26浏览次数:3  
标签:return 日期 时间 LocalDateTime date static 工具 LocalDate public


时间日期工具类

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);
    }
 
}


标签:return,日期,时间,LocalDateTime,date,static,工具,LocalDate,public
From: https://blog.51cto.com/u_15903793/12018145

相关文章

  • 字符串处理工具类
    字符串处理工具类importjava.util.Arrays;publicclassStringUtils{/***将字符串反转*@paramstr要反转的字符串*@return反转后的字符串*/publicstaticStringreverseString(Stringstr){returnnewStringBuilder(str......
  • 加密解密工具类
    加密解密工具类packagecom.example.modules.util;importjavax.crypto.Cipher;importjavax.crypto.KeyGenerator;importjavax.crypto.SecretKey;importjavax.crypto.spec.SecretKeySpec;importjava.security.SecureRandom;importjava.util.Base64;publ......
  • 【工具】前端JavaScript代码在线执行器 方便通过网页 手机测试js代码
    【工具】前端JavaScript代码在线执行器方便通过网页手机测试js代码自动补全js代码格式化代码色彩打印日志清空日志待补充<!DOCTYPEhtml><htmllang="zh-CN"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,ini......
  • 负荷预测 | Matlab基于CNN-BiGRU-Attention多变量时间序列多步预测
    目录效果一览基本介绍程序设计参考资料效果一览基本介绍1.Matlab基于CNN-BiGRU-Attention多变量时间序列多步预测;2.多变量时间序列数据集(负荷数据集),采用前多天时刻预测的特征和负荷数据预测未来多天时刻的负荷数据;3.excel数据方便替换,运行环境matlab2023及以上,展示最后96个时间步......
  • 时序预测 | MATLAB实现BKA-XGBoost(黑翅鸢优化算法优化极限梯度提升树)时间序列预测
    时序预测|MATLAB实现BKA-XGBoost(黑翅鸢优化算法优化极限梯度提升树)时间序列预测目录时序预测|MATLAB实现BKA-XGBoost(黑翅鸢优化算法优化极限梯度提升树)时间序列预测预测效果基本介绍模型描述程序设计参考资料预测效果基本介绍Matlab实现BKA-XGBoost时间序列预测,黑翅鸢优......
  • YZ系列工具之YZ07:VBA对工作簿事件的监听
    我给VBA下的定义:VBA是个人小型自动化处理的有效工具。利用好了,可以大大提高自己的工作效率,而且可以提高数据的准确度。我的教程一共九套+一部VBA手册,教程分为初级、中级、高级三大部分。是对VBA的系统讲解,从简单的入门,到数据库,到字典,到高级的网抓及类的应用;手册是为方便编程人员查......
  • capital许可监控工具
    在软件资产日益增长的今天,如何有效管理和监控软件许可,确保合规使用并优化资源,已成为企业面临的重要挑战。Capital许可监控工具,作为一款专业的软件许可监控解决方案,正是为解决这一难题而生。一、Capital许可监控工具的核心价值Capital许可监控工具通过实时追踪和监控软件许可的使......
  • LEETCODE 1709 两个日期的最大空档期
      表: UserVisits+-------------+------+|ColumnName|Type|+-------------+------+|user_id|int||visit_date|date|+-------------+------+该表没有主键,它可能有重复的行该表包含用户访问某特定零售商的日期日志。 假设今天的日期是 '2021-1-1......
  • 推荐一个比较好用的工具Microsoft PowerToys
    MicrosoftPowerToys是一组实用工具,可帮助高级用户调整和简化其Windows体验,从而提高工作效率。虽然刚刚使用这个工具不久,但是已经能初步感受到它的强大,接下来我就对于我使用的几个功能进行简单介绍。屏幕截取这个功能在日常工作中应该是经常会用到,在学生时代,使用这个功能......
  • 时间模块time and date time
    importtimeimportdatetimeprint(time.time())#时间戳格式time.sleep(1)#延迟时间print(time.localtime())#北京时区元组格式时间,结构时间print(time.strftime("%Y-%m-%d%H:%M:%S",time.localtime()))#把结构时间转换成格式时间print(time.strptime("2024......