• 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



  • 相关阅读:
    QML的一些基础的区分
    qml的一个文章----可以看出状态、动画的使用
    凡是人性的,都是如下的
    全国经纬度,具体到县
    web-nodkit 入门
    一个文章-转年收入50万美元的软件工程师做的是什么类型的工作
    qml 封装技巧-利用数据来进行适配
    windbg内核诊断方式--转载
    Windbg程序调试--转载
    编写你自己的单点登录(SSO)服务
  • 原文地址:https://www.cnblogs.com/guliang/p/14921411.html
Copyright © 2020-2023  润新知