各种时间的周期以及获取时间是第几个周,以及获得当前时间的某一天


    /**
     * 根据系统时间返回一串无符号字符串
     */
    public static String toString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return simpleDateFormat.format(date);
    }

    public static Date toDate(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sdf(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static String getMonthBeginTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        return sdf.format(c.getTime());
    }


    public static String getMonthEndTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sdf.format(c.getTime());
    }

    public static String getMonthBeginTimeOfLastYear() {

        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        c.add(Calendar.MONTH, 0);
        c.add(Calendar.YEAR, -1);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        return sdf.format(c.getTime());
    }


    public static String getMonthEndTimeOfLastYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.add(Calendar.YEAR, -1);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sdf.format(c.getTime());
    }


    public static String getToday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    public static String getYesterday() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -10);
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd");
        return startSdf.format(c.getTime());
    }


    /**
     * 获得昨天日期:yyyy-MM-dd  HH:mm:ss
     */
    public static String getYesterdayBeginTime() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return startSdf.format(c.getTime());
    }

    /**
     * 获得昨天日期:yyyy-MM-dd  HH:mm:ss
     */
    public static String getYesterdayEndTime() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        return startSdf.format(c.getTime());
    }

    public static String getTodayBeginTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return sdf.format(new Date());
    }


    public static String getTodayEndTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        return sdf.format(new Date());
    }

    public static String getNowDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    /**
     * 获得明天日期:yyyy-MM-dd  HH:mm:ss
     */
    public static String getTomorrowBeginTime() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 1);
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return startSdf.format(c.getTime());
    }

    /**
     * 获得明天日期:yyyy-MM-dd  HH:mm:ss
     */
    public static String getTomorrowEndTime() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 1);
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        return startSdf.format(c.getTime());
    }

    /**
     * 获得明天日期:yyyy-MM-dd  time
     */
    public static String getTomorrowTime(String time) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 1);
        SimpleDateFormat startSdf = new SimpleDateFormat(String.format("yyyy-MM-dd %s", time));
        return startSdf.format(c.getTime());
    }


    public static String getYearBeginTime() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-01-01 00:00:00");
        return sdf.format(c.getTime());
    }


    public static String getYearEndTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-12-31 23:59:59");
        Calendar c = Calendar.getInstance();
        return sdf.format(c.getTime());
    }

    public static String getYearBeginTimeOfLastYear() {

        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-01-01 00:00:00");
        c.add(Calendar.YEAR, -1);
        return sdf.format(c.getTime());
    }


    public static String getYearEndTimeOfLastYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-12-31 23:59:59");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1);
        return sdf.format(c.getTime());
    }

    //默认当天
//    public static Map<String, Integer> getWeekOfMonth() {
//        return DateUtil.getWeekOfMonth(DateUtil.getToday());
//    }

    //当前时间所属周,一周占用两个月的情况下属于上个月
    public static Map<String, Integer> getWeekOfMonth(String time) {
        //Calendar周日为一周的第一天
        //1.当月的1号为周日:
        //日期是否为周日 是,周数-1 第一个周日属于上个月最后一周
        //2.当月的1号为周一:
        //日期是否为周日 是,周数-1
        //3.当月的1号为周二--六:
        //日期是否为周日 是,周数-2 否,周数-1 为0属于上个月最后一周
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int resultYear;
        int resultMonth;
        int resultWeekOfMonth;
        Map<String, Integer> resultMap = new HashMap<>();
        try {
            Date parse = sdf.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(parse);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
            resultYear = year;
            resultMonth = month + 1;

            Calendar monthCalendar = Calendar.getInstance();
            monthCalendar.set(Calendar.YEAR, year);
            monthCalendar.set(Calendar.MONTH, month);
            monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
            int monthDayOfWeek = monthCalendar.get(Calendar.DAY_OF_WEEK);

            if (monthDayOfWeek == Calendar.SUNDAY) {
                if (dayOfWeek == Calendar.SUNDAY) {
                    if (dayOfMonth == 1) {
                        if (month == 0) {
                            resultWeekOfMonth = getLastWeekOfMonth(year - 1, 11);
                            resultYear = year - 1;
                            resultMonth = 12;
                        } else {
                            resultWeekOfMonth = getLastWeekOfMonth(year, month - 1);
                            resultMonth = month;
                        }
                    } else {
                        resultWeekOfMonth = weekOfMonth - 1;
                    }
                } else {
                    resultWeekOfMonth = weekOfMonth;
                }
            } else if (monthDayOfWeek == Calendar.MONDAY) {
                if (dayOfWeek == Calendar.SUNDAY) {
                    resultWeekOfMonth = weekOfMonth - 1;
                } else {
                    resultWeekOfMonth = weekOfMonth;
                }
            } else {
                if (dayOfWeek == Calendar.SUNDAY) {
                    resultWeekOfMonth = weekOfMonth - 2;
                } else {
                    resultWeekOfMonth = weekOfMonth - 1;
                }
                if (resultWeekOfMonth == 0) {
                    if (month == 0) {
                        resultWeekOfMonth = getLastWeekOfMonth(year - 1, 11);
                        resultYear = year - 1;
                        resultMonth = 12;
                    } else {
                        resultWeekOfMonth = getLastWeekOfMonth(year, month - 1);
                        resultMonth = month;
                    }
                }
            }
//            System.out.println(resultYear + "年" + resultMonth + "月"+ resultWeekOfMonth+"周");
            resultMap.put("year", resultYear);
            resultMap.put("month", resultMonth);
            resultMap.put("weekOfMonth", resultWeekOfMonth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    //按月度获取当月最后一周是第几周
    private static int getLastWeekOfMonth(int year, int month) {
        int result;
        //当月1号
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        //当月最后一天
        Calendar lastDayOfMonth = Calendar.getInstance();
        lastDayOfMonth.set(Calendar.YEAR, year);
        lastDayOfMonth.set(Calendar.MONTH, month);
        lastDayOfMonth.set(Calendar.DAY_OF_MONTH, 1);
        lastDayOfMonth.roll(Calendar.DAY_OF_MONTH, -1);

        //
        int lastWeekOfMonth = lastDayOfMonth.get(Calendar.WEEK_OF_MONTH);
        //
        int lastDayOfWeek = lastDayOfMonth.get(Calendar.DAY_OF_WEEK);
        //当月1号是周几
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        //如果1号是周日或周一,那么这个月的所有除了周日的日期用Calendar获取的周数与需要的周数是一致的;
        if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.MONDAY) {
            //如果最后一天是周日需要的周数要-1
            if (lastDayOfWeek == Calendar.SUNDAY) {
                result = lastWeekOfMonth - 1;
            } else {
                result = lastWeekOfMonth;
            }
        } else {//1号是周二,三,四,五,六的情况下,这个月的所有除了周日的日期用Calendar获取的周数-1后与需要的周数是一致的;
            //是周日的日期需要-2
            if (lastDayOfWeek == Calendar.SUNDAY) {
                result = lastWeekOfMonth - 2;
            } else {
                result = lastWeekOfMonth - 1;
            }
        }
        return result;
    }

    public static String getWeekBeginTime() {
        Date date = new Date();
        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);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return sdf.format(cal.getTime());
    }

    // 获取本周的结束时间
    public static String getWeekEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);

        cal.setTime(cal.getTime());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        return sdf.format(cal.getTime());
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值