java.util.Calendar clear()方法

java.util.Calendar.add() 方法设置给定的日历字段值和未定义此Calendar的时间值。Calendar实现类可以将默认字段值用于日期和时间计算。

1 语法

public final void clear(int field)  

2 参数

field:设置为未定义的日历字段。

3 返回值

4 示例1 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar clear()方法的例子
 */
import java.util.Calendar;  
public class CalendarClearExample1 {      
   public static void main(String[] args) {  
      // create a calendar  
       Calendar cal = (Calendar) Calendar.getInstance();  
      // display the current date and time  
      System.out.println("Current Calendar Date: " + cal.getTime());  
      // use clear method to set year as undefined.  
       cal.clear(Calendar.YEAR);  
      // print the result  
     System.out.println("The calendar shows : " + cal.getTime());        
   }  
} 

输出结果为:

Current Calendar Date: Mon Jul 30 06:35:54 PDT 2018
The calendar shows : Thu Jul 30 06:35:54 PDT 1970

5 示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar clear()方法的例子
 */
import java.util.Calendar;  
public class CalendarclearExample2 {     
 public static void main(String[] args) {  
      // create a calendar  
   Calendar cal = (Calendar) Calendar.getInstance();  
      // display the current date and time  
      System.out.println("Current Calendar Date: " + cal.getTime());        
      // use clear method to set month as undefined.  
      cal.clear(Calendar.MONTH);  
      // print the result  
      System.out.println("The calendar shows : " + cal.getTime());  
   }  
}  

输出结果为:

Current Calendar Date: Mon Jul 30 06:39:17 PDT 2018
The calendar shows : Tue Jan 30 06:39:17 PST 2018

6 示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar clear()方法的例子
 */
import java.util.Calendar;  
public class CalendarClearExample3 {     
 public static void main(String[] args) {  
      // create a calendar  
      Calendar cal = (Calendar) Calendar.getInstance();  
      // display the current date and time  
      System.out.println("Current Calendar Date: " + cal.getTime());       
      // use clear method to set month as undefined.  
      cal.clear(Calendar.DAY_OF_MONTH);  
      // print the result  
      System.out.println("The calendar shows : " + cal.getTime());        
      cal.clear(Calendar.HOUR);  
      cal.clear(Calendar.MINUTE);  
      cal.clear(Calendar.SECOND);  
      // print the result  
      System.out.println("The calendar shows : " + cal.getTime());  
   }  
}  

输出结果为:

Current Calendar Date: Mon Jul 30 06:45:15 PDT 2018
The calendar shows : Mon Jul 30 06:45:15 PDT 2018
The calendar shows : Mon Jul 30 06:00:00 PDT 2018 

7 示例4

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar clear()方法的例子
 */
import java.util.Calendar;  
public class  CalendarClearExample4 {      
public static void main(String[] args) {  
      // create a calendar  
      Calendar cal = (Calendar) Calendar.getInstance();  
     // display the current date and time  
      System.out.println("Current Calendar Date: " + cal.getTime());       
      cal.clear(Calendar.HOUR);  
      cal.clear(Calendar.MINUTE);  
      cal.clear(Calendar.SECOND);  
      // print the result  
      System.out.println("The calendar shows : " + cal.getTime());  
   }  
}  

输出结果为:

Current Calendar Date: Mon Jul 30 06:48:47 PDT 2018
The calendar shows : Mon Jul 30 06:00:00 PDT 2018

 

热门文章

优秀文章