Java Utility Library

Java Calendar - computeFields() Method



The java.util.Calendar.computeFields() method is used to convert the current millisecond time value time to calendar field values in fields[]. This allows you to sync up the calendar field values with a new time that is set for the calendar.

Syntax

protected abstract void computeFields()

Parameters

No parameter is required.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.Calendar.computeFields() method is used to convert the current millisecond time value time to calendar field values.

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(Calendar.YEAR, 2015);
    System.out.println("The new date is: " + Cal.getTime());

    //compute field 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: Thu Sep 10 05:38:56 UTC 2020
The new date is: Thu Sep 10 05:38:56 UTC 2015
The new date is: Thu Sep 10 05:38:56 UTC 2015

❮ Java.util - Calendar