Java Utililty Methods Date Add

List of utility methods to do Date Add

Description

The list of methods to do Date Add are organized into topic(s).

Method

DatedateAdd(final Date baseDateTime, int daysToAdd, boolean chopTime)
Method dateAdd.
if (baseDateTime == null)
    throw new RuntimeException("Null date passed to Utils.dateAdd.");
Date dateTime = baseDateTime;
if (chopTime)
    dateTime = getMidnight(dateTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
calendar.add(Calendar.DATE, daysToAdd);
...
DatedateAdd(int datePart, int amount, Date date)
Method that returns the result of date + amount for the specified datePart using Calendar.get(int) for obtaining it
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(datePart, cal.get(datePart) + amount);
return cal.getTime();
DatedateAdd(int interval, Date date, int n)
date Add
long time = date.getTime() / 1000L;
switch (interval) {
case 1:
    time += n * 86400;
    break;
case 2:
    time += n * 604800;
    break;
...
StringDateAdd(String strDate, int iCount, int iType)
Date Add
Calendar Cal = parseDateTime(strDate);
int pType = 0;
if (iType == 0) {
    pType = 1;
} else if (iType == 1) {
    pType = 2;
} else if (iType == 2) {
    pType = 5;
...
StringDateAdd3Days(String _date)
Date Add Days
Calendar cl = Calendar.getInstance();
Date date = null;
try {
    date = (Date) simpleDateFormat.parse(_date);
} catch (ParseException e) {
    e.printStackTrace();
cl.setTime(date);
...
DatedateAddDay(Date date, int day)
date Add Day
if (date == null)
    return now();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, day);
return c.getTime();
StringDateAddDays(Date _date, int days)
Date Add Days
Calendar cl = Calendar.getInstance();
cl.setTime(_date);
cl.add(Calendar.DAY_OF_YEAR, days);
_date = cl.getTime();
return simpleDateFormat.format(_date);
DatedateAddDays(Date date, int days)
date Add Days
long now = date.getTime() + (long) days * DAY_IN_MILLISECOND;
return new Date(now);
DatedateAddDays(Date date, int days)
Add days to an existing date
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, days);
return cal.getTime();
StringdateAddDays(Date time, int days, String dateFormat)
date Add Days
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
long timeLong = time.getTime() / 1000 + 60l * 60 * 24 * days;
time.setTime(timeLong * 1000);
String timeStr = sdf.format(time);
return timeStr;