Java GregorianCalendar add()方法与示例
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 objectGregorianCalendar g_ca = (GregorianCalendar) GregorianCalendar.getInstance();// Display current GregorianCalendarSystem.out.println("g_ca.getTime() : " + g_ca.getTime());// By using add() method is to substract 3 years// to the current GregorianCalendarg_ca.add(GregorianCalendar.YEAR, -3);// Display Update GregorianCalendarSystem.out.println("g_ca.add(GregorianCalendar.YEAR, -3): " + g_ca.getTime());// By using add() method is to add 5 months// to the current GregorianCalendarg_ca.add(GregorianCalendar.MONTH, 5);// Display Update CalendarSystem.out.println("g_ca.add(GregorianCalendar.MONTH, 3): " + g_ca.getTime());// By using add() method is to substract 2 days// to the current GregorianCalendarg_ca.add(GregorianCalendar.DATE, -2);// Display Update GregorianCalendarSystem.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
總結
以上是生活随笔為你收集整理的Java GregorianCalendar add()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大数据数据可视化设计原则_数据可视化设计
- 下一篇: 数字和数字根的总和_使用8086微处理器