Calendar日期操作
Calendar calendar = Calendar.getInstance();
//获取当前年份
int year = calendar.get(Calendar.YEAR);//获取当前月份
int month = calendar.get(Calendar.MONTH) + 1;//获取本月天数
int days= calendar.getActualMaximum(Calendar.DATE)
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
// 获取当前时间
String today = dateFormat.format(calendar.getTime());// 获取本月第一天
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
String fristDay = dateFormat.format(calendar.getTime());
private static final SimpleDateFormat SDF_YMD = new SimpleDateFormat("yyyy-MM-dd");
// 获取age个月前的第一天
private String getFirstDayOfMonthAge(Integer age) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, age);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return SDF_YMD.format(calendar.getTime());
}
// 获取age个月前的最后一天
private String getLastDayOfMonthAge(Integer age) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, age);
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DATE));
return SDF_YMD.format(calendar.getTime());
}