當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java GregorianCalendar add()用法及代碼示例


java.util.GregorianCalendar.add(int calendarfield,int time)是Java中GregorianCalendar類的內置方法。該函數接受日曆字段和要添加到該特定日曆字段的時間量作為參數。根據日曆規則,該方法根據其符號在指定字段中添加或減去指定的時間量。

用法:

public void add(int calendarfield, int time)

參數:該函數接受兩個強製性參數,如下所述:


  • calendarfield:要修改的日曆字段。
  • time :要添加的時間量。

返回值:此方法沒有返回值。

異常:如果calendarfield的值為ZONE_OFFSET,DST_OFFSET或未知,或者任何日曆字段的值超出範圍,則此方法將引發IllegalArgumentException。

例子:

Current Date and Time : Mon Jul 23 12:46:05 UTC 2018

Input : calendarfied = GregorianCalendar.YEAR, time = 2
Output : Thu Jul 23 12:46:05 UTC 2020

Input : calendarfied = GregorianCalendar.MONTH, time = 16
Output : Sat Nov 23 12:46:45 UTC 2019

以下程序說明了Java中java.util.GregorianCalendar.add()方法的使用:
程序1:

// Java Program to demonstrate add() method 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Creating a new calendar 
        GregorianCalendar newcalendar = (GregorianCalendar) 
                           GregorianCalendar.getInstance(); 
  
        // Display the current date and time 
        System.out.println(" Current Date and Time : "
                           + newcalendar.getTime()); 
        // Adding two months to the current date 
        newcalendar.add((GregorianCalendar.MONTH), 2); 
  
        // Display the modified date and time 
        System.out.println(" Modified Date and Time : "
                           + newcalendar.getTime()); 
    } 
}
輸出:
Current Date and Time : Fri Aug 03 11:48:38 UTC 2018
 Modified Date and Time : Wed Oct 03 11:48:38 UTC 2018

程序2:

// Java Program to demonstrate add() method 
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Creating a new calendar 
        GregorianCalendar newcalendar = (GregorianCalendar) 
                           GregorianCalendar.getInstance(); 
  
        // Display the current date and time 
        System.out.println(" Current Date and Time : "
                           + newcalendar.getTime()); 
        // Adding twenty years to the current date 
        newcalendar.add((GregorianCalendar.YEAR), 20); 
        // Display the modified date and time 
        System.out.println(" Modified Date and Time : "
                           + newcalendar.getTime()); 
    } 
}
輸出:
Current Date and Time : Fri Aug 03 11:48:40 UTC 2018
 Modified Date and Time : Tue Aug 03 11:48:40 UTC 2038

程序3:

// Java Program to illustrate 
// GregorianCalendar.add() 
// function  
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Creating a new calendar 
        GregorianCalendar newcalendar = (GregorianCalendar) 
                           GregorianCalendar.getInstance(); 
  
        // Display the current date and time 
        System.out.println(" Current Date and Time : "
                           + newcalendar.getTime()); 
        // Deducting 16 months to the current date 
        newcalendar.add((GregorianCalendar.MONTH), -16); 
        // Display the modified date and time 
        System.out.println(" Modified Date and Time : "
                           + newcalendar.getTime()); 
  
        // Deducting twelve years to the current date 
        newcalendar.add((GregorianCalendar.MONTH), -12); 
        // Display the modified date and time 
        System.out.println(" Modified Date and Time : "
                           + newcalendar.getTime()); 
    } 
}
輸出:
Current Date and Time : Fri Aug 03 11:48:43 UTC 2018
 Modified Date and Time : Mon Apr 03 11:48:43 UTC 2017
 Modified Date and Time : Sun Apr 03 11:48:43 UTC 2016

參考: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#add()



相關用法


注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 GregorianCalendar add() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。