Calendar complete() Method in Java with Examples

The complete() method in Calendar class is used to fill any unset fields of the calendar fields. It follows the following process of completion:

  1. At first, if the time value has not been calculated, the computeTime() method is called to calculate it from calendar field values.
  2. Second, to calculate all the calendar field values the computeFields() method is used.

Syntax:

protected void complete()

Parameters: The method does not take any parameters.

Return Value: The method does not return any value.

Below programs illustrate the working of complete() Method of Calendar class:
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate
// complete() 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
        // complete()
        calndr.set(GregorianCalendar.YEAR, 2008);
        calndr.complete();
  
        // Displaying the current date
        System.out.println("The new"
                           + " date is: " + calndr.getTime());
    }
}

chevron_right


Output:

The Current date is: Wed Feb 13 15:39:33 UTC 2019
The new date is: Tue Jan 01 00:00:00 UTC 2008

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate
// complete() 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
        // complete()
        calndr.set(GregorianCalendar.YEAR, 1996);
        calndr.complete();
  
        // Displaying the current date
        System.out.println("The new"
                           + " date is: " + calndr.getTime());
    }
}

chevron_right


Output:

The Current date is: Wed Feb 13 15:39:36 UTC 2019
The new date is: Mon Jan 01 00:00:00 UTC 1996

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#complete()



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.