Java时间工具类,获取本日、本月、本年、指定日期本日、本月、本年开始结束时间

 日常开发中难免遇到这种蛋疼的需求,于是粘贴了各种网站上的工具类,统一整理了一下。有需要的伙伴直接粘贴复制即可!感谢支持!

package com.agileboot.domain.datamanage.meteorological.db.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 应用模块名称:  获取指定日期 工具类
 * 代码描述:
 *
 * @author HelloAWei
 * @since 2023/9/20 11:53
 */
public class DateFormatUtil {
    /**
     * 获取当天的开始时间
     */
    public Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取当天的结束时间
     */
    public Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 获取昨天的开始时间
     */
    public Date getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * 获取昨天的结束时间
     */
    public Date getEndDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * 获取明天的开始时间
     */
    public Date getBeginDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    /**
     * 获取明天的结束时间
     */
    public Date getEndDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    /**
     * 获取本周的开始时间
     */
    @SuppressWarnings("unused")
    public Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    /**
     * 获取本周的结束时间
     */
    public Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    /**
     * 获取上周的开始时间
     */
    @SuppressWarnings("unused")
    public Date getBeginDayOfLastWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return getDayStartTime(cal.getTime());
    }

    /**
     * 获取上周的结束时间
     */
    public Date getEndDayOfLastWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfLastWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    /**
     * 获取本月的开始时间
     */
    public Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * 获取本月的结束时间
     */
    public Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * 获取上月的开始时间
     */
    public Date getBeginDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * 获取上月的结束时间
     */
    public Date getEndDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 2, day);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * 获取本年的开始时间
     */
    public Date getBeginDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);
        return getDayStartTime(cal.getTime());
    }

    /**
     * 获取本年的结束时间
     */
    public Date getEndDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DATE, 31);
        return getDayEndTime(cal.getTime());
    }

    /**
     * 获取某个日期的开始时间
     */
    public Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取某个日期的结束时间
     */
    public Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
            calendar.set(Calendar.MILLISECOND, 999);
        }
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取今年是哪一年
     */
    public Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    /**
     * 获取本月是哪一月
     */
    public int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    /**
     * 获取两个日期中的最大日期
     */
    public Date max(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return beginDate;
        }
        return endDate;
    }

    /**
     * 获取两个日期中的最小日期
     */
    public Date min(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return endDate;
        }
        return beginDate;
    }

    /**
     * 获取上一小时的开始时间
     */
    public Date getLastHourBegin() {
        Date date = new Date();
        date.setHours(date.getHours() - 1);
        date.setMinutes(00);
        date.setSeconds(00);
        return date;
    }

    /**
     * 获取上一小时的结束时间
     */
    public Date getLastHourEnd() {
        Date date = new Date();
        date.setHours(date.getHours() - 1);
        date.setMinutes(59);
        date.setSeconds(59);
        return date;
    }

    /**
     * 获取指定时间 当日的开始时间
     */
    public Date getDayBegin(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //把小时置为0
        cal.set(Calendar.HOUR_OF_DAY, 0);
        //把分钟置为0
        cal.set(Calendar.MINUTE, 0);
        //把秒置为0
        cal.set(Calendar.SECOND, 0);
        //把毫秒置为0
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取指定时间当日的结束时间
     */
    public Date getDayEnd(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //把小时置为23
        cal.set(Calendar.HOUR_OF_DAY, 23);
        //把分钟置为59
        cal.set(Calendar.MINUTE, 59);
        //把秒置为59
        cal.set(Calendar.SECOND, 59);
        //把毫秒置为999
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    /**
     * 获取指定时间当月的开始时间
     */
    public Date getMonthBegin(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //设置为1号,当前日期既为本月第一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        //将小时置为0
        cal.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟置为0
        cal.set(Calendar.MINUTE, 0);
        //将秒置为0
        cal.set(Calendar.SECOND, 0);
        //将毫秒置为0
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取指定时间当月的结束时间
     */
    public Date getMonthEnd(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //设置为当月最后一天
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时置为23
        cal.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟置为59
        cal.set(Calendar.MINUTE, 59);
        //将秒置为59
        cal.set(Calendar.SECOND, 59);
        //将毫秒置为999
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    /**
     * 获取指定时间当年的开始时间
     */
    public Date getYearBegin(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //设置月和日都为1,即为开始时间(注:月份是从0开始;日中0表示上个月最后一天,1表示本月开始第一天)
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        //将小时置为0
        cal.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟置为0
        cal.set(Calendar.MINUTE, 0);
        //将秒置为0
        cal.set(Calendar.SECOND, 0);
        //将毫秒置为0
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取指定时间当年的结束时间
     */
    public Date getYearEnd(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //设置月为12,月份从0开始
        cal.set(Calendar.MONTH, 11);
        //设置为当月最后一天
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时置为23
        cal.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟置为59
        cal.set(Calendar.MINUTE, 59);
        //将秒置为59
        cal.set(Calendar.SECOND, 59);
        //将毫秒置为999
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    public static void main(String[] args) throws ParseException {

//        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String time = "2023-11-13 22:10:00";
//        Date date = ft.parse(time);
//
//        System.out.println("获取规定时间的本月开始时间:" + ft.format(getMonthBegin(date)));
//        System.out.println("获取规定时间的本月结束时间:" + ft.format(getMonthEnd(date)));
//        System.out.println("-------------------------------------------------");

//        System.out.println("获取规定时间的本年开始时间:" + ft.format(getYearBegin(date)));
//        System.out.println("获取规定时间的本年结束时间:" + ft.format(getYearEnd(date)));
//        System.out.println("-------------------------------------------------");


//        //时
//        System.out.println("上一小时开始时间:" + ft.format(getLastHourBegin()));
//        System.out.println("上一小时结束时间:" + ft.format(getLastHourEnd()));
//        System.out.println("-------------------------------------------------");
//
//        //日
//        System.out.println("昨日开始时间:" + ft.format(getBeginDayOfYesterday()));
//        System.out.println("昨日结束时间:" + ft.format(getEndDayOfYesterDay()));
//        System.out.println("-------------------------------------------------");
//
//        System.out.println("今日开始时间:" + ft.format(getDayBegin()));
//        System.out.println("今日结束时间:" + ft.format(getDayEnd()));
//        System.out.println("-------------------------------------------------");
//
//        System.out.println("明日开始时间:" + ft.format(getBeginDayOfTomorrow()));
//        System.out.println("明日结束时间:" + ft.format(getEndDayOfTomorrow()));
//        System.out.println("-------------------------------------------------");
//
//
//        //周
//        System.out.println("本周开始时间" + ft.format(getBeginDayOfWeek()));
//        System.out.println("本周结束时间" + ft.format(getEndDayOfWeek()));
//        System.out.println("-------------------------------------------------");
//
//        System.out.println("上周开始时间" + ft.format(getBeginDayOfLastWeek()));
//        System.out.println("上周结束时间" + ft.format(getEndDayOfLastWeek()));
//        System.out.println("-------------------------------------------------");
//        //月
//        System.out.println("上月开始时间" + ft.format(getBeginDayOfLastMonth()));
//        System.out.println("上月结束时间" + ft.format(getEndDayOfLastMonth()));
//        System.out.println("-------------------------------------------------");


    }

}

你可能感兴趣的:(java,测试工具)