java.util.Calendar computeFields()方法

java.util.Calendar.computeFields() 方法是受保护的抽象方法。它将当前毫秒时间值转换为fields []中的日历字段值。这可以与为日历对象设置的新时间与日历字段值同步。

1 语法

protected abstract void computeFields()  

2 参数

3 返回值

4 示例1 

package com.yiidian;

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

输出结果为:

The current date is : Tue Jul 31 20:06:08 PDT 2018
New date is : Thu Jan 01 00:00:00 PST 3018
New date is : Thu Jan 01 00:00:00 PST 3018

5 示例2

package com.yiidian;

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

输出结果为:

The current date is : Tue Jul 31 20:14:45 PDT 2018
New date is : Sun Nov 01 00:00:00 PST 1970
New date is : Sun Nov 01 00:00:00 PST 1970

6 示例3

package com.yiidian;

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

输出结果为:

The current date is : Tue Jul 31 20:19:24 PDT 2018
New date is : Sat Jan 10 00:00:00 PST 1970
New date is : Sat Jan 10 00:00:00 PST 1970

7 示例4

package com.yiidian;

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

输出结果为:

The current date is : Tue Jul 31 20:21:45 PDT 2018
New date is : Thu Jan 01 23:16:40 PST 1970
New date is : Thu Jan 01 23:16:40 PST 1970

 

热门文章

优秀文章