当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Calendar ComputeFields()用法及代码示例


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

用法

protected abstract void computeFields()

参数

NA

返回

此方法不返回任何值。

抛出

NA

例子1

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

例子2

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

例子3

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

示例 4

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



相关用法


注:本文由纯净天空筛选整理自 Java Calendar ComputeFields() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。