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()
相关用法
- Java GregorianCalendar getLeastMaximum()用法及代码示例
- Java GregorianCalendar getGreatestMinimum()用法及代码示例
- Java GregorianCalendar getMaximum()用法及代码示例
- Java GregorianCalendar equals()用法及代码示例
- Java GregorianCalendar hashCode()用法及代码示例
- Java GregorianCalendar getTimeZone()用法及代码示例
- Java GregorianCalendar setTimeZone()用法及代码示例
- Java GregorianCalendar clone()用法及代码示例
- Java GregorianCalendar isLeapYear()用法及代码示例
- Java GregorianCalendar getMinimum()用法及代码示例
- Java Java.util.GregorianCalendar用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
注:本文由纯净天空筛选整理自RICHIK BHATTACHARJEE大神的英文原创作品 GregorianCalendar add() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。