Add/set value to a field and get new calendar value

TypeMethodSummary
voidadd(int field, int amount)Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.
voidroll(int field, int amount)Adds the specified (signed) amount to the specified calendar field without changing larger fields.
voidset(int field, int value)Sets the given calendar field to the given value.
voidset(int year, int month, int date)Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
voidset(int year, int month, int date, int hourOfDay, int minute)Sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.
voidset(int year, int month, int date, int hourOfDay, int minute, int second)Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND.
voidsetFirstDayOfWeek(int value)Sets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.
voidsetLenient(boolean lenient)Specifies whether or not date/time interpretation is to be lenient.
voidsetMinimalDaysInFirstWeek(int value)Sets what the minimal days required in the first week of the year are; For example, if the first week is defined as one that contains the first day of the first month of a year, call this method with value 1.
voidsetTime(Date date)Sets this Calendar's time with the given Date.
voidsetTimeInMillis(long millis)Sets this Calendar's current time from the given long value.
voidsetTimeZone(TimeZone value)Sets the time zone with the given time zone value.
booleanisSet(int field)Determines if the given calendar field has a value set, including cases that the value has been set by internal fields calculations triggered by a get method call.

Add or substract days to current date


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();

    System.out.println("Month: " + (now.get(Calendar.MONTH) + 1));
    System.out.println("Date:"+ now.get(Calendar.DATE));
    System.out.println("Year:" + now.get(Calendar.YEAR));
    
    // add days to current date using Calendar.add method
    now.add(Calendar.DATE, 1);

    System.out.println("date after one day : ");
    System.out.println("Month: " + (now.get(Calendar.MONTH) + 1));
    System.out.println("Date:"+ now.get(Calendar.DATE));
    System.out.println("Year:" + now.get(Calendar.YEAR));
    
    // substract days from current date using Calendar.add method
    // by adding minus value
    now = Calendar.getInstance();
    now.add(Calendar.DATE, -1);

    System.out.println("Month: " + (now.get(Calendar.MONTH) + 1));
    System.out.println("Date:"+ now.get(Calendar.DATE));
    System.out.println("Year:" + now.get(Calendar.YEAR));

    
  }
}

The output:


Month: 10
Date:30
Year:2010
date after one day : 
Month: 10
Date:31
Year:2010
Month: 10
Date:29
Year:2010
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.