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

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    System.out.println("The current date is : " + cal.getTime());

    // add 20 days to the calendar
    cal.add(Calendar.DATE, 20);
    System.out.println("20 days later: " + cal.getTime());

    // subtract 2 months from the calendar
    cal.add(Calendar.MONTH, -2);/*from w w w  .ja  v a 2  s.com*/
    System.out.println("2 months ago: " + cal.getTime());

    // subtract 5 year from the calendar
    cal.add(Calendar.YEAR, -5);
    System.out.println("5 years ago: " + cal.getTime());
}

From source file:DateAddHour.java

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    System.out.println("Original = " + calendar.getTime());

    // Substract 2 hour from the current time
    calendar.add(Calendar.HOUR, -2);

    // Add 30 minutes to the calendar time
    calendar.add(Calendar.MINUTE, 30);

    // Add 300 seconds to the calendar time
    calendar.add(Calendar.SECOND, 300);
    System.out.println("Updated  = " + calendar.getTime());
}

From source file:CalendarManipulation.java

public static void main(String s[]) {
    Calendar cal = Calendar.getInstance();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM);

    System.out.println(df.format(cal.getTime()));

    cal.add(Calendar.DATE, 8);
    System.out.println(df.format(cal.getTime()));
}

From source file:CalendarManipulation.java

public static void main(String s[]) {
    Calendar cal = Calendar.getInstance();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM);

    System.out.println(df.format(cal.getTime()));

    cal.add(Calendar.HOUR, -4);
    System.out.println(df.format(cal.getTime()));
}

From source file:CalendarManipulation.java

public static void main(String s[]) {
    Calendar cal = Calendar.getInstance();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM);

    System.out.println(df.format(cal.getTime()));

    cal.add(Calendar.AM_PM, 1);
    System.out.println(df.format(cal.getTime()));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Calendar start = Calendar.getInstance();
    start.set(2015, 1, 21, 11, 1, 11);/*from   ww  w. ja  va 2 s.  c  o m*/
    Calendar end = Calendar.getInstance();

    Calendar clone = (Calendar) start.clone();
    int year = elapsed(clone, end, Calendar.YEAR);
    System.out.println(year + " year");
    clone.add(Calendar.YEAR, year);
    int month = elapsed(clone, end, Calendar.MONTH);
    clone.add(Calendar.MONTH, month);
    System.out.println(month + " month");

    int date = elapsed(clone, end, Calendar.DATE);
    clone.add(Calendar.DATE, date);
    System.out.println(date + " day");

    int hour = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 3600000;
    clone.add(Calendar.HOUR, hour);
    System.out.println(hour + " hour");

    int minute = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 60000;
    clone.add(Calendar.MINUTE, minute);
    System.out.println(minute + " minute");

    int millis = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 1000;
    System.out.println(millis + " millis");

}

From source file:Main.java

public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE) + "-"
            + now.get(Calendar.YEAR));

    now.add(Calendar.MONTH, 10);
    System.out.println("date after 10 months : " + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
            + "-" + now.get(Calendar.YEAR));
}

From source file:Main.java

public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE) + "-"
            + now.get(Calendar.YEAR));

    now.add(Calendar.YEAR, 1);
    System.out.println("date after one year : " + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
            + "-" + now.get(Calendar.YEAR));

}

From source file:Main.java

public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Current time : " + now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":"
            + now.get(Calendar.SECOND));

    now.add(Calendar.SECOND, 100);
    System.out.println("New time after adding 100 seconds : " + now.get(Calendar.HOUR_OF_DAY) + ":"
            + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));
}

From source file:Main.java

public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Current time : " + now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":"
            + now.get(Calendar.SECOND));

    now.add(Calendar.MINUTE, 20);
    System.out.println("New time after adding 20 minutes : " + now.get(Calendar.HOUR_OF_DAY) + ":"
            + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));

}