Java教程

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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4