第一个想到的是开源库,无奈,好用但是太大了600多kb o(╥﹏╥)o
implementation 'net.danlew:android.joda:2.9.5'
public static String plusDay(int num, String newDate) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
return DateTime.parse(newDate, formatter).plusDays(num).toString("yyyy-MM-dd");
}
开始找android的Calendar 类处理这个问题。
public static String plusDay(int num, String newDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
Date date = null;
try {
date = sdf.parse(newDate);
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(date);
rightNow.add(Calendar.DAY_OF_YEAR, +num);
Date time = rightNow.getTime();
String format = sdf.format(time);
return format;
} catch (ParseException e) {
e.printStackTrace();
}
return newDate;
}