Java.util.GregorianCalendar 类

java.util.GregorianCalendar.computeTime() 方法用于将日历字段值转换为时间值(毫秒偏移量)来自纪元)。

语法

protected void computeTime()

参数

不需要参数。

返回值

void 类型。

异常

无。

示例:

在下面的示例中,java.util.GregorianCalendar.computeTime() 方法用于将日历字段值转换为时间值(距纪元的毫秒偏移量)。

import java.util.*;

public class MyClass extends GregorianCalendar {
  public static void main(String[] args) {
    //创建日历对象
    MyClass Cal = new MyClass();

    //打印当前日期
    System.out.println("The current date is: " + Cal.getTime());

    //设置为新的一年
    Cal.set(GregorianCalendar.YEAR, 2015);
    System.out.println("The new date is: " + Cal.getTime());

    //计算时间并打印日期
    Cal.computeFields();
    System.out.println("The new date is: " + Cal.getTime());
  }
}

输出上述代码将是:

The current date is: Tue Sep 08 07:11:57 UTC 2020
The new date is: Tue Sep 08 07:11:57 UTC 2015
The new date is: Tue Sep 08 07:11:57 UTC 2015