欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Calendar类获取本天、本周、本月、本季度、半年度、本年度的起始时间

程序员文章站 2022-06-15 23:01:22
...

很多时候别人对你好,并不是因为别人喜欢你,而是因为他们喜欢对人好。

Calendar类获取本天、本周、本月、本季度、半年度、本年度的起始时间

构建TimeUtils工具类

public class TimeUtils {
 /**
     * 获取 当前年、半年、季度、月、日、小时 开始结束时间
     */
    private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
    private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 获得本周的第一天,周一
     * @return
     */
    public static Date getCurrentWeekDayStartTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    /**
     * 获得本周的最后一天,周日
     * @return
     */
    public static Date getCurrentWeekDayEndTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            c.add(Calendar.DATE, 8 - weekday);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    /**
     * 获得上周的第一天,周一
     * @return
     */
    public static Date getLastWeekDayStartTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday-7);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    /**
     * 获得上周的最后一天,周日
     * @return
     */
    public static Date getLastWeekDayEndTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            c.add(Calendar.DATE, 1 - weekday);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    /**
     * 获得本天的开始时间
     * @return
     */
    public static Date getCurrentDayStartTime() {
        Date now = new Date();
        try {
            now = shortSdf.parse(shortSdf.format(now));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 获得本天的结束时间
     * @return
     */
    public static Date getCurrentDayEndTime() {
        Date now = new Date();
        try {
            now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 获得昨日的开始时间
     * @return
     */
    public static Date getYesterdayStartTime() {
     Calendar c=Calendar.getInstance();
        Date now = new Date();
        c.setTime(now);
        c.add(Calendar.DATE, -1);
        Date yesterdayStart = null;
        try {
            yesterdayStart = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return yesterdayStart;
    }
    /**
     * 获得昨天的结束时间
     * @return
     */
    public static Date getYesterdayEndTime() {
     Calendar c=Calendar.getInstance();
        Date now = new Date();
        c.setTime(now);
        c.add(Calendar.DATE, -1);
        Date yesterdayEnd = null;
        try {
         yesterdayEnd = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return yesterdayEnd;
    }
    /**
     * 获得本小时的开始时间
     * @return
     */
    public static Date getCurrentHourStartTime() {
        Date now = new Date();
        try {
            now = longHourSdf.parse(longHourSdf.format(now));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 获得本小时的结束时间
     * @return
     */
    public static Date getCurrentHourEndTime() {
        Date now = new Date();
        try {
            now = longSdf.parse(longHourSdf.format(now) + ":59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 获得本月的开始时间
     * @return
     */
    public static Date getCurrentMonthStartTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 本月的结束时间
     * @return
     */
    public static Date getCurrentMonthEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            c.add(Calendar.MONTH, 1);
            c.add(Calendar.DATE, -1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 获得上月的开始时间
     * @return
     */
    public static Date getLastMonthStartTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
         int month=c.get(Calendar.MONTH);
         c.set(Calendar.MONTH, month-1);
            c.set(Calendar.DATE, 1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 上月的结束时间
     * @return
     */
    public static Date getLastMonthEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            c.add(Calendar.DATE, -1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前年的开始时间
     * @return
     */
    public static Date getCurrentYearStartTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前年的结束时间
     * @return
     */
    public static Date getCurrentYearEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.MONTH, 11);
            c.set(Calendar.DATE, 31);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前季度的开始时间
     * @return
     */
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
             c.set(Calendar.DATE, 1);
             now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前季度的结束时间
     * @return
     */
    public static Date getCurrentQuarterEndTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
     /**
     * 上一季度的开始时间
     * @return
     */
    public static Date getLastQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 9);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 6);
             c.set(Calendar.DATE, 1);
             now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 上一季度的结束时间
     * @return
     */
    public static Date getLastQuarterEndTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 获取前/后半年的开始时间
     * @return
     */
    public static Date getHalfYearStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 0);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 6);
            }
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 获取前/后半年的结束时间
     * @return
     */
    public static Date getHalfYearEndTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 获得一分钟后的时间
     * @param date
     * @param seconds
     * @return
     */
    public static Date addSeconds(Date date, int seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, seconds);
        return calendar.getTime();
    } 
    /**
     * 查询当前时间 - 本季度
     * @param date
     * @return
     */
   public static String assPeriodQuarter(Date date){
    Calendar c=Calendar.getInstance();
    c.setTime(date);
    int month=c.get(c.MONTH);
    if(month >= 0 && month <= 2){
     return "1-3";
    }
    if(month >= 3 && month <= 5){
     return "4-6";
    }
    if(month >= 6 && month <= 8){
     return "7-9";
    }
    if(month >= 9 && month <= 11){
     return "10-12";
    }
    return null;
   }  
   /**
    * 查询当前时间 - 半年度
    * @param date
    * @return
    */
   public static String assPeriodHalfYear(Date date){
    Calendar c=Calendar.getInstance();
    c.setTime(date);
    int month=c.get(c.MONTH);
    if(month >= 0 && month <= 5){
     return "1-6";
    }
    if(month >= 6 && month <= 11){
     return "7-12";
    }
    return null;
   }
 /**
     * 以当前时间为起点一周前时间点
     * @return
     */
    public static Date getCurrentTimeLastWeek() {
     Calendar c = Calendar.getInstance();
     Date date = new Date();
     c.setTime(date);
     c.add(Calendar.DATE, -7);
     Date yearCurrentTime=c.getTime();
  return yearCurrentTime;
    }
    /**
     * 以当前时间为起点一月前时间点
     * @return
     */
    public static Date getCurrentTimeLastMonth() {
     Calendar c = Calendar.getInstance();
     Date date = new Date();
     c.setTime(date);
     c.add(Calendar.MONTH, -1);
     Date yearCurrentTime=c.getTime();
  return yearCurrentTime;
    }
    /**
     * 以当前时间为起点半年前时间点
     * @return
     */
    public static Date getCurrentTimeLastHalfYear() {
     Calendar c = Calendar.getInstance();
     Date date = new Date();
     c.setTime(date);
     c.add(Calendar.MONTH, -6);
     Date halfYearCurrentTime=c.getTime();
  return halfYearCurrentTime;
    }
    /**
     * 以当前时间为起点一年前时间点
     * @return
     */
    public static Date getCurrentTimeLastYear() {
     Calendar c = Calendar.getInstance();
     Date date = new Date();
     c.setTime(date);
     c.add(Calendar.YEAR, -1);
     Date yearCurrentTime=c.getTime();
  return yearCurrentTime;
    }
   public static void main(String[] args) {
   	System.out.println("当前小时开始:"+getCurrentHourStartTime().toString());
   	System.out.println("当前小时结束:"+getCurrentHourEndTime().toString());
        System.out.println("当前天开始:"+getCurrentDayStartTime().toString());
        System.out.println("当前天时结束:"+getCurrentDayEndTime().toString());
        System.out.println("昨日开始:"+getYesterdayStartTime().toString());
        System.out.println("昨日结束:"+getYesterdayEndTime().toString());
        System.out.println("当前周开始:"+getCurrentWeekDayStartTime().toString());
        System.out.println("当前周结束:"+getCurrentWeekDayEndTime().toString());
        System.out.println("上周开始:"+getLastWeekDayStartTime().toString());
        System.out.println("上周结束:"+getLastWeekDayEndTime().toString());
        System.out.println("当前月开始:"+getCurrentMonthStartTime().toString());
        System.out.println("当前月结束:"+getCurrentMonthEndTime().toString());
        System.out.println("上月开始:"+getLastMonthStartTime().toString());
        System.out.println("上月结束:"+getLastMonthEndTime().toString());
        System.out.println("当前季度开始:"+getCurrentQuarterStartTime().toString());
        System.out.println("当前季度结束:"+getCurrentQuarterEndTime().toString());
        System.out.println("上季度开始:"+getLastQuarterStartTime().toString());
        System.out.println("上季度结束:"+getLastQuarterEndTime().toString());
        System.out.println("当前半年/后半年开始:"+getHalfYearStartTime().toString());
        System.out.println("当前半年/后半年结束:"+getHalfYearEndTime().toString());
        System.out.println("当前年开始:"+getCurrentYearStartTime().toString());
        System.out.println("当前年结束:"+getCurrentYearEndTime().toString());
        System.out.println("当前季度:"+assPeriodQuarter(new Date());
        System.out.println("当前半年度:"+assPeriodHalfYear(new Date());
        System.out.println("以当前时间为起点一周前时间点"+getCurrentTimeLastWeek().toString());
        System.out.println("以当前时间为起点一月前时间点"+getCurrentTimeLastMonth().toString());
        System.out.println("以当前时间为起点半年前时间点"+getCurrentTimeLastHalfYear().toString());
        System.out.println("以当前时间为起点一年前时间点"+getCurrentTimeLastYear().toString());
   }
   }

这里写的蛮好的https://blog.csdn.net/cjw1231/article/details/81541952

来都来了,点个赞再走呗!????????????
上天不能给到你想要的,不是你值得拥有更好的,而是你不配拥有。
Calendar类获取本天、本周、本月、本季度、半年度、本年度的起始时间