Android Utililty Methods Calendar Add

List of utility methods to do Calendar Add

Description

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

Method

Calendaradd(int field, long time, int val)
Useful routine to add given a number of units to specifed part of given time.
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
c.add(field, val);
return c;
DateaddDay(Calendar c, int days)
add Day
if (c == null)
    return null;
c.add(Calendar.DAY_OF_MONTH, days);
return c.getTime();
DateaddMonth(Calendar c, int months)
add Month
if (c == null)
    return null;
c.add(Calendar.MONTH, months);
return c.getTime();
Calendarafter(Calendar c, long offset)
after
Calendar calendar = null;
if (c != null) {
    calendar = c;
} else {
    calendar = Calendar.getInstance();
calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
return calendar;
...
CalendarafterNDays(Calendar c, int n)
after N Days
long offset = n * 24 * 60 * 60 * 1000;
Calendar calendar = null;
if (c != null) {
    calendar = c;
} else {
    calendar = Calendar.getInstance();
calendar.setTimeInMillis(calendar.getTimeInMillis() + offset);
...
DategetDateAdd(Date date, int amount)
get Date Add
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(GregorianCalendar.DATE, amount);
return cal.getTime();
StringgetDateByAddFltHour(float flt)
get Date By Add Flt Hour
int addMinute = (int) (flt * 60);
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
cal.add(GregorianCalendar.MINUTE, addMinute);
return getFormatDateTime(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
StringgetDateByAddHour(String datetime, int minute)
get Date By Add Hour
String returnTime = null;
Calendar cal = new GregorianCalendar();
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
try {
    date = ft.parse(datetime);
    cal.setTime(date);
    cal.add(GregorianCalendar.MINUTE, minute);
...
StringgetFormatCurrentAdd(int amount, String format)
get Format Current Add
Date d = getDateAdd(new Date(), amount);
return getFormatDateTime(d, format);
Calendartomorrow(Calendar c)
tomorrow
long offset = 1 * 24 * 60 * 60 * 1000;
Calendar calendar = null;
if (c != null) {
    calendar = c;
} else {
    calendar = Calendar.getInstance();
calendar.setTimeInMillis(calendar.getTimeInMillis() + offset);
...