1. 月份、天数、年份加一
public static String addMonthOrDateOrYear(Date date) {
if (null == date) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date); //设置当前日期
c.add(Calendar.MONTH, 1); //月份加1月
//c.add(Calendar.DATE, 1); //天数份加1月
//c.add(Calendar.YEAR, 1); //年份加1月
date = c.getTime();
SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM");
return dateFm.format(date);
}
2. 根据时间(实例中将string格式的日期转化为标准日期),获取当前是星期几*
public static String getWeek(String time) throws Exception {
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date datetime = format1.parse(time);
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
return dateFm.format(datetime);
}
3. 获得本周的所有日期*
private static List<String> getWeekDay() {
Calendar calendar = Calendar.getInstance();
// 获取本周的第一天
int firstDayOfWeek = calendar.getFirstDayOfWeek();
List<String> list = new ArrayList<>();
for (int i = 0; i < 7; i++) {
calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + i);
String day = new SimpleDateFormat("MM-dd").format(calendar.getTime());
list.add(day);
}
return list;
}