Java中的日历完成()方法与示例
Calendar 类中的complete()方法用于填充日历字段的任何未设置字段。它遵循以下完成过程:
- 首先,如果尚未计算时间值,则调用 computeTime() 方法从日历字段值中计算时间值。
- 其次,要计算所有日历字段值,使用了 computeFields() 方法。
句法:
protected void complete()
参数:该方法不带任何参数。
返回值:该方法不返回任何值。
下面的程序说明了 Calendar 类的 complete() 方法的工作原理:
示例 1:
// Java Code to illustrate
// complete() Method
import java.util.*;
public class CalendarClassDemo
extends GregorianCalendar {
public static void main(String args[])
{
// Creating calendar object
CalendarClassDemo calndr = new CalendarClassDemo();
// Displaying the current date
System.out.println("The Current "
+ "date is: " + calndr.getTime());
// Clearing the calendar
calndr.clear();
// Set a new year and call
// complete()
calndr.set(GregorianCalendar.YEAR, 2008);
calndr.complete();
// Displaying the current date
System.out.println("The new"
+ " date is: " + calndr.getTime());
}
}
输出:
The Current date is: Wed Feb 13 15:39:33 UTC 2019
The new date is: Tue Jan 01 00:00:00 UTC 2008
示例 2:
// Java Code to illustrate
// complete() Method
import java.util.*;
public class CalendarClassDemo
extends GregorianCalendar {
public static void main(String args[])
{
// Creating calendar object
CalendarClassDemo calndr = new CalendarClassDemo();
// Displaying the current date
System.out.println("The Current "
+ "date is: " + calndr.getTime());
// Clearing the calendar
calndr.clear();
// Set a new year and call
// complete()
calndr.set(GregorianCalendar.YEAR, 1996);
calndr.complete();
// Displaying the current date
System.out.println("The new"
+ " date is: " + calndr.getTime());
}
}
输出:
The Current date is: Wed Feb 13 15:39:36 UTC 2019
The new date is: Mon Jan 01 00:00:00 UTC 1996
参考: https: Java/util/Calendar.html#complete()