日期获取Calendar,获取本周第一天,本月第一天,往前7天,往前30的日期

获取每个月的第一天

	public static String getFirstDay(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDayOfMonth = calendar.getTime();
        return simpleDateFormat.format(firstDayOfMonth);
    }

获取每个月的最后一天

	public static String getLastDate(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);//加一个月
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date lastDayOfMonth = calendar.getTime();
        return simpleDateFormat.format(lastDayOfMonth);
    }

获取每周的第一天

	public static String getFirstOfWeek(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, 2);
        Date firstDayOfMonth = calendar.getTime();
        return simpleDateFormat.format(firstDayOfMonth);
    }

获取每周的最后一天

	public static String getLastOfWeek(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, 2);
        calendar.add(Calendar.WEEK_OF_MONTH,1);//加一周
        calendar.add(Calendar.DAY_OF_WEEK,-1);//在当前时间上减去天数
        Date firstDayOfMonth = calendar.getTime();
        return simpleDateFormat.format(firstDayOfMonth);
    }

获取当前时间减去7天

	public static String before7Days(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -7);//在当前时间上减去天数
        Date before7Days = calendar.getTime();
        return simpleDateFormat.format(before7Days);
    }

获取当前时间减去30天

	public static String before30Days(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -30);//在当前时间上减去天数
        Date before7Days = calendar.getTime();
        return simpleDateFormat.format(before7Days);
    }
发布了78 篇原创文章 · 获赞 29 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/lvhonglei1987/article/details/103685835