📜  Java中的 GregorianCalendar add() 方法

📅  最后修改于: 2022-05-13 01:54:54.538000             🧑  作者: Mango

Java中的 GregorianCalendar add() 方法

Java.util.GregorianCalendar.add(int calendarfield, int time)是Java中 GregorianCalendar 类的内置方法。该函数接受一个日历字段和添加到该特定日历字段的时间量作为参数。根据日历规则,该方法基于其符号将指定时间量添加或减少到指定字段。

句法:

public void add(int calendarfield, int time)

参数:该函数接受两个强制性参数,如下所述:

  • calendarfield:要修改的日历字段。
  • time :要添加的时间量。

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

异常:如果calendarfield具有值ZONE_OFFSETDST_OFFSET或 unknown,或者如果任何日历字段具有超出范围的值,则该方法将引发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() 方法:
方案一:

// 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

方案二:

// 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: Java/util/GregorianCalendar.html#add()