Add/set value to a field and get new calendar value
void add(int field, int amount)
- Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.
void roll(int field, int amount)
- Adds the specified (signed) amount to the specified calendar field without changing larger fields.
void set(int field, int value)
- Sets the given calendar field to the given value.
void set(int year, int month, int date)
- Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
void set(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.
void set(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.
void setFirstDayOfWeek(int value)
- Sets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.
void setLenient(boolean lenient)
- Specifies whether or not date/time interpretation is to be lenient.
void setMinimalDaysInFirstWeek(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.
void setTime(Date date)
- Sets this Calendar's time with the given Date.
void setTimeInMillis(long millis)
- Sets this Calendar's current time from the given long value.
void setTimeZone(TimeZone value)
- Sets the time zone with the given time zone value.
boolean isSet(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
Home
Java Book
Essential Classes
Java Book
Essential Classes
Calendar:
- Calendar class
- Constants value in Calendar
- Create new Calendar instance
- Get field value from Calendar
- Compare two Calendar values
- Calendar and display name
- Is lenient
- Add/set value to a field and get new calendar value
- Convert Calendar value to string value