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