Java Utility Library

Java GregorianCalendar - computeTime() Method



The java.util.GregorianCalendar.computeTime() method is used to convert calendar field values to the time value (millisecond offset from the Epoch).

Syntax

protected void computeTime()

Parameters

No parameter is required.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.GregorianCalendar.computeTime() method is used to convert calendar field values to the time value (millisecond offset from the Epoch).

import java.util.*;

public class MyClass extends GregorianCalendar {
  public static void main(String[] args) {
    //creating a Calendar object
    MyClass Cal = new MyClass();

    //printing the current date
    System.out.println("The current date is: " + Cal.getTime());

    //set to a new year
    Cal.set(GregorianCalendar.YEAR, 2015);
    System.out.println("The new date is: " + Cal.getTime());

    //compute time and print date
    Cal.computeFields();
    System.out.println("The new date is: " + Cal.getTime());
  }
}

The output of the above code will be:

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

❮ Java.util - GregorianCalendar