GregorianCalendar类roll()方法 (GregorianCalendar Class roll() method)
Syntax:
句法:
public void roll(int fi, boolean status);
public void roll(int fi, int amt);
roll() method is available in java.util package.
roll()方法在java.util包中可用。
roll(int fi, boolean status) method is used to roll up or down a single quantity of time on the given time field (fi).
roll(int fi,boolean status)方法用于在给定的时间字段(fi)上上下滚动单个时间量。
roll(int fi, int amt) method is used to roll up or down the given time field (fi) with the given amt (amount) of time.
roll(int fi,int amt)方法用于以给定的amt(时间)来向上或向下滚动给定的时间字段(fi)。
These methods may throw an exception at the time of rolling the given field (fi).
这些方法在滚动给定字段(fi)时可能会引发异常。
IllegalArgumentException: This exception may throw when the given field (fi) is not in a range in non-lenient mode.
IllegalArgumentException :当给定字段(fi)不在非宽松模式下的范围内时,可能引发此异常。
These are non-static methods, so it is accessible with the class object and if we try to access these methods with the class name then we will get an error.
这些是非静态方法,因此可以通过类对象进行访问,如果尝试使用类名称访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, roll(int fi, boolean status)
在第一种情况下, roll(int fi,boolean status)
- int fi – represent the calendar field.
- int fi –代表日历字段。
- boolean status – sets true when the given field (fi) roll up otherwise it sets false.
- 布尔状态 -当给定字段(fi)汇总时设置为true,否则设置为false。
In the first case, roll(int fi, int amt)
在第一种情况下, roll(int fi,int amt)
- int fi – represent the calendar field.
- int fi –代表日历字段。
- int amt – represents the amount(amt) to add or substract to field (positive amt to add & negative amt to substract).
- int amt –表示要添加或减去到字段的数量(金额)(正amt要添加或负amt要减去)。
Return value:
返回值:
In both the cases, the return type of the method is void, it does not return anything.
在这两种情况下,方法的返回类型均为void ,它不返回任何内容。
Example:
例:
// Java program is to demonstrate the example of
// roll() method of GregorianCalendar
import java.util.*;
public class RollOfGregorianCalendar {
public static void main(String[] args) {
// Instantiating a GregorianCalendar object
GregorianCalendar g_ca = (GregorianCalendar) GregorianCalendar.getInstance();
// Display current GregorianCalendar
System.out.println("g_ca.getTime(): " + g_ca.getTime());
// By using roll(int,boolean) method is to
// up a single unit of the given field if
// boolean sets to true
g_ca.roll(GregorianCalendar.YEAR, true);
// Display Updated GregorianCalendar
System.out.println("g_ca.roll(GregorianCalendar.YEAR, true): " + g_ca.getTime());
// By using roll(int,int) method is to up
// or down the given field with the given
// amount of time
g_ca.roll(GregorianCalendar.MONTH, 5);
// Display Updated Calendar
System.out.println("g_ca.roll(GregorianCalendar.MONTH, 5): " + g_ca.getTime());
}
}
Output
输出量
g_ca.getTime(): Sat Feb 15 12:41:19 GMT 2020
g_ca.roll(GregorianCalendar.YEAR, true): Mon Feb 15 12:41:19 GMT 2021
g_ca.roll(GregorianCalendar.MONTH, 5): Thu Jul 15 12:41:19 GMT 2021
翻译自: https://www.includehelp.com/java/gregoriancalendar-roll-method-with-example.aspx