JAVA 获取某天、某周、某月、某年的开始时间和结束时间

import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    
    /**
     * 获取指定天的开始时间
     * @return
     */
    public static Date getStartTimeOfCurrentDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        setMinTimeOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * 获取指定天的结束时间
     * @return
     */
    public static Date getEndTimeOfCurrentDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        setMaxTimeOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * 获取指定周的开始时间
     * @return
     */
    public static Date getStartTimeOfCurrentWeek(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        setMinTimeOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * 获取指定月的结束时间
     * @return
     */
    public static Date getEndTimeOfCurrentWeek(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        setMaxTimeOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * 获取指定月的开始时间
     * @return
     */
    public static Date getStartTimeOfCurrentMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),1);
        setMinTimeOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * 获取指定月的结束时间
     * @return
     */
    public static Date getEndTimeOfCurrentMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        int maxMonthDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),maxMonthDay);
        setMaxTimeOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * 获取指定年的开始时间 注意月份要减1
     * @return
     */
    public static Date getStartTimeOfCurrentYear(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR),0,1);
        setMinTimeOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * 获取指定年的结束时间 注意月份要减1
     * @return
     */
    public static Date getEndTimeOfCurrentYear(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR),11,31);
        setMaxTimeOfDay(calendar);
        return calendar.getTime();
    }
    
   /**
     * 设置当天的开始时间
     * @param calendar
     */
    private static void setMinTimeOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    /**
     * 设置当天的结束时间
     * @param calendar
     */
    private static void setMaxTimeOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.MILLISECOND, 999);
    }
    

}

 

Date nowTime = new Date();
System.out.println(
"当前时间:"+nowTime); Date currentDayStartTime = DateUtil.getStartTimeOfCurrentDay(nowTime); Date currentDayEndTime = DateUtil.getEndTimeOfCurrentDay(nowTime); System.out.println("当天开始时间:"+currentDayStartTime+"当天结束时间:"+currentDayEndTime); Date currentWeekStartTime = DateUtil.getStartTimeOfCurrentWeek(nowTime); Date currentWeekEndTime = DateUtil.getEndTimeOfCurrentWeek(nowTime); System.out.println("当周开始时间:"+currentWeekStartTime+"当周结束时间:"+currentWeekEndTime); Date currentMonthStartTime = DateUtil.getStartTimeOfCurrentMonth(nowTime); Date currentMonthEndTime = DateUtil.getEndTimeOfCurrentMonth(nowTime); System.out.println("当月开始时间:"+currentMonthStartTime+"当月结束时间:"+currentMonthEndTime); Date currentYearStartTime = DateUtil.getStartTimeOfCurrentYear(nowTime); Date currentYearEndTime = DateUtil.getEndTimeOfCurrentYear(nowTime); System.out.println("当年开始时间:"+currentYearStartTime+"当年结束时间:"+currentYearEndTime);


结果如下:

当前时间:Wed Jun 23 10:27:03 CST 2021
当天开始时间:Wed Jun 23 00:00:00 CST 2021当天结束时间:Wed Jun 23 23:59:59 CST 2021
当周开始时间:Mon Jun 21 00:00:00 CST 2021当周结束时间:Sun Jun 27 23:59:59 CST 2021
当月开始时间:Tue Jun 01 00:00:00 CST 2021当月结束时间:Wed Jun 30 23:59:59 CST 2021
当年开始时间:Fri Jan 01 00:00:00 CST 2021当年结束时间:Fri Dec 31 23:59:59 CST 2021

 

 

 



posted @ 2021-06-23 09:47  bug毁灭者  阅读(4109)  评论(0编辑  收藏  举报