GregorianCalendar類的add()方法 (GregorianCalendar Class add() method)
add() method is available in java.util package.
add()方法在java.util包中可用。
add() method is used to add the given quantity to the specified GregorianCalendar field (fi).
add()方法用于將給定數量添加到指定的GregorianCalendar字段(fi)。
add() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
add()方法是一個非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。
add() method may throw an exception at the time of adding the given quantity to the given field (fi).
在將給定量添加到給定字段(fi)時, add()方法可能會引發異常。
IllegalArgumentException: This exception may throw when the given field (fi) is not in a range.
IllegalArgumentException :如果給定字段(fi)不在范圍內,則可能引發此異常。
Syntax:
句法:
public void add(int fi , int quantity);
Parameter(s):
參數:
int fi – represents the GregorianCalendar field.
int fi –表示GregorianCalendar字段。
int quantity – represents the quantity of time to be added to the given calendar field.
int數量 –表示要添加到給定日歷字段中的時間量。
Return value:
返回值:
The return type of this method is void, it returns nothing.
此方法的返回類型為void ,不返回任何內容。
Example:
例:
// Java program to demonstrate the example
// of void add(int fi , int quantity) method of
// GregorianCalendar
import java.util.*;
public class AddOfGregorianCalendar {
public static void main(String[] args) {
// Instantiating a GregorianCalendar object
GregorianCalendar g_ca = (GregorianCalendar) GregorianCalendar.getInstance();
// Display current GregorianCalendar
System.out.println("g_ca.getTime() : " + g_ca.getTime());
// By using add() method is to substract 3 years
// to the current GregorianCalendar
g_ca.add(GregorianCalendar.YEAR, -3);
// Display Update GregorianCalendar
System.out.println("g_ca.add(GregorianCalendar.YEAR, -3): " + g_ca.getTime());
// By using add() method is to add 5 months
// to the current GregorianCalendar
g_ca.add(GregorianCalendar.MONTH, 5);
// Display Update Calendar
System.out.println("g_ca.add(GregorianCalendar.MONTH, 3): " + g_ca.getTime());
// By using add() method is to substract 2 days
// to the current GregorianCalendar
g_ca.add(GregorianCalendar.DATE, -2);
// Display Update GregorianCalendar
System.out.println("g_ca.add(GregorianCalendar.DATE, -2): " + g_ca.getTime());
}
}
Output
輸出量
g_ca.getTime() : Sat Feb 15 06:51:12 GMT 2020
g_ca.add(GregorianCalendar.YEAR, -3): Wed Feb 15 06:51:12 GMT 2017
g_ca.add(GregorianCalendar.MONTH, 3): Sat Jul 15 06:51:12 GMT 2017
g_ca.add(GregorianCalendar.DATE, -2): Thu Jul 13 06:51:12 GMT 2017
翻譯自: https://www.includehelp.com/java/gregoriancalendar-add-method-with-example.aspx