java时间操作工具类(判断时间是周几、多少号、几月、几年,计算时间差、计算时间相差几天,获取今天、昨天、近7天、本周、本月、本季、本年、去年开始时间和结束时间,获取本周、本月、本季、本年集合)

做数据统计时经常用的时间判断

    /**
     * 获得当前月开始时间
     * @return 结果
     */
    public static Date getCurrentMonthStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    /**
     * 获得当前月结束时间
     * @return 结果
     */
    public static Date getCurrentMonthEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        return cal.getTime();
    }
    /**
     * 获取当前季度开始时间
     * @return 结果
     */
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        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 cal = Calendar.getInstance();
        cal.setTime(getCurrentQuarterStartTime());
        cal.add(Calendar.MONTH, 3);
        return cal.getTime();
    }
    /**
     * 获得本周开始时间
     * @return 结果
     */
    public static Date getCurrentWeekStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    /**
     * 获得本周结束时间
     * @return 结果
     */
    public static Date getCurrentWeekEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentWeekStartTime());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        return cal.getTime();
    }
    /**
     * 获取当前年开始时间
     * @return 结果
     */
    public static Date getCurrentYearStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH + 1, cal.getActualMinimum(Calendar.YEAR));
        return cal.getTime();
    }

    /**
     * 获取本年结束时间
     * @return 结果
     */
    public static Date getCurrentYearEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentYearStartTime());
        cal.add(Calendar.YEAR, 1);
        return cal.getTime();
    }
    /**
     * 获取去年开始时间
     * @return 结果
     */
    public static Date getLastYearStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentYearStartTime());
        cal.add(Calendar.YEAR, -1);
        return cal.getTime();
    }

    /**
     * 获取去年结束时间
     * @return 结果
     */
    public static Date getLastYearEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentYearStartTime());
        cal.add(Calendar.YEAR, 0);
        return cal.getTime();
    }
    /**
     * 获取今天开始时间
     * @return 结果
     */
    public static Date getCurrentDayTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得昨天开始时间
     * @return 结果
     */
    public static Date getYesterdayTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date time = cal.getTime();
        cal.setTimeInMillis(time.getTime() - 3600 * 24 * 1000);
        return cal.getTime();
    }
    /**
     * 获得当天近7天时间
     * @return 结果
     */
    public static Date getWeekFromNow() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(getCurrentDayTime().getTime() - 3600 * 24 * 1000 * 7);
        return cal.getTime();
    }
    /**
     * 精准计算俩个时间差
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 结果
     */
    public static String computationTime(Date startTime, Date endTime) {
        try {
            long diff = endTime.getTime() - startTime.getTime();
            long day = diff / ND;
            long hour = diff % ND / NH;
            long min = diff % ND % NH / NM;
            long sec = diff % ND % NH % NM / 1000;
            String str = day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
            return str;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 计算俩个时间相差几天
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 结果
     */
    public static Long computationDayTime(Date startTime, Date endTime) {
        try {
            long diff = endTime.getTime() - startTime.getTime();
            long day = diff / ND;
            return day;
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 获取当前周集合
     * @return 结果
     */
    public static List<String> getWeekList() {
        List<String> weekList = new LinkedList<>();
        weekList.add("周一");
        weekList.add("周二");
        weekList.add("周三");
        weekList.add("周四");
        weekList.add("周五");
        return weekList;
    }

    /**
     * 获取当前月集合
     * @return 结果
     */
    public static List<String> getMonthList() {
        Long i = computationDayTime(getCurrentMonthStartTime(), getCurrentMonthEndTime());
        List<String> monthList = new ArrayList<>();
        for (int j = 1; j <= i; j++) {
            monthList.add(j + "日");
        }
        return monthList;
    }

    /**
     * 获取当前季集合
     * @return 结果
     */
    public static List<String> getCurrentQuarterList() {
        Date time = getCurrentQuarterStartTime();
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //获取本季的开始月份
        int month = cal.get(Calendar.MONTH) + 1;
        List<String> currentQuarterList = new ArrayList<>();
        for (int j = 0; j < 3; j++) {
            currentQuarterList.add(month + "月");
            month++;
        }
        return currentQuarterList;
    }

    /**
     * 获取本年集合
     * @return 结果
     */
    public static List<String> getYearList() {
        List<String> yearList = new ArrayList<>();
        for (int j = 1; j <= 12; j++) {
            yearList.add(j + "月");
        }
        return yearList;
    }
    /**
     * 判断当前时间是周几
     * @param time 时间
     * @return 结果
     */
    public static String dayForWeek(Date time) {
        Calendar cal = Calendar.getInstance();
        String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
        try {
            cal.setTime(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 指示一个星期中的某天。
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    //判断时间是多少号
    public static String timeForMonth(Date time) {
        Calendar cal = Calendar.getInstance();
        String[] monthDays = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
        try {
            cal.setTime(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_MONTH) - 1;
        if (w < 0) {
            w = 0;
        }
        return monthDays[w];
    }

    //判断时间是几月
    public static String equalsTimeForMonths(Date time) {
        Calendar cal = Calendar.getInstance();
        String[] months = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
        try {
            cal.setTime(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.MONTH);
        if (w < 0) {
            w = 0;
        }
        return months[w];
    }

    //判断时间是几年
    public static String equalsTimeForYear(Date time) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.YEAR);
        if (w < 0) {
            w = 0;
        }
        return String.valueOf(w);
    }
    //获取当前月有多少天
    private static Integer getMonthLastDay(Date date) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    //获取当前年有多少天
    private static Integer getYearLastDay(Date date) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
    }
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科杰智能制造

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值