當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。