Calendar computeFields() method in Java with Examples
The computeFields() method in Calendar class is used for the conversion operation of the current millisecond time value time to calendar field values mentioned in the fields[].
Note: This allows syncing up the new time that is set for the calendar object with the calendar field values.
Syntax:
protected abstract void computeFields()
Parameters: The method does not take any parameters.
Return Value: The method does not return any value.
Below programs illustrate the working of computeFields() Method of Calendar class:
Example 1:
// Java Code to illustrate // computeFields() Method import java.util.*; public class CalendarClassDemo extends GregorianCalendar { public static void main(String args[]) { // Creating calendar object CalendarClassDemo calndr = new CalendarClassDemo(); // Displaying the current date System.out.println("The Current " + "date is: " + calndr.getTime()); // Clearing the calendar calndr.clear(); // Set a new year and call // computeFields() calndr.set(GregorianCalendar.YEAR, 2016); calndr.computeFields(); // Displaying the current date System.out.println("The recent" + " date is: " + calndr.getTime()); } } |
The Current date is: Wed Feb 13 16:15:15 UTC 2019 The recent date is: Wed Feb 13 16:15:15 UTC 2019
Example 2:
// Java Code to illustrate // computeFields() Method import java.util.*; public class CalendarClassDemo extends GregorianCalendar { public static void main(String args[]) { // Creating calendar object CalendarClassDemo calndr = new CalendarClassDemo(); // Displaying the current date System.out.println("The Current " + "date is: " + calndr.getTime()); // Clearing the calendar calndr.clear(); // Set a new year and call // computeFields() calndr.set(GregorianCalendar.YEAR, 2000); calndr.computeFields(); // Displaying the current date System.out.println("The recent" + " date is: " + calndr.getTime()); } } |
The Current date is: Wed Feb 13 16:15:18 UTC 2019 The recent date is: Wed Feb 13 16:15:18 UTC 2019
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#computeFields()
Recommended Posts:
- Calendar add() Method in Java with Examples
- Calendar set() Method in Java with Examples
- Calendar get() method in Java with Examples
- Calendar internalGet() Method in Java with Examples
- Calendar setLenient() Method in Java with Examples
- Calendar setFirstDayOfWeek() Method in Java with Examples
- Calendar getTimeZone() Method in Java with Examples
- Calendar getActualMinimum() Method in Java with Examples
- Calendar getActualMaximum() Method in Java with Examples
- Calendar getDisplayName() Method in Java with Examples
- Calendar getDisplayNames() Method in Java with Examples
- Calendar getAvailableLocales() Method in Java with Examples
- Calendar getTime() Method in Java with Examples
- Calendar getGreatestMinimum() Method in Java with Examples
- Calendar getInstance() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


