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 : calendarfield = GregorianCalendar.YEAR, time = 2
Output : Thu Jul 23 12:46:05 UTC 2020
Input : calendarfield = GregorianCalendar.MONTH, time = 16
Output : Sat Nov 23 12:46:45 UTC 2019
下面的程序说明了java.util.GregorianCalendar.add()方法在Java中的应用:
程序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()