Calendar类中的roll(int calndr_field,boolean up_down)方法用于通过上下移动传递的字段单个时间单位来对传递的日历字段进行操作。这涉及在不更改较大字段的情况下对时间字段进行加法或减法。
用法:
public abstract void roll(int calndr_field, boolean up_down)
参数:该方法有两个参数:
- calndr_field:这是日历类型,是指要对其进行操作的日历字段。
- up_down:这是布尔类型,用于指示是向上或向下移动calndr_field还是增大或减小。 true表示添加时间单位,false表示减去时间单位。
返回值:该方法不返回任何值。
以下示例程序旨在说明Calendar类的roll()方法的用法:
示例1:
// Java code to illustrate
// isSet() method
import java.util.*;
public class Calendar_Demo {
public static void main(String args[])
{
// Creating a calendar
Calendar calndr = Calendar.getInstance();
// Displaying the year
System.out.println("The Current Year"
+ " is: "
+ calndr.get(
Calendar.YEAR));
// Decrementing the year
// false indicates subtraction
calndr.roll(Calendar.YEAR, false);
// Displaying the result after operation
System.out.println("The New Year is: "
+ calndr.get(
Calendar.YEAR));
// Incrementing the year
// true indicates addition
calndr.roll(Calendar.YEAR, true);
// Displaying the result after operation
System.out.println("The new year is: "
+ calndr.get(
Calendar.YEAR));
}
}
输出:
The Current Year is: 2019 The New Year is: 2018 The new year is: 2019
示例2:
// Java code to illustrate
// isSet() method
import java.util.*;
public class Calendar_Demo {
public static void main(String args[])
{
// Creating a calendar
Calendar calndr = Calendar.getInstance();
// Displaying the month
System.out.println("The Current Month"
+ " is: "
+ calndr.get(
Calendar.MONTH));
// Incrementing the month
// true indicates addition
calndr.roll(Calendar.MONTH, true);
// Displaying the result after operation
System.out.println("The New Month is: "
+ calndr.get(
Calendar.MONTH));
// Decrementing the month
// false indicates subtraction
calndr.roll(Calendar.MONTH, false);
// Displaying the result after operation
System.out.println("The new month is: "
+ calndr.get(
Calendar.MONTH));
}
}
输出:
The Current Month is: 1 The New Month is: 2 The new month is: 1
参考: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#roll-int-boolean-
相关用法
- Java Calendar roll(int calndr_field, int amt)用法及代码示例
- Java Calendar get()用法及代码示例
- Java Calendar set()用法及代码示例
- Java Calendar add()用法及代码示例
- Java Calendar computeFields()用法及代码示例
- Java Calendar internalGet()用法及代码示例
- Java Calendar setLenient()用法及代码示例
- Java Calendar setFirstDayOfWeek()用法及代码示例
- Java Calendar getActualMaximum()用法及代码示例
- Java Calendar getActualMinimum()用法及代码示例
- Java Calendar getTimeZone()用法及代码示例
- Java Calendar clear()用法及代码示例
- Java Calendar clone()用法及代码示例
- Java Calendar toString()用法及代码示例
- Java Calendar compareTo()用法及代码示例
注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 Calendar roll() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。