DateUtils

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import java.util.*;

/**
 * @author luo zhuo tao
 * @create 2023/5/8
 */
public class DateUtils {

    /**
     * 此处依赖糊涂包,可以有效减少代码
     * <dependency>
     * <groupId>cn.hutool</groupId>
     * <artifactId>hutool-all</artifactId>
     * <version>5.8.16</version>
     * </dependency>
     */


    /**
     * 判断时间是否在有效期内
     * @param newTime   判断时间
     * @param startTime 起始时间
     * @param endTime   截至时间
     * @param res       包含true 不包含false
     * @return
     */
    public static boolean period(Date newTime, Date startTime, Date endTime, boolean res) {
        boolean a;
        boolean b;
        if (res) {
            a = startTime.getTime() <= newTime.getTime();
            b = endTime.getTime() >= newTime.getTime();
        }else {
            a = startTime.getTime() < newTime.getTime();
            b = endTime.getTime() > newTime.getTime();
        }
        if (a && b) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * newTime比sourceTime大(或等)则返回true,反之false
     * @param newTime
     * @param sourceTime
     * @param res 包含true 不包含false
     * @return
     */
    public static boolean specific(Date newTime, Date sourceTime,boolean res) {
        if (res){
            return sourceTime.getTime() <= newTime.getTime();
        }else {
            return sourceTime.getTime() < newTime.getTime();
        }
    }


    /**
     * 计算两个时间之间的所有时间(年月日)
     *
     * @param start
     * @param end
     * @param res   false升序排序、true倒序排序
     * @return
     */
    public static List<String> getBetweenDates(Date start, Date end, boolean res) {
        List<String> result = new ArrayList<>();
        try {
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
                result.add(DateUtil.format(tempStart.getTime(), DatePattern.NORM_DATE_PATTERN));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (res) {
            Collections.reverse(result);
        }
        return result;

    }


    /**
     * 通过秒毫秒数判断两个时间的间隔的天数
     *
     * @param start
     * @param end
     * @return
     */
    public static int differenceDays(Date start, Date end) {
        int days = (int) ((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
        return days;
    }


    /**
     * 计算传入时间月份的天数,默认当前时间月份
     *
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        if (ObjectUtils.isEmpty(date)) {
            date = DateUtil.date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }


    /**
     * 在给定的日期加上或减去指定年后的日期
     *
     * @param sourceDate 原始时间
     * @param month      要调整的年份,向前为负数,向后为正数
     * @return
     */
    public static Date YEAR(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.YEAR, month);

        return c.getTime();
    }


    /**
     * 在给定的日期加上或减去指定月份后的日期
     *
     * @param sourceDate 原始时间
     * @param month      要调整的月份,向前为负数,向后为正数
     * @return
     */
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);

        return c.getTime();
    }


    /**
     * 在给定的日期加上或减去指定天数后的日期
     *
     * @param sourceDate 原始时间
     * @param month      要调整的天份,向前为负数,向后为正数
     * @return
     */
    public static Date stepDAY_OF_MONTH(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.DAY_OF_MONTH, month);

        return c.getTime();
    }




    /**
     * 获取上一个月1号
     *
     * @return
     */
    public static Date getLastMonth() {
        Date date = null;
        try {
            date = DateUtils.parseDate(DateUtil.format(stepMonth(new Date(), -1), DatePattern.NORM_MONTH_PATTERN).concat("-01"), DatePattern.NORM_MONTH_PATTERN);
        }catch (Exception e){
            e.printStackTrace();
        }
        return date;
    }



    /**
     * 获取本月1号
     *
     * @return
     */
    public static Date getCurrentMonth() {
        Date date = null;
        try {
             date = DateUtils.parseDate(DateUtil.format(DateUtil.date(), DatePattern.NORM_MONTH_PATTERN).concat("-01"), DatePattern.NORM_MONTH_PATTERN);
        }catch (Exception e){
            e.printStackTrace();
        }
        return date;
    }


    /**
     *
     * 获取下一个月1号
     *
     * @return
     */
    public static Date getPreMonth() {
        Date date = null;
        try {
            date = DateUtils.parseDate(DateUtil.format(stepMonth(new Date(), +1), DatePattern.NORM_MONTH_PATTERN).concat("-01"), DatePattern.NORM_MONTH_PATTERN);
        }catch (Exception e){
            e.printStackTrace();
        }
        return date;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值