.complete()

Published Aug 21, 2023Updated May 15, 2024
Contribute to Docs

The .complete() method of the Calendar class is used to fill in any empty fields in the Calendar instance.

Syntax

someCalendar.complete()

Note: If the time value has not been calculated from the calendar field values, the .computeTime() method is called. Then to calculate all calendar field values, the .computeFields() method is called.

Example

The example below demonstrates the use of the .complete() method.

import java.util.GregorianCalendar;
public class CalendarCompleteExample extends GregorianCalendar {
public static void main(String[] args) {
// Create a new calendar
CalendarCompleteExample cal = new CalendarCompleteExample();
// Print the current date
System.out.println("The current date is : " + cal.getTime());
// Clear the calendar
cal.clear();
// Set a new year and call complete()
cal.set(GregorianCalendar.YEAR, 2021);
cal.complete();
// Print the current date
System.out.println("New date is : " + cal.getTime());
}
}

This code will return an output similar to the following;

The current date is: Mon Aug 14 15:39:33 UTC 2023
The new date is: Tue Jan 01 00:00:00 UTC 2021

All contributors

Looking to contribute?

Learn Java on Codecademy