获取每个月的第一天
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);
}