欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

Java GregorianCalendar add()方法与示例

发布时间:2025/3/11 java 30 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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()方法与示例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。