Example usage for java.util Calendar add

List of usage examples for java.util Calendar add

Introduction

In this page you can find the example usage for java.util Calendar add.

Prototype

public abstract void add(int field, int amount);

Source Link

Document

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

Usage

From source file:Main.java

/**
 * Check the schedule has expired or not
 * //w ww.  j a  v  a 2s  . c o m
 * @param endDate
 * @return
 */
public static boolean hasNotExpired(Date endDate) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_MONTH, -1);

    // Check expired date
    return (endDate == null || cal.getTime().before(endDate));
}

From source file:Main.java

public static Date addField(Date date, int field, int amount) {
    Calendar result = getCurrentCalendar(date);
    result.add(field, amount);
    return result.getTime();
}

From source file:Main.java

public static Calendar getNotificationCalendarNextDate(int hour, int minute) {
    Calendar currentCalendar = Calendar.getInstance();
    currentCalendar.add(Calendar.DATE, 1);
    currentCalendar.set(Calendar.HOUR_OF_DAY, hour);
    currentCalendar.set(Calendar.MINUTE, minute);
    return currentCalendar;

}

From source file:Main.java

public static void setTimerReceiver(Context context, int repeat_time, Class<?> class_name) {
    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intentReceiver = new Intent(context, class_name);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentReceiver,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, repeat_time / 1000);

    // InexactRepeating allows Android to optimize the energy consumption
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), repeat_time, pendingIntent);
}

From source file:Main.java

public static String getCurrMonthAfter(int monthDiff, String format) {
    SimpleDateFormat ft = new SimpleDateFormat(format);
    Calendar c = new GregorianCalendar();
    c.add(Calendar.MONTH, monthDiff);
    return ft.format(c.getTime());
}

From source file:Main.java

public static String previousDateString(String dateString) throws ParseException {
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    Date myDate = dateFormat.parse(dateString);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(myDate);/* ww w.j ava  2s  .  c om*/
    calendar.add(Calendar.DAY_OF_YEAR, -1);

    Date previousDate = calendar.getTime();
    String result = dateFormat.format(previousDate);

    return result;
}

From source file:Main.java

public static String getCurrentDateByOffset(String format, int calendarField, int offset) {
    String mDateTime = null;/*from w w  w .  j a  va  2s . c  om*/
    try {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        Calendar c = Calendar.getInstance();
        c.add(calendarField, offset);
        mDateTime = mSimpleDateFormat.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDateTime;
}

From source file:Main.java

public static String getYestoryDate() {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -1);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String yestoday = sdf.format(calendar.getTime());
    return yestoday;
}

From source file:Main.java

public static String getBeforeTodayWithFormat(int gap, String dateFormat) {
    Calendar currentCalendar = Calendar.getInstance();
    currentCalendar.add(Calendar.DATE, gap);

    Date date = currentCalendar.getTime();
    SimpleDateFormat format = new SimpleDateFormat(dateFormat);
    return format.format(date);
}

From source file:Main.java

/**
 * Given a calendar date, find a logical end time for an event.  Most likely one hour later.
 * @param fromCalendar//from   www .j a v a2  s. c o m
 * @return endCalendar
 */
public static Calendar getLogicalEnd(Calendar fromCalendar) {
    Calendar endCalendar = (Calendar) fromCalendar.clone();
    endCalendar.add(Calendar.HOUR_OF_DAY, 1);
    return endCalendar;
}