.computeFields()
chip01889226953 total contributions
Published Sep 11, 2023
Contribute to Docs
The .computeFields()
method of the Calendar
class converts the current millisecond time value to the relevant field values in the Calendar
object.
Syntax
calendar.computeFields()
Note:
computeFields()
method is a non-static method, it is accessible with theCalendar
instance. Trying to access it with the class name throws an error.
Example
In the example below the CalendarComputeFieldExample
class is instantiated and the calendar is set to the year 2024
. Then, the computeFields()
method is called to sync up the Calendar
field values with the new time that has been set for the Calendar
object.
import java.util.*;public class CalendarComputeFieldExample extends GregorianCalendar {public static void main(String[] args) {// Instantiate a new Calendar objectCalendarComputeFieldExample cal = new CalendarComputeFieldExample();// Print the current dateSystem.out.println("Current date is: " + cal.getTime());// Clear the calendarcal.clear();// Set a new year and call computeFields()cal.set(GregorianCalendar.YEAR, 2024);System.out.println("New date is: " + cal.getTime());cal.computeFields();// Print the current dateSystem.out.println("New date is: " + cal.getTime());}}
This results in the following output:
Current date is: Fri Aug 18 20:54:51 GMT 2023New date is: Mon Jan 01 00:00:00 GMT 2024New date is: Mon Jan 01 00:00:00 GMT 2024
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.