java.util.Calendar complete()方法

java.util.Calendar.complete() 方法是一种受保护的方法。该方法可由其类内的任何子类调用,但不能由不相关的类调用。它将填充日历字段中所有未设置的字段。调用ComputeTime() 和ComputeField() 来计算时间值和所有日历字段值。

1 语法

protected void complete() 

2 参数

3 返回值

4 示例1 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar complete()方法的例子
 */
import java.util.*;  
public class CalendarCompleteExample1 extends GregorianCalendar {  
   public static void main(String[] args) {  
      // create a new calendar  
      CalendarCompleteExample1 cal = new CalendarCompleteExample1();  
      // print the current date  
      System.out.println("The current date is : " + cal.getTime());  
      // clear the calendar  
      cal.clear();  
      // set a new year and call complete()  
      cal.set(GregorianCalendar.YEAR, 1998);  
      //protected method  
      cal.complete();  
  // print the current date  
      System.out.println("New date is : " + cal.getTime());  
   }  
} 

输出结果为:

The current date is : Tue Jul 31 19:56:09 PDT 2018
New date is : Thu Jan 01 00:00:00 PST 1998

5 示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar complete()方法的例子
 */
import java.util.*;  
public class CalendarCompleteExample2 extends GregorianCalendar {  
   public static void main(String[] args) {  
      // create a new calendar  
      CalendarCompleteExample2 cal = new CalendarCompleteExample2();  
      // print the current date  
      System.out.println("The current date is : " + cal.getTime());  
      // clear the calendar  
      cal.clear();  
      // set a new year and call complete()  
      cal.set(GregorianCalendar.MONTH, 10);  
      //protected method  
      cal.complete();  
      // print the current date  
      System.out.println("New Month is : " + cal.getTime());  
   }  
}   

输出结果为:

The current date is : Tue Jul 31 19:56:28 PDT 2018
New Month is : Sun Nov 01 00:00:00 PST 1970

6 示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar complete()方法的例子
 */
import java.util.*;  
public class CalendarCompleteExample3 extends GregorianCalendar {  
   public static void main(String[] args) {  
      // create a new calendar  
      CalendarCompleteExample3 cal = new CalendarCompleteExample3();  
      // print the current date  
      System.out.println("The current date is : " + cal.getTime());  
  
      // clear the calendar  
      cal.clear();  
      // set a new year and call complete()  
      cal.set(GregorianCalendar.DAY_OF_MONTH, 23);  
      //protected method  
      cal.complete();  
      // print the current date  
      System.out.println("New day is : " + cal.getTime());  
   }  
}  

输出结果为:

The current date is : Tue Jul 31 19:57:33 PDT 2018
New day is : Fri Jan 23 00:00:00 PST 1970

7 示例4

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar complete()方法的例子
 */
import java.util.*;  
public class CalendarCompleteExample4 extends GregorianCalendar {  
   public static void main(String[] args) {  
      // create a new calendar  
      CalendarCompleteExample4 cal = new CalendarCompleteExample4();  
      // print the current date  
      System.out.println("The current date is : " + cal.getTime());  
      // clear the calendar  
      cal.clear();  
      // set a new year and call complete()  
      cal.set(GregorianCalendar.DAY_OF_MONTH, 23);  
      cal.set(GregorianCalendar.MONTH, 9);  
      cal.set(GregorianCalendar.YEAR, 1996);  
      cal.set(GregorianCalendar.HOUR, 22);  
       cal.set(GregorianCalendar.MINUTE, 1);  
        cal.set(GregorianCalendar.SECOND, 0);  
      //protected method  
      cal.complete();  
      // print the current date  
      System.out.println("New DATE is : " + cal.getTime());  
   }  
} 

输出结果为:

The current date is : Tue Jul 31 20:01:19 PDT 2018
New DATE is : Wed Oct 23 22:01:00 PDT 1996

 

热门文章

优秀文章