Android examples for java.util:Day
add Day to date
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class Main{ public static final String YEAR = "yyyy"; public static final String MONTH = "MM"; public static final String DAY = "dd"; public static final String FULL_TIME = "HH:mm:ss"; public static final String YEAR_MONTH = YEAR + "-" + MONTH; public static final String FULL_DATE = YEAR_MONTH + "-" + DAY; public static final String FULL_DATE_TIME = FULL_DATE + " " + FULL_TIME; public static final String FULL_TIME_ZONE = FULL_DATE + "'T'" + FULL_TIME + "'Z'"; /**//from w w w. j a v a 2s . com * <br> * Convert date format * @param format * @param date * @return String */ private static String format(String format, Date date) { SimpleDateFormat f = new SimpleDateFormat(format); return f.format(date); } /** * <br> * From now to a few days ago or after few days * @param move * @return yyyy-MM-dd */ public static String addDay(int $add) { Calendar calendar = Calendar.getInstance(); return addDay(format(FULL_DATE, calendar.getTime()), $add, FULL_DATE); } /** * <br> * From now to a few days ago or after few days * @param $add * @param $format ?? ? format? ??? format? ??? ? * @return format */ public static String addDay(int $add, String $format) { Calendar calendar = Calendar.getInstance(); return addDay(format($format, calendar.getTime()), $add, $format); } /** * <br> * From 'dateStr' to a few days ago or after few days * @param dateStr * @param move days * @return yyyy-MM-dd */ public static String addDay(String $dateStr, int moveDays) { return addDay($dateStr, moveDays, FULL_DATE); } /** * <br> * From 'dateStr' to a few days ago or after few days * @param $dateStr * @param move days * @param $format * @return $format */ public static String addDay(String dateStr, int moveDays, String format) { return addDay(dateStr, moveDays, format, format); } /** * $dateStr? ?? $add ? ? <br> * @param $dateStr * @param $add * @param $inFormat ?? ? format * @param $outFormat ??? format * @return */ public static String addDay(String $dateStr, int $add, String $inFormat, String $outFormat) { try { Date date = new SimpleDateFormat($inFormat).parse($dateStr); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + $add); return format($outFormat, calendar.getTime()); } catch (ParseException e) { return $dateStr; } } }