Here you can find the source of addDaysToDate(String s, int day, String format)
Parameter | Description |
---|---|
s | input date |
day | date value |
format | date format |
Parameter | Description |
---|---|
ParseException | error info |
public static Date addDaysToDate(String s, int day, String format) throws ParseException
//package com.java2s; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Main { /**//from w w w . j a va 2s . c om * return add day to date object with system defined format. * @param s input date (yyyyMMdd) * @param day date value * @return Date addDays result * @throws ParseException error info */ public static Date addDaysToDate(String s, int day) throws ParseException { return addDaysToDate(s, day, "yyyyMMdd"); } /** * return add day to date object with user defined format. * @param s input date * @param day date value * @param format date format * @return Date addDAys result * @throws ParseException error info */ public static Date addDaysToDate(String s, int day, String format) throws ParseException { Date date = check(s, format); return addDaysToDate(date, day); } /** * return add day to date object * @param date input date * @param day date value * @return */ public static Date addDaysToDate(Date date, int day) { date.setTime(date.getTime() + ((long) day * 1000 * 60 * 60 * 24)); return date; } /** * * @param s date string you want to check. * @param format string representation of the date format. For example, "yyyy-MM-dd". * @return date Date * @throws ParseException error info */ public static Date check(String s, String format) throws ParseException { if (s == null) throw new ParseException("date string to check is null", 0); if (format == null) throw new ParseException("format string to check date is null", 0); SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA); Date date = null; try { date = formatter.parse(s); } catch (ParseException e) { throw new ParseException(" wrong date:\"" + s + "\" with format \"" + format + "\"", 0); } if (!formatter.format(date).equals(s)) throw new ParseException("Out of bound date:\"" + s + "\" with format \"" + format + "\"", 0); return date; } }