Java 时间工具类(计算距离现在多久、返回某个日期的前一天、 返回当月最后一天的日期、获取当前时间到凌晨还剩多少毫秒、获取当前时间前一个月第一天......)

这个Java时间工具类提供了丰富的日期处理方法,包括计算距离现在多久(精确和非精确)、获取日期的前一天、返回当月最后一天、计算当前时间到凌晨的毫秒数、以及获取当前时间前一个月的第一天和最后一天等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.qycar.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**

  • 日期处理
    /
    public class DateUtils {
    /
    *

    • 时间格式(yyyy-MM-dd)
      /
      public static final String DATE_PATTERN = “yyyy-MM-dd”;
      public static final String MOTH_PATTERN = “yyyy-MM”;
      /
      *
    • 时间格式(yyyy-MM-dd HH:mm:ss)
      */
      public static final String DATE_TIME_PATTERN = “yyyy-MM-dd HH:mm:ss”;

    public static final String format(Date date) {
    return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
    if (date != null) {
    SimpleDateFormat df = new SimpleDateFormat(pattern);
    return df.format(date);
    }
    return null;
    }

    /**

    • 计算距离现在多久,非精确
    • @param date
    • @return
      */
      public static String getTimeBefore(Date date) {
      Date now = new Date();
      long l = now.getTime() - date.getTime();
      long day = l / (24 * 60 * 60 * 1000);
      long hour = (l / (60 * 60 * 1000) - day * 24);
      long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
      long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
      String r = “”;
      if (day > 0) {
      r += day + “天”;
      } else if (hour > 0) {
      r += hour + “小时”;
      } else if (min > 0) {
      r += min + “分”;
      } else if (s > 0) {
      r += s + “秒”;
      }
      r += “前”;
      return r;
      }

    /**

    • 计算距离现在多久,精确
    • @param date
    • @return
      */
      public static String getTimeBeforeAccurate(Date date) {
      Date now = new Date();
      long l = now.getTime() - date.getTime();
      long day = l / (24 * 60 * 60 * 1000);
      long hour = (l / (60 * 60 * 1000) - day * 24);
      long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
      long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
      String r = “”;
      if (day > 0) {
      r += day + “天”;
      }
      if (hour > 0) {
      r += hour + “小时”;
      }
      if (min > 0) {
      r += min + “分”;
      }
      if (s > 0) {
      r += s + “秒”;
      }
      r += “前”;
      return r;
      }

    /**

    • 返回某个日期的前一天
      */
      public static Date getNextDay(Date date) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.DAY_OF_MONTH, -1);
      date = calendar.getTime();
      return date;
      }

    /**

    • 返回当月最后一天的日期
      */
      public static Date getLastDayOfMonth(Date date) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
      return calendar.getTime();
      }

    /**

    • 获取当前时间到凌晨还剩多少毫秒
      */
      public static long getMilliToDawn(){
      long milliSecondsLeftToday = 86400000 -
      org.apache.commons.lang3.time.DateUtils.getFragmentInMilliseconds(Calendar.getInstance(), Calendar.DATE);
      return milliSecondsLeftToday;
      }

    /**

    • 时间戳矫正(Java中标准的时间戳为13位)
      */
      public static long rectifyTimeStamp(long stateTime){
      //Java中标准的时间戳为13位
      String strTimeStamp = stateTime + “”;
      int len = strTimeStamp.length();
      for (int i = len; i < 13; i++) {
      strTimeStamp += “0”;
      }
      return Long.valueOf(strTimeStamp);
      }

    /**

    • 获取当前时间前一个月第一天
    • @return String
      */
      public static String getBeforeMonthFirstDay() {
      SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
      Calendar calendar1 = Calendar.getInstance();
      calendar1.add(Calendar.MONTH, -1);
      calendar1.set(Calendar.DAY_OF_MONTH,1);
      String firstDay = sdf.format(calendar1.getTime());
      return firstDay;
      }

    /**

    • 获取前一个月最后一天
    • @return String
      */
      public static String getBeforeMonthLastDay() {
      //获取前一个月最后一天
      SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
      Calendar calendar2 = Calendar.getInstance();
      calendar2.set(Calendar.DAY_OF_MONTH, 0);
      String lastDay = sdf.format(calendar2.getTime());
      return lastDay;
      }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值