java Date工具类 日期常用工具类

这篇文章介绍了JavaDateUtil类,包含了日期字符串转换、格式化、移动以及日期差值计算等实用方法,用于处理日期和时间的相关操作。
摘要由CSDN通过智能技术生成
package com.example.pushsfb.execute.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description
 * @create 2024/1/19 16:18
 **/
public class DateUtil {

    public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public final static String YYYY_MM_DD = "yyyy-MM-dd";
    public final static String HH_MM_SS = "HH:mm:ss";

    /**
     * 获取当前日期时间字符串
     * @return
     */
    public static String getNowDateTimeStr() {
        return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(new Date());
    }

    /**
     * 获取当前日期字符串
     * @return
     */
    public static String getNowDateStr() {
        return new SimpleDateFormat(YYYY_MM_DD).format(new Date());
    }

    /**
     * 获取当前时间字符串
     * @return
     */
    public static String getNowTimeStr() {
        return new SimpleDateFormat(HH_MM_SS).format(new Date());
    }

    /**
     * 字符串转换成日期
     * @param dateStr
     * @return
     */
    public static Date transferStrToDate(String dateStr) throws ParseException {
        return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).parse(dateStr);
    }

    /**
     * 字符串转换成日期
     * @param dateStr
     * @param dateFormat
     * @return
     */
    public static Date transferStrToDate(String dateStr, String dateFormat) throws ParseException {
        return new SimpleDateFormat(dateFormat).parse(dateStr);
    }

    /**
     * 日期转换成字符串
     * @param date
     * @return
     */
    public static String transferDateToStr(Date date) {
        return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(date);
    }

    /**
     * 日期转换成字符串
     * @param date
     * @param dateFormat
     * @return
     */
    public static String transferDateToStr(Date date, String dateFormat) {
        return new SimpleDateFormat(dateFormat).format(date);
    }

    /**
     * 日期移动天数
     * @param date
     * @param day
     * @return
     */
    public static Date dateMoveDay(Date date, double day) {
        return new Date(date.getTime() + (long) (day * 24 * 60 * 60 * 1000));
    }

    /**
     * 日期移动天数
     * @param date 不包含时间
     * @param day
     * @return
     */
    public static String dateMoveDayStr(String date, double day) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
        return simpleDateFormat.format(new Date(simpleDateFormat.parse(date).getTime() + (long) (day * 24 * 60 * 60 * 1000)));
    }

    /**
     * 日期移动天数
     * @param datetime 不包含时间
     * @param day
     * @return
     */
    public static String dateTimeMoveDayStr(String datetime, double day) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return simpleDateFormat.format(new Date(simpleDateFormat.parse(datetime).getTime() + (long) (day * 24 * 60 * 60 * 1000)));
    }

    /**
     * 日期移动小时
     * @param date
     * @param hour
     * @return
     */
    public static Date dateMoveHour(Date date, double hour) {
        return new Date(date.getTime() + (long) (hour * 60 * 60 * 1000));
    }

    /**
     * 日期移动小时
     * @param date
     * @param hour
     * @return
     */
    public static String dateMoveHourStr(String date, double hour) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return simpleDateFormat.format(new Date(simpleDateFormat.parse(date).getTime() + (long) (hour * 60 * 60 * 1000)));
    }

    /**
     * 日期移动分钟
     * @param date
     * @param minute
     * @return
     */
    public static Date dateMoveMinute(Date date, double minute) {
        return new Date(date.getTime() + (long) (minute * 60 * 1000));
    }

    /**
     * 日期移动分钟
     * @param date
     * @param minute
     * @return
     */
    public static String dateMoveMinuteStr(String date, double minute) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return simpleDateFormat.format(new Date(simpleDateFormat.parse(date).getTime() + (long) (minute * 60 * 1000)));
    }

    /**
     * 日期移动秒
     * @param date
     * @param second
     * @return
     */
    public static Date dateMoveSecond(Date date, int second) {
        return new Date(date.getTime() + ((long) second * 1000));
    }

    /**
     * 日期移动秒
     * @param date
     * @param second
     * @return
     */
    public static String dateMoveSecondStr(String date, int second) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return simpleDateFormat.format(new Date(simpleDateFormat.parse(date).getTime() + ((long) second * 1000)));
    }




    /**
     * 计算日期间相差天数-得整数
     * @param dateBigStr
     * @param dateSmallStr
     * @return
     */
    public static int getDayDiffInt(String dateBigStr, String dateSmallStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date dateBig = simpleDateFormat.parse(dateBigStr);
        Date dateSmall = simpleDateFormat.parse(dateSmallStr);
        long minSec = dateBig.getTime() - dateSmall.getTime();
        return (int) minSec/(1000 * 60 * 60 * 24);
    }


    /**
     * 计算日期间相差天数-得整数
     * @param dateBig
     * @param dateSmall
     * @return
     */
    public static int getDayDiffInt(Date dateBig, Date dateSmall) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        long minSec = dateBig.getTime() - dateSmall.getTime();
        return (int) minSec/(1000 * 60 * 60 * 24);
    }


    /**
     * 计算日期间相差小时数-精确两位小数
     * @param dateBigStr
     * @param dateSmallStr
     * @return
     */
    public static BigDecimal getDiffHourBigDecimal(String dateBigStr, String dateSmallStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date dateBig = simpleDateFormat.parse(dateBigStr);
        Date dateSmall = simpleDateFormat.parse(dateSmallStr);
        long minSec = dateBig.getTime() - dateSmall.getTime();
        return new BigDecimal(minSec).divide(new BigDecimal(1000 * 60 * 60), 2, RoundingMode.HALF_UP);
    }


    /**
     * 计算日期间相差小时数-精确两位小数
     * @param dateBig
     * @param dateSmall
     * @return
     */
    public static BigDecimal getDiffHourBigDecimal(Date dateBig, Date dateSmall) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        long minSec = dateBig.getTime() - dateSmall.getTime();
        return new BigDecimal(minSec).divide(new BigDecimal(1000 * 60 * 60), 2, RoundingMode.HALF_UP);
    }


    /**
     * 计算日期间相差小时数-得整数
     * @param dateBigStr
     * @param dateSmallStr
     * @return
     */
    public static int getDiffHourInt(String dateBigStr, String dateSmallStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date dateBig = simpleDateFormat.parse(dateBigStr);
        Date dateSmall = simpleDateFormat.parse(dateSmallStr);
        long minSec = dateBig.getTime() - dateSmall.getTime();
        return (int) minSec/(1000 * 60 * 60);
    }


    /**
     * 计算日期间相差小时数-得整数
     * @param dateBig
     * @param dateSmall
     * @return
     */
    public static int getDiffHourInt(Date dateBig, Date dateSmall) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        long minSec = dateBig.getTime() - dateSmall.getTime();
        return (int) minSec/(1000 * 60 * 60);
    }


    /**
     * 计算日期间相差分钟数-得整数
     * @param dateBigStr
     * @param dateSmallStr
     * @return
     */
    public static int getDiffMinuteInt(String dateBigStr, String dateSmallStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date dateBig = simpleDateFormat.parse(dateBigStr);
        Date dateSmall = simpleDateFormat.parse(dateSmallStr);
        long minSec = dateBig.getTime() - dateSmall.getTime();
        return (int) minSec/(1000 * 60);
    }


    /**
     * 计算日期间相差分钟数-得整数
     * @param dateBig
     * @param dateSmall
     * @return
     */
    public static int getDiffMinuteInt(Date dateBig, Date dateSmall) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        long minSec = dateBig.getTime() - dateSmall.getTime();
        return (int) minSec/(1000 * 60);
    }


    /**
     * 计算日期间相差秒数-得整数
     * @param dateBigStr
     * @param dateSmallStr
     * @return
     */
    public static int getDiffSecondInt(String dateBigStr, String dateSmallStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date dateBig = simpleDateFormat.parse(dateBigStr);
        Date dateSmall = simpleDateFormat.parse(dateSmallStr);
        long minSec = dateBig.getTime() - dateSmall.getTime();
        return (int) minSec/(1000);
    }


    /**
     * 计算日期间相差秒数-得整数
     * @param dateBig
     * @param dateSmall
     * @return
     */
    public static int getDiffSecondInt(Date dateBig, Date dateSmall) {
        long minSec = dateBig.getTime() - dateSmall.getTime();
        return (int) minSec/(1000);
    }


    /**
     * 日期比较
     * @param dateBig
     * @param dateSmall
     * @return
     */
    public static boolean dateCompare(Date dateBig, Date dateSmall) {
        return dateBig.after(dateSmall);
    }


    /**
     * 日期比较
     * @param datetimeBigStr
     * @param datetimeSmallStr
     * @return
     */
    public static boolean datetimeStrCompare(String datetimeBigStr, String datetimeSmallStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return simpleDateFormat.parse(datetimeBigStr).after(simpleDateFormat.parse(datetimeSmallStr));
    }

    public static void main(String[] args) throws ParseException {
//        System.out.println(transferStrToDate("2024-01-11 07:33:00"));
//        System.out.println(getDayDiffInt("2024-01-11 07:000:00","2024-01-10 07:000:00"));
//        System.out.println(getDiffHourInt("2024-01-11 07:000:00","2024-01-10 07:000:00"));
//        System.out.println(getDiffHourBigDecimal("2024-01-11 07:33:00","2024-01-10 07:00:00"));
//        System.out.println(getDiffMinuteInt("2024-01-11 07:33:00","2024-01-11 07:00:00"));
//        System.out.println(getDiffSecondInt("2024-01-11 07:33:33","2024-01-11 07:33:00"));
//        System.out.println(getNowDateStr());
//        System.out.println(getNowDateTimeStr());
//        System.out.println(getNowTimeStr());
//        System.out.println(dateMoveDayStr("2024-01-11", 3));
//        System.out.println(dateTimeMoveDayStr("2024-01-11 07:33:00", 2));
//        System.out.println(dateMoveHourStr("2024-01-11 07:33:00", -2.5));
//        System.out.println(dateMoveMinuteStr("2024-01-11 07:33:00", -2.5));
//        System.out.println(dateMoveSecondStr("2024-01-11 07:33:00", -2));
//        System.out.println(dateMoveSecondStr("2024-01-11 07:33:00", 2));
//        System.out.println(datetimeStrCompare("2024-01-11 07:33:33",dateTimeMoveDayStr("2024-01-11 07:33:00", 1)));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我想出去玩-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值